Overview:
In the call conventions, the main content involved is whether the caller maintains the stack balance for the function call stack in different call conventions, whether the caller maintains the stack balance by himself, and whether the incoming parameter direction is from right to left or from left to right. PASS Parameters through stack or through registers, and modify function names according to different call conventions. In general, this is almost the case.
----------------------------------------------------------------------
The caller restores the stack balance by himself and is used for Variable Parameter functions. The call convention is _ cdecl, and the Assembly Code is represented.
Push Parameters
Call Function
Add ESP, 4
The called user restores stack balance by himself _ stdcall
Push Parameters
Call Function
Use register to pass parameter _ fastcall
MoV edX, parameter
MoV ECx, parameter
Call Function
The above call conventions correspond to the compiler options/GZ (stdcall),/GR (fastcall),/GD (cdecl)
Note: The keyword _ cdcel can be searched in the msdn help document.
----------------------------------------------------------------------
The following describes how to use the C function name in different call conventions:
The modification form of the C function depends on the call conventions used in its Declaration, as shown below.
Call conventions
_ Cdecl (default) Leading underline (_)
_ Stdcall: The leading underline (_) and the ending at sign (@), followed by a number indicating the number of bytes in the parameter list
_ Fastcall is the same as _ stdcall, but the prefix is not an underscore but an @ character.
Instance analysis: (verify vs2010 by yourself)
Function prototype: Modified Name:
Int _ fastcall fun3 (int A, int B); @ fun3 @ 8
Int _ stdcall fun2 (int A, int B); _ fun2 @ 8
Int _ cdecl fun1 (int A, int B); _ fun1
------------------------ C ++ modifier name format ----------------------------------------------
The modifier name of the C ++ function contains the following information:
Function name.
The class to which the function belongs (if the function is a member function ). This may include classes that encapsulate functions.
The namespace to which the function belongs (if the function is an integral part of a namespace ).
The parameter type of the function.
Call conventions.
The return type of the function.
Function names and class names exist in the form of code in the modifier name. The rest of the modifier name is code that has internal significance only for the compiler and the linker. The following is an example of undecorated and decorated C ++ names.
Name not modified
Int A (char) {int I = 3; return I ;};? A @ yahd @ Z
Void _ stdcall B: C (float ){};? C @ B @ aagxm @ Z
------------ Argument passing and naming conventions ---------------------------------
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. structures that are not pods will not be returned in registers.
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.
Note:
When 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 cleanupparameter passing
_ Cdecl callerpushes parameters on the stack, in reverse order (Right to left)
_ Clrcall N/
Load parameters onto CLR expression stack in order (left to right ).
_ Stdcall calleepushes parameters on the stack, in reverse order (Right to left)
_ Fastcall calleestored in registers, then pushed on Stack
_ Thiscall calleepushed on stack; this pointer stored in ECx
The last note is the descriptions of the call conventions under IPF and x64 processors. The following is a reference from the original words in the msdn help manual:
On itanium Processor family (IPF) and x64 processors, _ cdecl is accepted and ignored by the compiler; on IPF, by convention, parameters are passed in register.
On itanium Processor family (IPF) and x64 processors, _ stdcall is accepted and ignored by the compiler; on IPF, by convention, parameters are passed in register.
On itanium Processor family (IPF) and amd64 machines,_ FastcallIs accepted and ignored by the compiler; on an IPF chip, by convention, parameters are passed in register.
In this case, register parameters are directly used for the two processing methods.