Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is argument push order

I'm learning Assembly language. What exactly is argument push order? i understand its how arguments are pushed to the stack but what does the left and right part mean? left or right of what? Or is this merely to do with the way the command is semantically written, i.e.:

mov ebp, esp ;esp is moved into ebp, right to left.

Is this correct or could someone enlighten me?

Many thanks!

like image 595
Axolotl Avatar asked Feb 04 '12 14:02

Axolotl


People also ask

What order are arguments pushed onto the stack?

ST0 must also be empty when not used for returning a value. In the context of the C programming language, function arguments are pushed on the stack in the right-to-left order, i.e. the last argument is pushed first. The caller cleans the stack after the function call returns.

Why are arguments pushed right to left?

They are pushed right to left so that the leftmost argument is always at the top of the stack in the called function's frame.

Does the order of arguments matter?

Yes, it matters. The arguments must be given in the order the function expects them. C passes arguments by value. It has no way of associating a value with an argument other than by position.


1 Answers

The processor knows no 'function arguments'. Therefore when you want to write f(a,b,c), you really need to push the arguments 'somewhere'.

This is convention. I know that on most x86 machines, function arguments are pushed on the stack from right to left, i.e. first c, then b, then a.

push c
push b
push a
call f

Now the called function can use ebx -1 for a, ebx - 2 for b and ebx - 3 for c.

You could as well establish a convention that says: first two arguments are in registers ebx and ecx, rest are on the stack. As long as the caller and callee agree, you're fine.

like image 82
xtofl Avatar answered Sep 21 '22 00:09

xtofl