Pageviews

Wednesday, July 22, 2015

stdcall vs cdecl

The _stdcall calling convention is a variation on the Pascal calling convention in which the callee is responsible for cleaning up the stack, so the compiler makes vararg functions__cdecl. 
stdcall is the standard calling convention for the Microsoft Win32 API and for Open Watcom C++.
Element
Implementation
Argument-passing order
Right to left.
Argument-passing convention
By value, unless a pointer or reference type is passed.
Stack-maintenance responsibility
Callee pops its own arguments from the stack.
Name-decoration convention
An underscore (_) is prefixed to the name. The name is followed 
by the at sign (@) followed by the number of bytes (in decimal) 
in the argument list. Therefore, the function declared as
int func( int a, double b ) is decorated as follows: _func@12
Case-translation convention
No case translation performed.

The _cdecl calling convention is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions.  Registers EAX, ECX, and EDX are designated for use within the function. Return values are stored in the EAX register.
Element
Implementation
Argument-passing order
Right to left.
Stack-maintenance responsibility
Caller pops the arguments from the stack.
Name-decoration convention
Underscore character (_) is prefixed to names, except 
when __cdecl functions that use C linkage are exported.
Case-translation convention
No case translation performed.
Subtle differences
The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code.