Quote from: http://www.nasm.us/doc/nasmdoc9.html
9.1.2 Function Definitions and Function Calls
The C calling convention in 32-bit programs is as follows. In the following description, the words caller and callee are used to denote the function doing the calling and the function which gets called.
- The caller pushes the function's parameters on the stack, one after another, in reverse order (right to left, so that the first argument specified to the function is pushed last).
- The caller then executes a near
instruction to pass control to the callee.CALL - The callee receives control, and typically (although this is not actually necessary, in functions which do not need to access their parameters) starts by saving the value of
inESP
so as to be able to useEBP
as a base pointer to find its parameters on the stack. However, the caller was probably doing this too, so part of the calling convention states thatEBP
must be preserved by any C function. Hence the callee, if it is going to set upEBP
as a frame pointer, must push the previous value first.EBP - The callee may then access its parameters relative to
. The doubleword atEBP
holds the previous value of[EBP]
as it was pushed; the next doubleword, atEBP
, holds the return address, pushed implicitly by[EBP+4]
. The parameters start after that, atCALL
. The leftmost parameter of the function, since it was pushed last, is accessible at this offset from[EBP+8]
; the others follow, at successively greater offsets. Thus, in a function such asEBP
which takes a variable number of parameters, the pushing of the parameters in reverse order means that the function knows where to find its first parameter, which tells it the number and type of the remaining ones.printf - The callee may also wish to decrease
further, so as to allocate space on the stack for local variables, which will then be accessible at negative offsets fromESP
.EBP - The callee, if it wishes to return a value to the caller, should leave the value in
,AL
orAX
depending on the size of the value. Floating-point results are typically returned inEAX
.ST0 - Once the callee has finished processing, it restores
fromESP
if it had allocated local stack space, then pops the previous value ofEBP
, and returns viaEBP
(equivalently,RET
).RETN - When the caller regains control from the callee, the function parameters are still on the stack, so it typically adds an immediate constant to
to remove them (instead of executing a number of slowESP
instructions). Thus, if a function is accidentally called with the wrong number of parameters due to a prototype mismatch, the stack will still be returned to a sensible state since the caller, which knows how many parameters it pushed, does the removing.POP
There is an alternative calling convention used by Win32 programs for Windows API calls, and also for functions called by the Windows API such as window procedures: they follow what Microsoft calls the
convention. This is slightly closer to the Pascal convention, in that the callee clears the stack by passing a parameter to the
instruction. However, the parameters are still pushed in right-to-left order.