View Single Post
Old 09-10-2005, 04:27 AM   #4 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
As for your original question...
For that particular example, essentially yes, but it depends on the calling convention the function uses.

In the default C/C++ calling convention, cdecl, arguments are pushed onto the stack right to left by the calling function, and the calling function is also the one that cleans up the stack.

Win32 API functions use a different calling convention, stdcall. Arguments are pushed right to left as with cdecl, but the called function is responsable for cleaning up the stack (ie, stdcall functions end with stuff like ret 4, ret 8, etc).

Since for variable argument functions only the calling functions knows the number of arguments, cdecl has to be used for those.

Win16 used the pascal/fortran calling convention where arguments are pushed left to right onto the stack. Called function cleans the stack.

There's also fastcall, used by MS for some stuff, which passes the first two arguments that fit into a register through ecx & edx and passes the rest rigth to left through the stack. Called functions cleans up the stack.

Thiscall is the default for C++ member function, with "this" being passed through ecx, the rest right to left through the stack, called function cleans the stack.

GCC has the "regparm(num)" attribute you can use on a function to specify it should pass up to num arguments through registers. I think the maximum is 3.

You don't really need to know all that for solving your problem, as redhead pretty much did , but it's nice stuff to know anyway.
Locutus is offline   Reply With Quote