From: http://blog.csdn.net/JFDream_15/archive/2007/08/21/1752930.aspx
In C, suppose we have a function like this:
Int function (int A, int B)
You can use this function by using result = function. However, when advanced
When a language is compiled into a machine code that can be recognized by a computer, a problem arises: In the CPU, the computer cannot know how many and what parameters a function call requires, no hardware can save these parameters. That is to say, the computer does not know how to pass parameters to this function. The operation of passing parameters must be coordinated by the function caller and the function itself. Therefore, the computer provides a data structure called Stack to support parameter transmission.
Stack is an advanced post-release data structure with a storage zone and a stack top pointer. Stack top pointer pointing to stack
The first available data item (called the top stack ). You can add data to the stack at the top of the stack.
It is called the push. After the stack is pressed, the top of the stack is automatically changed to the position where new data items are added, and the top pointer of the stack is also repaired.
Change. You can also remove the top stack from the stack, which is called pop. After the stack pops up, an element at the top of the stack changes.
The stack top pointer is modified accordingly.
When a function is called, the caller pushes the parameter to the stack in sequence and then calls the function. After the function is called, the caller obtains
And compute the data. After function compute is completed, the stack can be modified by the caller or the function itself to make the stack
Restore original.
Two important issues in parameter passing must be clarified:
When there are more than one parameter, in what order will the parameter be pushed into the stack?
Who will restore the original stack after the function is called?
In advanced languages, the two problems are described through the function call convention. Common call conventions include:
Stdcall
Cdecl
Fastcall
Thiscall
Naked call
Stdcall call conventions
Stdcall is often referred to as Pascal's call convention, because Pascal is a very common programming language for teaching in the early days, and its syntax is rigorous. The function call Convention used is stdcall. In Microsoft C ++ C/C ++ compilers, Pascal macro is often used to declare this call Convention. Similar macros include winapi and callback.
The syntax of the stdcall call Convention Declaration is (the previous function is used as an example ):
Int __ StdcallFunction (int A, int B)
The call Convention of stdcall means: 1) the parameter is pushed from right to left into the stack, 2) the function modifies the stack. 3) the function
The name is automatically followed by a leading underline, followed by a @ symbol, followed by the parameter size
The preceding function is used as an example. parameter B is first pushed to the stack, and parameter A is followed by function call (1, 2 ).
The translation into assembly language will become:
Push 2 second parameter into Stack
Push 1 first parameter into Stack
Call function call parameters. Note that CS: EIP is automatically added to the stack.
For the function itself, you can translate it:
Push EBP to save the EBP register, which will be used to save the stack top pointer of the stack. you can exit the Function
When mov EBP is restored, esp saves the stack pointer mov eax, and EBP, CS: EIP, A, B, EBP + 8 points to
Add eax, [EBP + 0ch] B is saved at EBP + 12 in the stack
MoV ESP and EBP resume ESP
Pop EBP
RET 8
During compilation, the function name is translated into _ FUNCTION @ 8.
Note that different compilers insert their own compilation code to provide compilation versatility, but the general code is as follows. The ESP is reserved to EBP at the beginning of the function, and the restoration at the end of the function is a common method used by the compiler.
From the perspective of function calls, 2 and 1 are pushed into the stack sequentially, and in the function, they access parameters through the offset relative to EBP (that is, the stack pointer when the function is just introduced. After the function is completed, RET 8 indicates that the 8-byte stack is cleared and the function restores the stack.
Cdecl call conventions
The cdecl call Convention, also known as the C call Convention, is the default call Convention of C language. Its definition syntax is:
Int function (int A, int B) // The C call convention is not modified.
Int _ cdecl function (int A, int B) // specify C call conventions
When I wrote this article, I was surprised to find that the parameter pressure stack sequence set by cdecl is the same as that set by stdcall.
The parameter is first pushed to the stack from the left. The difference is that the function itself does not clean up the stack, and the caller is responsible for clearing the heap.
Stack. Due to this change, the number of parameters allowed by the C call convention is not fixed, which is also a major part of the C language.
Features. For the previous function, the assembly code after cdecl is changed:
Call Process
Push 1
Push 2
Call Function
Add ESP, 8 Note: here the caller is restoring the stack
Called function _ Function
Push EBP to save the EBP register, which will be used to save the stack top pointer of the stack. you can exit the Function
Recovery
MoV EBP, esp save Stack pointer
MoV eax, [EBP + 8 h] EBP points to the position in the stack, which is stored in sequence with EBP, CS: EIP, A, B, EBP + 8 points
A
Add eax, [EBP + 0ch] B is saved at EBP + 12 in the stack
MoV ESP and EBP resume ESP
Pop EBP
RET note: the stack is not modified here
According to msdn, this modifier is automatically prefixed with a leading underline before the function name, so the function name is recorded as _ F in the symbol table.
Unction, but I didn't seem to see this change during compilation.
Because the parameters are pressed from right to left, the initial parameters are at the position closest to the top of the stack.
When the number of parameters is not specified, the position of the first parameter in the stack must be known.
If the first and later parameters are determined, you can use an indefinite parameter, for example, the sprin
TF function, defined:
Int sprintf (char * buffer, const char * format ,...)
Since all the indefinite parameters can be determined through format, it is no problem to use the indefinite number of parameters.
Fastcall
The fastcall call Convention is similar to stdcall, which means:
The first and second DWORD parameters (or smaller ones) of the function are passed through ECx and EDX, and other parameters are passed through
Stack pressure from right to left
Call function to clear the stack
Function Name modification rules are the same as stdcall
Statement Syntax: int fastcall function (int A, int B)
Thiscall
Thiscall is the only function modifier that cannot be explicitly specified, because thiscall is not a keyword. It is a C ++ class
The default call Convention of member functions. Since the member function call has another this pointer, it must be specially processed.
Iscall means:
Parameter from right to left into Stack
If the number of parameters is determined, this pointer is passed to the caller through ECx; if the number of parameters is not determined, this pointer
All parameter stacks are pushed into the stack.
If the number of parameters is not fixed, the caller clears the stack. Otherwise, the function clears the stack by itself.
To illustrate this call Convention, define the following classes and use code:
Class
{
Public:
Int function1 (int A, int B );
Int function2 (int ,...);
};
Int A: function1 (int A, int B)
{
Return A + B;
}
# I nclude
Int A: function2 (int ,...)
{
Va_list AP;
Va_start (AP, );
Int I;
Int result = 0;
For (I = 0 I <a I ++)
{
Result + = va_arg (AP, INT );
}
Return result;
}
Void callee ()
{
A;
A. function1 (1, 2 );
A. function2 (3,1, 2,3 );
}
After the callee function is translated into an assembly, it becomes:
// Function function1 call
0401c1d push 2
00401c1f Push 1
00401c21 Lea ECx, [ebp-8]
00401c24 call function1 note that this is not included in the stack
// Function function2 call
00401c29 Push 3
00401c2b push 2
00401c2d Push 1
00401c2f Push 3
00401c31 Lea eax, [ebp-8] Here introducing this pointer
00401c34 push eax
00401c35 call function2
00401c3a add ESP, 14 h
It can be seen that, when the number of parameters is fixed, it is similar to stdcall. If it is not scheduled, it is similar to cdecl.
Naked call
This is a rare call Convention. Generally, it is recommended that you do not use it. The compiler does not add such a function.
Add initialization and cleanup code. More specifically, you cannot use return to return values. You can only use insert assembly to return values.
Result. This is generally used for real-Mode Driver Design. Suppose we define a sum addition program, which can be defined:
_ Declspec (naked) int add (int A, int B)
{
_ ASM mov eax,
_ ASM add eax, B
_ ASM RET
}
Note that this function does not return an explicit return value. The returned result is implemented by modifying the eax register and the exit function
The RET command must be explicitly inserted. The code above is translated into an assembly and then changed:
MoV eax, [EBP + 8]
Add eax, [EBP + 12]
RET 8
Note that this modifier is __ StdcallAnd cdecl used in combination, the previous is the code used in combination with cdecl
The code combined with stdcall becomes:
_ Declspec (naked) int __ StdcallFunction (int A, int B)
{
_ ASM mov eax,
_ ASM add eax, B
_ Asm ret 8 // pay attention to the following 8
}
This function is called in the same way as the common cdecl and stdcall call functions.
Common problems caused by function call conventions
If the defined and used conventions are inconsistent, the stack will be damaged, leading to serious problems. The following two
Common problems:
The function prototype declaration and function body definition are inconsistent.
Different function conventions are defined during DLL Function Import.
For example, if we declare a function in dll:
_ Declspec (dllexport) int func (int A, int B); // note that stdcall is not available here, which is
Cdecl
The time code used is:
Typedef int (* winapi dllfunc) func (int A, int B );
Hlib = loadlibrary (...);
Dllfunc func = (dllfunc) getprocaddress (...) // The call conventions are modified here
Result = func (); // cause Error
This modifier is added because the caller does not understand the meaning of winapi, and the above Code will inevitably cause the stack to be broken.
Bad. The checkesp function inserted during MFC compilation will tell you that the stack is broken.
Description on msdn: make a mark http://msdn.microsoft.com/en-us/library/984x0h58 (V = vs.71). aspx
Argument passing and naming conventions
Visual Studio. NET 2003Other Versions
- Visual Studio 2010
- Visual Studio 2008
- Visual Studio 2005
Microsoft specific
All arguments are widened to 32 bits when they are passed. return values are also widened to 32 bits and returned in the eax register, counter t for 8-byte structures, which are returned in the edX: eax register pair. larger structures are returned in the eax register as pointers to hidden return structures. parameters are pushed onto the stack from right to left.
The compiler generates PROLOG and epilog code to save and restore the ESI, EDI, EBX, and EBP registers, if they are used in the function.
NoteWhen a struct, union, or class is returned from a function by value, all definitions of the type need to be the same, else the program may fail at runtime.
For information on how to define your own function PROLOG and epilog code, see naked function CILS.
The following calling conventions are supported by the visual C/C ++ compiler.
Keyword |
Stack cleanup |
Parameter passing |
_ Cdecl |
Caller |
Pushes parameters on the stack, in reverse order (Right to left) |
_ Stdcall |
Callee |
Pushes parameters on the stack, in reverse order (Right to left) |
_ Fastcall |
Callee |
Stored in registers, then pushed on Stack |
Thiscall (Not a keyword) |
Callee |
Pushed on stack;ThisPointer stored in ECx |
For related information, see obsolete calling conventions.
End Microsoft specific
See also
Calling conventions