2011-05-10 3 views
0

어셈블러에서 C로 문자열의 addres를 얻습니다.이 주소의 내용을 가져와야합니다. 어떻게 수행하나요? Google은 reinterpret_cast를 사용하여 C++ 예제를 제공하지만 C 언어에서는 작동하지 않습니다 (아마도). 있다면주소를 문자열 상수

char* p = (char*) <your address here>; 
// use p for whatever here 

: 당신은 너무 libs가 필요 참고한다면 당신은 그 제로와 바이트가 문자열 뒤에있다을 알고 있다면 나는 다음이 시도

#include <stdio.h> 
#include <stdlib.h> 

unsigned long const1(void); 

int main() 
{ 
    printf("Const: %d\n", const1()); 
    return 0; 
} 
+2

시도한 코드와 작동하지 않는 코드를 보여줍니다. –

+0

어떤 컴파일러입니까? 어떤 건축인가요? – MrAnonymous

+0

'#include #include 부호없는 long const1 (void); int main() { \t printf ("Const : % d \ n", const1()); \t return 0; } – yons88

답변

3

주소가 이미 있고 널 종료 문자열 인 경우 그 문자열을 문자열처럼 처리해야합니다.

printf("%s", (char*)alleged_string_address); 
0

tenx, 감사합니다 문자열 뒤에 0이 없으면 C의 표준 문자열 함수가 실패합니다.

1

예비 대답 나는 당신의 정보를 기다리는 동안 :

char* foo = (char*)...pointer from assembly...; 
    *foo = 'a'; /* write a to the address pointed at by foo */ 
    foo++;  /* increment the address of foo by 1 */ 
    *foo = 'b'; /* write b to that address. foo now contains ab, if it points at RAM. */ 

이 답변은 임베디드 시스템을 위해 개발된다. 주변 장치 레지스터와 같은 포인터가 필요하면 volatile을 사용하여 컴파일러 최적화를 피하십시오.

volatile char* foo = (char*)...pointer from assembly...; 
    *foo = 'a'; /* write a to the address pointed at by foo */ 
    foo++;  /* increment the address of foo by 1 */ 
    *foo = 'b'; /* write b to that address. foo now contains ab, if it points at RAM. */ 
+0

'휘발성 (volatile)'의 +1은 특별히 이와 같은 것들을 위해 만들어졌습니다. – orlp