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
  • 134,614
  • 21
  • 188
  • 335
  • 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
  • 505,946
  • 29
  • 409
  • 696
Jerry Coffin
  • 455,417
  • 76
  • 598
  • 1,067
  • 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