Calling convention

Under calling convention refers to the method used to pass the computer programs in a sub-program data. As a rule, it is on the compiler, which convention is used, so that the programmer does not need to bother yourself. In the development of software in a plurality of languages ​​, it is necessary that all the modules are using compatible calling conventions.

Calling conventions of the x86 architecture

The x86 architecture has many different calling conventions. Because of the limited number of registers, the arguments are mainly transmitted via the stack in many x86 calling conventions, while the return value ( or a pointer to it ) is returned through a register. Some conventions use registers for the first arguments that the performance for simple, often called functions is performed ( such as functions that call no other, and is thereby not returned to ).

Cdecl

The so-called cdecl calling convention is used by many C and C compilers that run on the x86 architecture. The parameters are successively placed from right to left on the stack. Return values ​​are stored by the called function normally when the CPU EAX register. An exception floating-point numbers are stored in ST0. The registers EAX, ECX and EDX are available for use within the function. If other registers are to be used beyond the contents of these registers must be saved by the function (usually by dropping it into the stack) and restored before returning.

Example in C code:

Int function ( int, int, int); / * Function prototype * / int a, b, c, x; / * Variable declaration * /   x = function ( a, b, c); / * Function call * / The function call in the last line generates the following x86 assembly code ( in MASM syntax):

; Put arguments in reverse order on the stack push c push b push a  ; call function call function  ; Reset stack pointer add esp, 12  ; Secure return value of the function mov x, eax The calling function is built after the call to the stack itself again by the stack pointer ( stored in the ESP register ) is set so that it points back to the position in memory, to which he showed before the push operations. In the example above are three integers, ie 12 bytes pushed on the stack. As the stack grows in x86 from top to bottom, while the ESP is decremented by 12. To get back to the position from before, 12 must be added to the value in the ESP register in response to the call again. So also functions with variable arguments number and length can be realized.

The cdecl calling convention is usually the standard x86 calling convention of a C compiler. However, many compilers have the option to use a different convention.

A function can be manually declared using the following syntax as a cdecl function:

_cdecl int function ( int, int, int); stdcall

The stdcall calling convention is the de facto standard calling convention for the Microsoft Win32 API. Function parameters are passed from right to left. The registers EAX, ECX, EDX and are reserved for use within the function are thus modified under certain circumstances. Return values ​​are returned in the EAX register. Unlike cdecl the called function cleans up the stack, not the calling function. Because of this fact support stdcall functions no variable argument lists.

Example ( declaration of a stdcall function in C ):

_stdcall int function ( int, int, int); Functions that use the stdcall method are easy to see in assembly code, as they always degrade the stack itself before returning to the calling code. The x86 instruction ret accepts an optional parameter that specifies the size of the required cuts stacks.

Example: When you return remove 14 bytes of stack.

Ret 14 Pascal

In the Pascal calling convention, parameters are stored in contrast to the cdecl convention, in order from left to right on the stack, and the called function must reset itself the stack pointer before returning to the calling code.

Register ( Fast Call )

The Registrar or fastcall calling convention is compiler specific. In general, it states that the first two or three function arguments with a size of 32 bits or less in registers EAX, EDX, and may also pass ECX instead of on the stack. The remaining arguments are stored from right to left on the stack, similar to cdecl. The Borland and Delphi compiler, however, place the remaining arguments as in the Pascal calling convention from left to right on the stack from. The return values ​​are returned in registers AL, AX, or EAX. For x64 systems, up to four arguments with 64bit or less are passed in special registers, the rest on the stack.

This convention is used among other things in the Linux kernel to pass arguments to system calls. The system -call number that determines every possible call clearly, is stored in the EAX register, while all arguments are stored in the kernel function in the registers EBX, ECX, EDX, ESI, and EDI. Must be passed out of arguments, simply a data structure with the required elements is stored in memory and a pointer passed to this as an argument to the function.

Thiscall

This calling convention is used to call non- static C member functions. There are two main versions of this call, which are used depending on the compiler and depending on whether the function supports variable argument lists or not.

When GCC is this call almost identical to cdecl, the caller cleans up the stack and the parameters are stored from right to left on the stack. The difference is the this pointer, which is stored as the last argument on the stack, as if he would be the first to pass to the function parameters.

The Microsoft Visual C compiler of this pointer is passed in the ECX register and the called function cleans up the stack, so it will proceed as with the stdcall calling convention. However, variable argument lists are used after adjusting the calling function the stack (ie as cdecl ).

The thiscall calling convention can explicitly only with Microsoft Visual C 2005 and later versions, and allows out calling member functions from native code when classes by default use the clrcall calling convention (managed code).

Summary table calling conventions

87803
de