When calling a function, the computer uses a stack to store the arguments passed to the function.
Stack is an advanced data structure, the stack has a storage area, a stack top pointer. The top of the stack pointer points to the first available data item (called the top of the stack) on the stack. Users can add data in the stack on the top of the stack, this operation is called the pressure stack (push), after the stack, the top of the stack automatically become the new data entry position, the top of the stack pointer also changes. Users can also take the stack from the top of the stack, known as pop-up stack (POP), pop-up stack, the top of the stack under the top of the stack, the top of the stack pointer changes. function calls, the caller sequentially presses the parameter stack, then calls the function, the function is called, obtains the data in the stack, and calculates. After the function evaluates, either the caller, or the function itself, modifies the stack to restore the stack to its original.
There are two important issues that need to be clearly stated in parameter passing:
1. When the number of parameters is more than one, in what order to press the parameters into the stack;
2. After the function call, who will restore the stack to the original state.
In high-level languages, it is through the invocation of functions to illustrate these two problems. Common methods of invocation are:
StdCall
Cdecl
Fastcall
ThisCall
ThisCall
Naked Call
Here are a few ways to invoke each of these calls:
1. stdcall
The StdCall invocation method is also called the Pascal invocation method. In the Microsoft C + + compiler, use Pascal macros, WINAPI macros, and callback macros to specify how the function is called stdcall.
The function declared by the StdCall call method is:
int _stdcall function (int a, int b);
The way the stdcall is called means:
(1) The parameter is pressed from right to left to the stack
(2) by the called function to restore the stack itself
(3) The function name is automatically preceded by a leading underline followed by a @, followed by the size of the parameter
The above function translated into assembly language will become:
Push B presses the second argument first
Push A and press the first parameter
The call function calls functions
At compile time, the name of this function is translated to _function@8
2. cdecl
The Cdecl invocation method, also called the C invocation method, is the default invocation of the C language, and its syntax is:
int function (int a, int b)//No modifier is the C calling method
int _cdecl function (int a, int b)//explicitly specifies how to call in C
The way the cdecl is invoked determines:
(1) The parameters are pressed from right to left to the stack
(2) Recovery stack by caller
(3) The function name automatically adds the leading underline
Because the caller is the one who restores the stack, the C invocation method allows the number of parameters of the function to be fixed, which is a major feature of the C language.
The function of this method is translated as:
Push b//press the second parameter first
Push A//in pressing the first parameter
Call funtion//Calling function
Add ESP, 8//cleanup stack
At compile time, the function of this method is translated into: _function
3. Fastcall
Fastcall It is a quick way of calling, as you can tell by name. The first and second DWORD parameters of the function in this way are passed through ECX and edx,
The following parameters are pushed from right to left to the stack.
The called function cleans up the stack.
The function name is to fix a rule with stdcall
Its declaration syntax is:
int fastcall function (int a, int b);
4. ThisCall
The ThisCall invocation is the only way that the specified modifier cannot be displayed. It is the default invocation method for C + + class member functions. Because the member function call also has a this pointer, this special invocation must be used.
The way the thiscall is called means:
The parameters are pushed from right to left to the stack.
If the number of parameters is determined, the this pointer is passed to the callee via ECX, and if the number of parameters is indeterminate, the this pointer is pushed into the stack after all the arguments are pushed into the stack.
The number of arguments is not variable, the caller cleans up the stack, or the function itself cleans up the stack.
As you can see, for a fixed number of parameters, it is similar to stdcall, and is similar to cdecl.
5. Naked Call
is a relatively rare calling method, which is uncommon in general advanced programming languages.
The declaration invocation of a function must be the same as the actual invocation method, and the compiler will inevitably create confusion.
Function name Modification rule:
1. C Compile-time Function name Modification Convention rules:
The __stdcall calling convention adds an underscore prefix to the output function name followed by an "@" symbol and the number of bytes of its arguments, formatted as _function@8.
The __cdecl calling convention only adds an underscore prefix to the output function name, in the form of _function.
The __fastcall calling convention adds an "@" symbol to the output function name followed by an "@" symbol and the number of bytes of its arguments, in the form of @function@8.
They do not change the character case in the output function name, which is different from the Pascal calling convention, and the function name of Pascal convention output is not decorated and all uppercase.
2. C + + compile-time Function name Modification Convention rules:
__stdcall calling convention:
(1) to "?" Identifies the start of the function name, followed by the number of functions;
(2) The function name is followed by "@ @YG" to identify the start of the parameter table followed by the parameter table;
(3) The parameter table is indicated by the code name:
X--void,
D--char,
e--unsigned Char,
F--short,
H--int,
i--unsigned int,
J--long,
K--unsigned Long,
M--float,
N--double,
_n--bool,
....
pa--represents a pointer, followed by a code indicating the pointer type, if the same type of pointer appears continuously, substituting "0" for a "0" generation
Table repeats once;
(4) The first item of the parameter table is the return value type of the function, followed by the data type of the parameter, and the pointer is identified before the data type it refers to;
(5) The end of the entire name is identified with "@z" after the parameter table, and if the function has no arguments, the "Z" identity ends.
The format is "? functionname@ @YG *****@z" or "? functionname@ @YG *xz", for example
int Test1 (char *var1,unsigned long)-----"? test1@ @YGHPADK @z "
void Test2 ()-----"? test2@ @YGXXZ "
__cdecl calling convention:
The rule is the same as the _stdcall calling convention above, except that the start identification of the parameter table changes from "@ @YG" above to "@ @YA".
__fastcall calling convention:
The rule is the same as the _stdcall calling convention above, except that the start identification of the parameter table changes from "@ @YG" above to "@ @YI".
VC + + On the function of the lack of declaration is "__CEDCL", will only be called by C + +.