X86 calling conventions


X86 calling conventions

This article describes the calling conventions used on the x86 architecture.

Calling conventions describe the interface of called code:
* The order in which parameters are allocated
* Where parameters are placed (pushed on the stack or placed in registers)
* Which registers may be used by the function
* Whether the caller or the callee is responsible for unwinding the stack on return

A closely related topic is name mangling, which determines how symbol names in the code map to symbol names used by the linker.

It should be noted that there are often subtle differences in how various compilers implement these conventions, so it is often difficult to interface code which is compiled by different compilers. On the other hand, conventions which are used as an API standard (like stdcall) are necessarily very uniformly implemented.

Historical background

In the times of UNIX mainframes, the machine manufacturer also used to provide an OS for it and most of (if not all) the software including a C compiler. So there used to be only one calling convention — the one implemented by the "official" compiler.The IBM PC case was totally different. One firm (IBM) provided the hardware, another (Intel) made the processor, the third (Microsoft) was responsible for the OS (MS-DOS), and many others wrote compilers for quite a number of programming languages. Different mutually exclusive calling schemes were thus designed to satisfy their different requirements.

Caller clean-up

In these conventions the caller cleans the arguments from the stack, which allows for variable argument lists, eg. printf().

cdecl

The cdecl calling convention is used by many C systems for the x86 architecture. In cdecl, function parameters are pushed on the stack in a right-to-left order. Function return values are returned in the EAX register (except for floating point values, which are returned in the x87 register ST0). Registers EAX, ECX, and EDX are available for use in the function.

For instance, the following C code function prototype and function call:

int function_name(int, int, int);int a, b, c, x;...x = function_name(a, b, c);

will produce the following x86 Assembly code (written in MASM syntax, with destination first):

push cpush bpush acall function_nameadd esp, 12 ;Stack clearingmov x, eax

The calling function cleans the stack after the function call returns.

There are some variations in the interpretation of cdecl, particularly in how to return values. As a result, x86 programs compiled for different operating system platforms and/or by different compilers can be incompatible, even if they both use the "cdecl" convention and do not call out to the underlying environment. Some compilers return simple data structures with the length of 2 registers or less in EAX:EDX, and larger structures and class objects requiring special treatment by the exception handler (e.g., a defined constructor, destructor, or assignment) are returned in memory. To pass "in memory", the caller allocates memory and passes a pointer to it as a hidden first parameter; the callee populates the memory and returns the pointer, popping the hidden pointer when returning.

In Linux/gcc double/floating point values should be pushed on the stack via the x87 pseudo-stack. Like so:sub esp,8; make room for the doublefld [ebp+x] ; load our double onto the floating point stackfstp [esp] ; push our double onto the stackcall func;add esp,8;Using this method ensure it is pushed on the stack in the correct format.

The cdecl calling convention is usually the default calling convention for x86 C compilers, although many compilers provide options to automatically change the calling conventions used. To manually define a function to be cdecl, some support the following syntax:

void _cdecl function_name(params);

The _cdecl modifier must be included in the function prototype, and in the function declaration to override any other settings that might be in place.

syscall

This is similar to cdecl in that arguments are pushed right to left. EAX, ECX, and EDX are not preserved. The size of the parameter list in doublewords is passed in AL.

Syscall is the standard calling convention for 32 bit OS/2 API.

optlink

Arguments are pushed right to left. The three lexically first (leftmost) arguments are passed in EAX, EDX, and ECX and up to four floating-point arguments are passed in ST(0) thru ST(3), although space for them is reserved in the argument list on the stack. Results are returned in EAX or ST(0). Registers EBP, EBX, ESI, and EDI are preserved.

Optlink is used by the IBM VisualAge compilers.

Callee clean-up

When the callee cleans the arguments from the stack it needs to be known at compile time how many bytes the stack needs to be adjusted. Therefore, these calling conventions are not compatible with variable argument lists, eg. printf(). They may be, however, slightly more efficient as the code needed to unwind the stack does not need to be generated by the calling code.

Functions which utilize these conventions are easy to recognize in ASM code because they will unwind the stack prior to returning. The x86 ret instruction allows an optional byte parameter that specifies the number of stack locations to unwind before returning to the caller. Such code looks like this:

ret 12

pascal

The parameters are pushed on the stack in left-to-right order (opposite of cdecl), and the callee is responsible for balancing the stack before return.

This calling convention was common in the following 16 bit APIs OS/2 1.x , Microsoft Windows 3.x, and Borland Delphi version 1.x.

register

An old alias for Borland fastcall.

stdcall

The stdcall [http://msdn2.microsoft.com/en-us/library/zxk0tw93(vs.71).aspx] calling convention is a variation on the pascal calling convention in which parameters are passed on the stack, pushed right-to-left. Registers EAX, ECX, and EDX are designated for use within the function. Return values are stored in the EAX register. The callee is responsible for cleanup of the stack.

Stdcall is the standard calling convention for the Microsoft Win32 API.

fastcall

Conventions entitled fastcall have not been standardized, and have been implemented differently, depending on the compiler vendor.

Microsoft fastcall

* Microsoft or GCC [http://www.ohse.de/uwe/articles/gcc-attributes.html#func-fastcall] __fastcall [http://msdn2.microsoft.com/en-us/library/Aa271991] convention (aka __msfastcall) passes the first two arguments (evaluated left to right) that fit into ECX and EDX. Remaining arguments are pushed onto the stack from right to left.

Borland fastcall

Evaluating arguments from left to right, it passes three arguments via EAX, EDX, ECX. Remaining arguments are pushed onto the stack, also left to right.

It's the default calling convention of Borland Delphi.

Watcom register based calling convention

Watcom does not support the "__fastcall" keyword except to alias it to null. The register calling convention may be selected by command line switch. (However, IDA uses "__fastcall" anyway for uniformity)

Up to 4 registers are assigned to arguments in the order eax, edx, ebx, ecx. Arguments are assigned to registers from left to right. If any argument cannot be assigned to a register (say it is too large) it, and all subsequent arguments, are assigned to the stack. Arguments assigned to the stack are pushed from right to left. Names are mangled by adding a suffixed underscore.

Variadic functions fall back to the Watcom stack based calling convention.

The Watcom C/C++ compiler also uses the #pragma aux [http://www.openwatcom.org/index.php/Calling_Conventions#Specifying_Calling_Conventions_the_Watcom_Way] directive that allows the user to specify his own calling convention. As its manual states, "Very few users are likely to need this method, but if it is needed, it can be a lifesaver".

TopSpeed / Clarion / JPI

The first four integer parameters are passed in registers eax, ebx, ecx and edx. Floating point parameters are passed on the floating point stack – registers st0, st1, st2, st3, st4, st5 and st6. Structure parameters are always passed on the stack. Additional parameters are passed on the stack after registers are exhausted. Integer values are returned in eax, pointers in edx and floating point types in st0.

safecall

In Borland Delphi on Microsoft Windows, the safecall calling convention encapsulates COM (Component Object Model) error handling, so that exceptions aren't leaked out to the caller, but are reported in the HRESULT return value, as required by COM/OLE. When calling a safecall function from Delphi code, Delphi also automatically checks the returned HRESULT and raises an exception if necessary. Together with language-level support for COM interfaces and automatic IUnknown handling (implicit AddRef/Release/QueryInterface calls), the safecall calling convention makes COM/OLE programming in Delphi easy and elegant.

The safecall calling convention is the same as the stdcall calling convention, except that exceptions are passed back to the caller in EAX as a HResult (instead of in FS: [0] ), while the function result is passed by reference on the stack as though it were a final "out" parameter. When calling a Delphi function from Delphi this calling convention will appear just like any other calling convention, because although exceptions are passed back in EAX, they are automatically converted back to proper exceptions by the caller. When using COM objects created in other languages, the HResults will be automatically raised as exceptions, and the result for Get functions is in the result rather than a parameter. When creating COM objects in Delphi with safecall, there is no need to worry about HResults, as exceptions can be raised as normal but will be seen as HResults in other languages.

function function_name(a: DWORD): DWORD; safecall;

Returns a result and raises exceptions like a normal delphi function, but it passes values and exceptions as though it was:

function function_name(a: DWORD; out Result: DWORD): HResult; stdcall;

Either caller or callee clean-up

thiscall

This calling convention is used for calling C++ non-static member functions. There are two primary versions of thiscall used depending on the compiler and whether or not the function uses variable arguments.

For the GCC compiler, thiscall is almost identical to cdecl: the calling function cleans the stack, and the parameters are passed in right-to-left order. The difference is the addition of the this pointer, which is pushed onto the stack last, as if it were the first parameter in the function prototype.

On the Microsoft Visual C++ compiler, the this pointer is passed in ECX and it is the "callee" that cleans the stack, mirroring the stdcall convention used in C for this compiler and in Windows API functions. When functions use a variable number of arguments, it is the caller that cleans the stack (cf. cdecl).

The thiscall calling convention can only be explicitly specified on Microsoft Visual C++ 2005 and later. On any other compiler "thiscall" is not a keyword. (Disassemblers like IDA, however, have to specify it anyway. So IDA uses keyword "__thiscall__" for this)

Intel ABI

The Intel Application Binary Interface is a computer programming standard that most compilers and languages follow. According to the Intel ABI, the EAX, EDX, and ECX are to be free for use within a procedure or function, and need not be preserved.

Microsoft x64 calling convention

The x86-64 or x64 calling convention takes advantage of additional register space in the AMD64 / Intel64 platform. The registers RCX, RDX, R8, R9 are used for integer and pointer arguments, and XMM0, XMM1, XMM2, XMM3 are used for floating point arguments. Additional arguments are pushed onto the stack. The return value is stored in RAX.

Note that when compiling for the x64 architecture using Microsoft tools, there is only one calling convention — the one described here, so that stdcall, thiscall, cdecl, fastcall, etc., are now all one and the same.

On x86, one could create thunks that convert any function call from stdcall to thiscall by placing the 'this' pointer in ECX and jumping to the member function address. On x64 a universal stdcall-to-thiscall thunk cannot be written, except for functions that take no arguments. Putting the implicit 'this' in place requires shifting all the arguments, whose number and sizes are unknown.

In the Microsoft x64 calling convention, it's the caller's responsibility to allocate 32 bytes of "shadow space" on the stack right before calling the function (regardless of the actual number of parameters used), and to pop the stack after the call. The shadow space is used to spill RCX, RDX, R8, and R9.

AMD64 ABI convention

The calling convention of the AMD64 application binary interface is followed on Linux and other non-Microsoft operating systems. The registers RDI, RSI, RDX, RCX, R8 and R9 are used for integer and pointer arguments while XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6 and XMM7 are used for floating point arguments. As in the Microsoft x64 calling convention, additional arguments are pushed onto the stack and the return value is stored in RAX.

Standard exit and entry sequences for C code

The Standard Entry Sequence to a function is as follows:

_function: push ebp ;store the old base pointer mov ebp, esp ;make the base pointer point to the current stack location – at ;the top of the stack is the old ebp, followed by the return ;address and then the parameters. sub esp, x ;x is the size, in bytes, of all "automatic variables" ;in the function

This sequence preserves the original base pointer EBP; points EBP to the current stack pointer (which points at the old EBP, followed by the return address and then the function parameters); and then creates space for automatic variables on the stack. Local variables are created on the stack with each call to the function, and are cleaned up at the end of each function. This behaviour allows for functions to be called recursively. In C and C++, variables declared "automatic" are created in this way.

The Standard Exit Sequence goes as follows:

mov esp, ebp ;reset the stack to "clean" away the local variablespop ebp ;restore the original base pointerret ;return from the function

Note that while functions tend to have only one entry point, they may have multiple exit points, and thus may well have more than one standard exit sequence, or a jump to the standard exit sequence in the function body.

The following C function:

int _cdecl MyFunction(int i){ int k; return i + k;}

would produce the equivalent asm code:

;entry sequencepush ebpmov ebp, espsub esp, 4 ;create function stack frame

;function codemov eax, [ebp + 8] ;move parameter i to accumulatoradd eax, [ebp - 4] ;add k to i ;result is returned in eax

;exit sequencemov esp, ebppop ebpret

Note that many compilers can optimize these standard sequences away when not needed (often called "no stackframe generation"). If you require them for e.g. interlanguage interfacing, you probably need to search your compiler manual for a compiler directive (or pragma) to turn this kind of optimization locally off.

External links

* [http://www.sco.com/developers/devspecs/abi386-4.pdf System V Application Binary Interface Intel386 Architecture Processor Supplement]
* [http://www.codeproject.com/cpp/calling_conventions_demystified.asp The Code Project—Calling Conventions Demystified]
* [http://www.swissdelphicenter.ch/torry/printcode.php?id=1233 About Calling conventions]
* [http://www.unixwiz.net/techtips/win32-callconv-asm.html Intel x86 Function-call Conventions – Assembly View]
* [http://msdn2.microsoft.com/en-us/library/zthk2dkh(VS.80).aspx Microsoft x64 Calling Convention]
* [http://www.angelcode.com/dev/callconv/callconv.html Calling Conventions]
* [http://www.agner.org/optimize/calling_conventions.pdf Calling Conventions on x86 by Agner Fog (pdf)]
* [http://www.x86-64.org/documentation/abi-0.99.pdf AMD64 ABI (pdf)]
*The Old New Thing — the history of calling conventions (by Raymond Chen) — [http://blogs.msdn.com/oldnewthing/archive/2004/01/02/47184.aspx Part1] , [http://blogs.msdn.com/oldnewthing/archive/2004/01/07/48303.aspx Part2] , [http://blogs.msdn.com/oldnewthing/archive/2004/01/08/48616.aspx Part3] , [http://blogs.msdn.com/oldnewthing/archive/2004/01/13/58199.aspx Part4(ia64)] , [http://blogs.msdn.com/oldnewthing/archive/2004/01/14/58579.aspx Part5(amd64)]


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Calling convention — In computer science, a calling convention is a standardized method for a program to pass parameters to a function and receive a result value back from it. Calling conventions can differ in: * where they place parameters and return values (in… …   Wikipedia

  • x86 — This article is about Intel microprocessor architecture in general. For the 32 bit generation of this architecture which is also called x86 , see IA 32. x86 Designer Intel, AMD Bits 16 bit, 32 bit, and/or 64 bit Introduced 1978 Design …   Wikipedia

  • Microsoft Macro Assembler — Developer(s) Microsoft Stable release 10.0.30319.1 / April 12, 2010; 18 months ago (2010 04 12) Operating system Microsoft Windows and MS DOS …   Wikipedia

  • Netwide Assembler — Original author(s) Simon Tatham, Julian Hall Developer(s) H. Peter Anvin, et al. Stable release 2.09.09 / July 3, 2011; 4 months ago (2011 07 03) …   Wikipedia

  • Comparison of assemblers — This is a list of assemblers: computer programs that translate ( assemble ) assembly language source code into binary programs. Contents 1 x86 assemblers 2 Multiple target assemblers 3 Other assemblers …   Wikipedia

  • A86 (software) — A386 redirects here. For the A road in England, see A386 road (Great Britain). A86 Developer(s) Eric Isaacson Stable release 4.05 Operating system DOS, Windows Platform …   Wikipedia

  • Open Watcom Assembler — Original author(s) Open Watcom Assembler Operating system Microsoft Windows, Unix like, OS/2, Mac OS, DOS Available in English …   Wikipedia

  • Aufrufkonvention — Unter Aufrufkonvention (engl. calling convention) versteht man die Methode, mit der in Computerprogrammen einer Funktion Daten übergeben werden. In der Regel liegt es am Compiler, welche Konvention zum Einsatz kommt, so dass der Programmierer… …   Deutsch Wikipedia

  • Assembly language — See the terminology section below for information regarding inconsistent use of the terms assembly and assembler. Motorola MC6800 Assembly Language An assembly language is a low level programming language for computers, microprocessors,… …   Wikipedia

  • Name mangling — This article is about name mangling in computer languages. For name mangling in file systems, see filename mangling. In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the …   Wikipedia