Visual C++ provides different calling conventions based on users requirements for calling internal as well as external functions.
Some of the widely used conventions are __cdecl, __stdcall, __fastcall and __thiscall.
We will be discussing how these calling convention differs on the basis of how the arguments are passed, how the stack is cleared after the execution of the function and based on the return value of these functions.
__cdecl :
it is one of the most popular and is also the default calling convention in C and C++ programs. In cdecl, parameters are pushed onto the stack from right to left, the caller cleans up the stack once the function is complete and the return value of that function is stored in EAX. The cdec function call generally creates executable having greater size than stdcall because it requires each function call to include stack cleanup code.
Example
push a
push b
push c
call test
add esp, 12
as we can see that the add instruction cleans up the stack after the function call.
__stdcall :
__stdcall is similar to cdecl as the arguments are pushed to the stack before function is called, the main difference between them is in stdcall the callee function is responsible for clearing the stack once the function execution is finished.
like the above assembly would be different in case of stdcall as the function epilogue of test function would be responsible for cleaning up the stack.
stdcall is the standard calling convention for windows API. any code calling these API fuinctions will not require to clean up the stack because thats the responsibility of DLLs that implement the code for the API function.
__fastcall :
__fastall convention varies across compilers, but generally it works similarly in all the cases. In fastcall first few arguments are stored in a registers , the most commonly used register for storing arguments are EDX and ECX.
Additional arguments are loaded from right to left and the calling function is usually responsible for clearing up the stack.
It is generally more efficient to use fastcall as compared to other calling conventions as the code doesn’t need to involve stack as much.
__thiscall :
__thiscall is the calling conventions used on the member functions and it is also the default calling convention for the member function of C++ that are not using vararg(Variable Arguments).
In __thiscall callee cleans the stack which is not possible in case of vararg functions and the arguments pushed onto the stack from right to left and rather than passing the this pointer through stack in __thiscall it is passed to the ecx register.
There are other calling conventions too like __vectorcall and __clrcall but are rarely used, majority of time the above mentioned calling conventions are used.
Conclusion:
We discussed about the different calling conventions in Visual C/C++ and how do they differ from each other, these different approaches are important for us to debug the program and link our code to assembly-level routines.
Leave a Reply