Call instruction clarification

Programming, for all ages and all languages.
Post Reply
hafai
Posts: 11
Joined: Wed Mar 25, 2009 4:16 pm

Call instruction clarification

Post 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.
Developer of CDE the Cooperative Driver Environment <No website currently>
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Call instruction clarification

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
hafai
Posts: 11
Joined: Wed Mar 25, 2009 4:16 pm

Re: Call instruction clarification

Post 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.
Developer of CDE the Cooperative Driver Environment <No website currently>
Post Reply