Calling convention

From Freepedia

This article needs to be cleaned up to conform to a higher standard of quality.
This article has been tagged since October 2005.
See How to Edit and Style and How-to for help, or this article's talk page.


A calling convention is a method for a programming language to send data to a function, and receive data back from functions. When writing a piece of software in multiple languages and modules, it is necessary for all modules to use compatible calling conventions.

Calling conventions differ in the method for sending data to a function, receiving return data from a function, and methods of Name mangling.

Contents

cdecl

The cdecl calling convention is used by many C and C++ systems for the x86 architecture. In cdecl, function parameters are passed on the stack in a right-to-left order. Function return values are returned in the EAX register. Registers EAX, ECX, and EDX are available for use in the function. The calling function cleans the stack, so functions may utilize a variable-length argument list (printf is the canonical example of a function with a variable argument list).

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

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

will produce the following x86 Assembly code*:

push c
push b
push a
call function
add esp, 12
mov x, eax

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

As an example, the following function call with a variable argument list:

#include <stdarg.h> //for variable argument lists
int function(int, ...);
int a, b, c, x;
...
x = function(a, b, c);

will produce the same exact x86 Assembly code*:

push c
push b
push a
call function
add esp, 12
mov x, eax

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(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.

*assembly code written with MASM syntax

Pascal

The Pascal calling convention is the reverse of the C calling convention. The parameters are pushed on the stack in left-to-right order and the callee is responsible for balancing the stack before return.

Register (FastCall)

The Register or FastCall calling convention is compiler-specific; for specific information, consult the compiler's documentation. In general, however, the register calling convention states that the first 2 or 3 function arguments with a size of 32 bits or lower will be passed in the EAX, EDX, and possibly ECX registers instead of on the stack. The remaining arguments are passed right-to-left on the stack similar to cdecl. Return values are passed in the AL, AX, or EAX register.

stdcall

The stdcall calling convention is the de facto standard calling convention for Microsoft Windows NT programming API. Function parameters are passed Right-to-Left. Registers EAX, ECX, and EDX are preserved for use within the function. Return values are stored in the EAX register. Unlike cdecl, the called function cleans the stack, instead of the calling function. Because of this fact, stdcall functions cannot support variable-length argument lists.

On a Microsoft Windows system, a function may be declared to be stdcall using the following syntax in the function prototype, and in the function declaration:

void _stdcall function(params);

Stdcall functions are easy to recognize in ASM code because those functions will all 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 14

safecall

thiscall

This calling convention is the default calling convention for C++ member functions which do not use variable arguments. Arguments are passed on the stack from right to left, and the this pointer is passed in the ECX register. The callee cleans the stack and returns the results in EAX. As stack balancing is done by the callee, vararg functions (functions with a variable length of arguments) must use cdecl placing the this pointer on the stack last. The thiscall calling convention cannot be explicitly specified as thiscall is not a keyword.

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.

Standard Exit and Entry Sequences

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
                   ;which is where the parameters are
    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 location of the function parameters on the stack, and 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 behavior 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 variables
    pop ebp        ;restore the original base pointer
    ret            ;return from the function

The following C function:

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

would produce the equivalent asm code:

    ;entry sequence
    push ebp
    mov ebp, esp
    sub esp, 4     ;create space for "int k"

    ;function code
    mov eax, [ebp + 8] 
                   ;move parameter i to accumulator
    add eax, [ebp - 4]
                   ;add k to i
                   ;answer is returned in eax

    ;exit sequence
    mov esp, ebp
    pop ebp
    ret

See also

External links



Views
Personal tools
Similar Links