X86 calling conventions
lastmod 2018-08-17created 2015-04-056 edits
Describes the calling conventions used on the x86 architecture.
stdcall vs cdecl
- Difference between __stdcall and __cdecl!!! Endless Challenge1<span style="color:red">(At the top of the post, the definitions of
stdcallandcdeclare swapped)
A brief summary of the differences between __stdcall and __cdecl.
- With
cdecl, the calling function cleans up the stack. - With
stdcall, the stack is cleaned up inside the called function.- Using asm:ret is faster than cleaning up the SP (Stack Pointer) with ADD.
stdcall is faster than cdecl, but does not support variadic arguments.- This is because, with variadic arguments, the number of arguments cannot be determined directly inside the function.
stdcall where possible for functions called by the OS or functions in a DLL. cdecl
__cdecl is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall, because it includes stack cleanup code in each function call. The following list shows the implementation of this calling convention.
| Element | Implementation |
| Argument-passing order | Right to left |
| Argument-passing convention | Calling function pops the arguments from the stack. |
| Stack-maintenance responsibility | Called function pops its own 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. |
stdcall
The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl. Functions that use this calling convention require a function prototype.
| Element | Implementation |
| Argument-passing order | Right to left |
| Argument-passing convention | By value, unless a pointer or reference type is passed |
| Stack-maintenance responsibility | Called function 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. |
| Case-translation convention | None |
fastcall
The __fastcall calling convention specifies that arguments to functions are to be passed in registers, when possible. This calling convention only applies to the x86 architecture. The following list shows the implementation of this calling convention.
| Element | Implementation |
| Argument-passing order | The first two DWORD or smaller arguments that are found in the argument list from left to right are passed in ECX and EDX registers; all other arguments are passed on the stack from right to left. |
| Stack-maintenance responsibility | Called function pops the arguments from the stack. |
| Name-decoration convention | At sign (@) is prefixed to names. An at sign followed by the number of bytes (in decimal) in the parameter list is suffixed to names. |
| Case-translation convention | No case translation performed. |
See also
Favorite site
- Wikipedia (en) x86 calling conventions
- Wikipedia (ko) x86 calling conventions
- Calling conventions (__cdecl,__stdcall,__fastcall,thiscall,naked)
- MSDN: Decorated names
- [Recommended] C/C++ calling conventions: differences between __cdecl, __pascal, and __stdcall 2