Printf the current address in C program

后端 未结 8 1328
时光说笑
时光说笑 2021-02-04 12:27

Imagine I have the following simple C program:

int main() {

int a=5, b= 6, c;
c = a +b; 
return 0;
}

Now, I would like to know the address of

8条回答
  •  生来不讨喜
    2021-02-04 12:30

    For x86:

    int test()
    {
        __asm {
            mov eax, [esp]
        }
    }
    
    
    __declspec(noinline) int main() // or whatever noinline feature your compiler has
    {
        int a = 5;
        int aftertest;
    
        aftertest = test()+3; // aftertest = disasms to 89 45 F8 mov dword ptr [a],eax.
    
        printf("%i", a+9);
        printf("%x", test());
        return 0;
    }
    

提交回复
热议问题