Advertisement

Calling styles

Started by June 03, 2000 12:47 AM
4 comments, last by Jonathan 24 years, 6 months ago
Does anyone know what _cdecl actually means under Visual C++? I''m guessing it has something to do with calling from DLLs, but I''m lost at present Jonathan
As far as I know, cdecl is a calling convention, like PASCAL and Stdcall, and fastcall, etc.. From what I know, the calling convention specifies how and in what order the parameters should be pushed onto the stack, and maybe some other stuff (but I don''t know that other stuff )
Advertisement
The big difference between the conventions is who is responsible for cleaning up the stack after the function finishes. In _cdecl (the default), the caller will clean up the stack when the called function returns. In _pascal (_stdcall in Windows) the called function cleans up the stack before it returns. This is mainly only used for calling Win API functions, and DLLs I believe. fastcall doesn''t use the stack at all (unless there are lots of parameters passed) but places the parameters in registers, which should be slightly faster depending on how they''re used.

Rock
I -believe- ''fastcall'' will try to use up to 2 registers to pass variables, but any more than that will be passed on the stack as normal. There is also ''thiscall'', which I think is for C++ member functions (just means it puts the ''this'' pointer in there somewhere, too.)
Calling convention also affects name-mangling and case translation. The latter is much of a big deal as none of the MSVC calling conventions perform case translation, but if working with object modules developed with other programs, it''s something you need to know.

__cdecl for MSVC:
- passes arguments right to left.
- calling function pops arguments from stack.
- Underscore (_) is prefixed to decorate names.
- no case translation performed.

__fastcall for MSVC:
- first two DWORD or smaller arguments are passed in ECX and EDX registers; all other arguments passed right to left.
- called function pops arguments from stack.
- An at sign (@) is prefixed to names. An at sign (@) followed by number of bytes (in decimal) in parameter list is suffixed to names.
- no case translation performed.

__stdcall for MSVC:
- arguments are pass right to left.
- called function pops arguments from stack.
- An underscore is prefixed to names. An at sign (@) followed by number of bytes (in decimal) in parameter list is suffixed to names.
- no case translation performed.

In MSVC thiscall functions push the arguments on the stack as well as puts the this pointer in register ECX. For varargs style functions, it uses _cdecl conventions, otherwise uses _stdcall conventions.
fastcall only allows 2 register params? What a jip! I thought they stole that from Watcom, which will pretty much fill up all the registers with parameters. I loved that feature. With only 2, that seems to pretty much defeat the purpose of using it (which I guess is why I''ve never seen it used).

Rock

This topic is closed to new replies.

Advertisement