YinYang.ERROR
YinYang.ERROR

Reputation: 11

C/C++ return from x86 code

I am currently working on using some ASM in C/C++

I have the following

__declspec(naked) unsigned long 
someFunction( unsigned long inputDWord )
{
    __asm
    {
    }
}

how, in asm, would I return the unsigned long?

Do I need to push something onto the stack and then call ret? I haven't used Asm in a long time, and never inside C++ before.

Thanks!

Upvotes: 0

Views: 310

Answers (1)

valdo
valdo

Reputation: 12943

EDIT: Thanks to @Matteo Italia, I've corrected the usage of ret.

Put the retval in eax register, this is according to __cdecl and __stdcall conventions.

Then, depending on the calling convention, you should use the appropriate variant of ret instruction:

  • In case of __cdecl convention (or similar) - use ret. On machine level this means pop-ing the return address from the stack and jmp to it. The caller is responsible for removing all the function parameters from the stack.
  • In case of __stdcall convention (or similar) - use ret X, whereas X is the size of all the function arguments.

Upvotes: 2

Related Questions