Page 1 of 1

Call instruction clarification

Posted: Mon Aug 29, 2011 10:16 am
by hafai
When I execute the call instruction it pushes the return address to the stack, right?

Code: Select all

CODE         | Resulting stack (High address)
PUSH RAX     | RAX's value
PUSH RBX     | RBX's value
CALL CODE    | RET address
That would result in a stack shown on the right. And if I want to get at the pushed RAX/RBX values than I need to first put the address that CALL put there somewhere safe and return it to the stack before RET.

Am I missing anything else? Considering I have been programming in Assembly for two years and only found this out now I have a feeling that there might be more. Or that I got this wrong.

Re: Call instruction clarification

Posted: Mon Aug 29, 2011 10:37 am
by Brendan
Hi,

Code: Select all

    PUSH RAX     | RAX's value
    PUSH RBX     | RBX's value
    CALL CODE    | RET address
...

CODE:
    mov eax,[esp]        ;eax = return address
    mov eax,[esp+4]      ;eax = first parameter on stack
    add eax,[esp+8]      ;eax = sum of parameters = returned value
    ret                  ;For "C calling convention" where caller cleans up their own stack
    ret 4*2              ;For "Pascal calling convention" where callee cleans up the stack
It gets a little messier if you're preserving registers and using local variables though (and much worse once you start looking at 64-bit calling conventions).


Cheers,

Brendan

Re: Call instruction clarification

Posted: Mon Aug 29, 2011 11:02 am
by hafai
I decided to forego a predefined calling convention and implement a pre-made one later. I'm just using something simple that I thought up so I can get to writing my code.

But I ran into several interrupt resulting issues whenever I tried to retrieve the variables. Now I know why.