Visual c ++ compiler options can specify three types of calling convention:
/GD/GR/GZ
These three parameters determine:
1. Order in which function parameters are written to the stack, right to left or left to right.
2. After the function is run, it indicates whether to call the function or clear the parameters of the called function into the stack.
3. How is the function name converted during compilation.
The following describes in detail:
1./GD
This is the default conversion mode of the compiler. For general functions, use the C function call conversion method _ cdecl,
Except for C ++ member functions and functions with _ stdcall _ fastcall modified above.
2./GR
For general functions that use the _ fastcall function to call the conversion method, all functions that use the _ fastcall Function
You must have a function prototype. Except for C ++ member functions and functions with _ cdecl _ stdcall modified above.
3./GZ
For all c Functions, use the _ stdcall function to call the conversion method.
And functions modified with _ cdecl _ fastcall and C ++ member functions. All use _ stdcall
The modified function must have the original function.
In fact, for x86 systems, the calling method of C ++ member functions is a bit special.
The pointer is placed in ECx, and all function parameters go from right to left into the stack. The called member function is responsible for clearing
Parameters. For Variable Parameter member functions, always use the _ cdecl conversion method.
The following topic describes the differences between the three function call conversion methods:
1. _ cdecl
This is the default function call conversion method of the compiler. It can process Variable Parameter Function calls. Parameters
From right to left. After the function is executed, the called function is responsible for clearing the parameters in the stack.
During compilation, each function is preceded by an underscore (_) without case-sensitivity conversion. That is
_ Functionname
2. _ fastcall
Some function call parameters are placed in ECx and EDX, while other parameters are pushed from right to left. Called
The function is responsible for clearing the parameters in the stack when it is going to return. Pay attention to the embedded assembly language.
Register usage to avoid conflicts with compiler usage. Function Name conversion:
@ Functionname @ number
No case-insensitive function name conversion. number indicates the number of bytes of the function parameter. Because some parameters are not
Stack is required, so this conversion method will increase the speed of function calling to a certain extent.
3. _ stdcall
Function parameters are pushed from right to left into the stack. The called function cleans up the parameters in the stack. Function Name conversion Lattice
Format:
_ Functionname @ number
Next, we will write a program to see the differences between different calls after compilation.
Use the following functions:
Int function (int A, int B)
{
Return A + B;
}
Void main ()
{
Function (10, 20 );
}
1. _ cdecl
_ Function
Push EBP
MoV EBP, ESP
MoV eax, [EBP + 8]; parameter 1
Add eax, [EBP + C]; add parameter 2
Pop EBP
Retn
_ Main
Push EBP
MoV EBP, ESP
Push 14 h; parameter 2 into Stack
Push 0ah; parameter 1 into Stack
Call _ function; call a function
Add ESP, 8; Modify Stack
XOR eax, eax
Pop EBP
Retn
2. _ fastcall
@ Function @ 8
Push EBP
MoV EBP, esp; Save the stack pointer
Sub ESP, 8; two local variables are added
MoV [ebp-8], EDX; save parameter 2
MoV [ebp-4], ECx; save parameter 1
MoV eax, [ebp-4]; parameter 1
Add eax, [ebp-8]; add parameter 2
MoV ESP, EBP; corrected Stack
Pop EBP
Retn
_ Main
Push EBP
MoV EBP, ESP
MoV edX, 14 h; parameter 2 to edX
MoV ECx, 0ah; parameter 1 to ECx
Call @ function @ 8; call a function
XOR eax, eax
Pop EBP
Retn
3. _ stdcall
_ FUNCTION @ 8
Push EBP
MoV EBP, ESP
MoV eax, [EBP]; parameter 1
Add eax, [EBP + C]; add parameter 2
Pop EBP
Retn 8; stack repair
_ Main
Push EBP
MoV EBP, ESP
Push 14 h; parameter 2 into Stack
Push 0ah; parameter 1 into Stack
Call _ FUNCTION @ 8; function call
XOR eax, eax
Pop EBP
Retn
The preceding three methods have their own characteristics, and _ main must be _ cdecl. Generally, Win32 functions are
_ Stdcall. Windef. H has the following definitions:
# Define callback _ stdcall
# Define winapi _ stdcall