Calling conventions on the x86 platform

2005/02/13, Andreas Jönsson

This is a document that I wrote as research for the AngelCode Scripting Library. Since the library uses assembly to make the interaction between the script engine and the host application I needed to have complete knowledge of how the calling conventions are implemented by different compilers. To my surprise there were a lot more differences than I had initially thought. Most of the differences are related to C++ features, so the differences can be understood as there were no standard when the compilers were first written. Today there is a standard, but I believe that it doesn't mention how calling conventions should be implemented. Which leads to binary incompatibility between compilers, even though the source code is compatible.

The differences doesn't stop AngelScript from supporting each of the compilers. Though new compilers may have to make a few changes in order to follow the conventions used. As support for more compilers are added to AngelScript I will add those compilers to the article.


cdecl

This calling convention is the default for C programs and also global functions in C++ programs. Generally the function arguments are passed on the stack in reverse order so that the callee can access them in the correct order. The caller is responsible for popping the arguments after the function returns, which makes it possible to use the ... to send runtime defined arguments. Return values are returned in the registers.

Visual C++ / Win32

MinGW g++ / Win32

GCC g++ / Linux


stdcall

stdcall is the calling conventions used by the Win32 API. It is basically the same as the cdecl convention with the difference in that the callee is responsible for popping the arguments from the stack. This makes the call slightly faster, but also prevents the use of the ... operator.

Visual C++ / Win32

MinGW g++ / Win32

GCC g++ / Linux


thiscall

This calling convention was introduced with C++. The only sure thing about it is that arguments are pushed on the stack in reverse order and that the caller passes the object pointer to the function in some way or other.

Visual C++ / Win32

MinGW g++ / Win32

GCC g++ / Linux


fastcall

This is a special calling convention that is designed for speed. It is rarely used so I haven't studied it closely, but I understand that the first arguments are passed in registers while the rest are pushed on the stack as normal.

Visual C++ / Win32


Further reading

Not all of these articles are directly related to calling conventions, but they are still a worthy read for anyone interested in interacting with C++ programs on a truly low level.


Revision history