From: http://en.wikipedia.org/wiki/X86_calling_conventions
push c
push b
push a
call function_name
add esp, 12 ;Stack clearing
mov x, eax
Why do we need to explicitly add 12 to ESP to clear the stack since the called function should have poped the parameters off the stack therefore restoring the stack pointer...?
Another question:
Theoretically, it would be possible to implement variable parameter functions with the callee taking care of the cleanup right (for instance if you pass the number of arguments on the stack in a register)?
Calling conventions specify how arguments are passed to a function, how return values are passed back out of a function, how the function is called, and how the function manages the stack and its stack frame. In short, the calling convention specifies how a function call in C or C++ is converted into assembly language.
The cdecl (which stands for C declaration) is a calling convention that originates from Microsoft's compiler for the C programming language and is used by many C compilers for the x86 architecture. In cdecl, subroutine arguments are passed on the stack.
The __cdecl function specifier (C++ only) The __cdecl keyword instructs the compiler to read and write a parameter list by using C linkage conventions. To set the __cdecl calling convention for a function, place the linkage keyword immediately before the function name or at the beginning of the declarator.
In CDECL arguments are pushed onto the stack in revers order, the caller clears the stack and result is returned via processor registry (later I will call it "register A"). In STDCALL there is one difference, the caller doeasn't clear the stack, the calle do. You are asking which one is faster.
Because, with the C calling convention, the called function will not pop the parameters. That's the point of this calling convention.
It allows things like variable arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With