1

I am trying to implement this inline assembly trick to obtain the value of EIP in C++Builder. The following code works in Release mode:

unsigned long get_eip()
{
    asm { mov eax, [esp] }
}

however it doesn't work in Debug mode. In Debug mode the code has to be changed to this:

unsigned long get_eip()
{
    asm { mov eax, [esp+4] }
}

By inspecting the generated assembly; the difference is that in Debug mode the code generated for the get_eip() function (first version) is:

push ebp
mov ebp,esp
mov eax,[esp]
pop ebp
ret

however in Release mode the code is:

mov eax,[esp]
ret

Of course I could use #ifdef NDEBUG to work around the problem ; however is there any syntax I can use to specify that the whole function is in assembly and the compiler should not insert the push ebp stuff? (or otherwise solve this problem).

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • 1
    In Debug mode, stack frames are enabled by default. In Release mode, stack frames are disabled by default. There is a setting in the Project Options to control this. – Remy Lebeau Nov 12 '14 at 02:47

1 Answers1

3

Have you tried __declspec(naked)?

__declspec(naked) unsigned long get_eip()
{
    asm { mov eax, [esp] }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • This worked after I changed it to `asm { mov eax, [esp]; ret }` - otherwise it fell through to whatever code was next! – M.M Nov 12 '14 at 02:56