C++Builder - implement entire function in assembly

157 views Asked by At

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).

1

There are 1 answers

1
Jerry Coffin On BEST ANSWER

Have you tried __declspec(naked)?

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