Function call convention

xiaoxiao2021-03-06  130

1, _stdcall is the default call mode of the Pascal program. It is usually used in the Win32 API. The function uses the stacking method from the right to left, and the stack is cleared when you exit. The VC compiles the function before adding a downline prefix in front of the function name, plus the number of bytes of "@" and parameters after the function name.

2, c call convention (ie, using __cdecl keyword description) Press the parameter from right to left, and populates the argument by the caller. For the memory stack of the transfer parameters, it is maintained by the caller (for this, the function that implements variable parameters can only use the call convention). In addition, there is also different in terms of function name modification.

_CDECL is the default call mode of the C and C programs. Each function that calls it contains the code that clears the stack, so the resulting executable size is larger than the call _stdcall function. The function uses the stack of right to left. VC compiles the function and adds a downline prefix in front of the function name. It is an MFC default call convention.

3, __ fastCall call agreement is "person", such as its name, its main feature is fast, because it passes the parameter through the register (actually, it uses ECX and EDX to transfer the first two double words (DWORD) or smaller The parameters, the remaining parameters are still transmitted to the left stack, the called function is returned to the internal memory stack of the transfer parameters before returning), and in terms of the function name modification, it is different from the top.

_fastCall mode functions use register pass parameters, and VC compiles the function after the "@" prefix in front of the function name, add the number of bytes of "@" and parameters after the function name.

4. ThisCall is only applied to the "C " member function. The THIS pointer is stored in the CX register, the parameter is left from right to left. Thiscall is not a keyword, so it cannot be designated by the programmer.

5, Naked Call When using 1-4 call conventions, if necessary, the compiler generates code to save the ESI, EDI, EBX, EBP registers, generate code recovery of these registers when entering the function. Naked Call does not produce such a code. Naked Call is not a type modifier, so you must use the _declspec.

Keywords __stdcall, __ cdecl, and __fastcall can be selected before the functionality of the function you want to output, or you can choose the SETTING ... / C / C / Code Generation item of the compilation environment. When the keyword before the output function is not in the compilation environment, the keyword before the output function is added directly before the output function is valid. The corresponding command line parameters are / Gz, / GD, and / GR, respectively. The default state is / gd, ie __cdecl.

To fully imitate the Pascal call, you must first use the __stdcall call convention, as for the function name modification, can be imitated by other methods. There is also a WINAPI macro, Windows.h supports this macro, which can translate the function into an appropriate call agreement, in Win32, it is defined as __stdcall. Use WinAPI macros to create your own APIS.

2) Name modification agreement

1, Decoration Name

"C" or "C " function is identified by a modified name inside (compiled and link). The modisle name is a string generated when the compiler is generated when the compile function is defined or the prototype. Some cases are necessary to use the modifier of the function, such as the output "C " overload function, constructor, the structure, the destructor, and call "C" or "C " function in the assembly code. Wait.

The modified name, class name, call agreement, return type, parameter, etc.

2, the name modification will change with the adjustment agreement and the compilation species (C or C ). The function name modification is different from the compilation species and the call agreement, and below will be explained. A, C compile time function name modification agreement rules:

__stdcall call agreed to add a downline prefix before the output function name, and then a "@" symbol and its parameters of the number of bytes, the format is _functionName @ number.

__cdecl call conventions only add a downline prefix before the output function name, format is _functionName.

__fastcall call agreed to add a "@" symbol before the output function name, the later is also a "@" symbol and its parameters, the format is @ functionName @ number.

They do not change the case sensitive in the output function name, which is different from the PASCAL call, and the function name of the Pascal agreed output without any modification and all uppercase.

B, C compile function name modification agreement rules:

__stdcall call convention:

1. Taking the beginning of the "?" Identification function name, then follow the function name;

2, the function name is behind the "@@ YG" identifies the beginning of the parameter table, and then following the parameter table;

3, the parameter table is indicated by code:

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 the pointer, the subsequent code indicates the pointer type, if the same type of pointer continuously, instead of "0", a "0" represents a repetition;

4, the first item of the parameter table is the return value type of the function, and then the data type of the parameter is subsequently, the pointer identifies before its index data;

5. After the parameter table identifies the end of the entire name, if the function is not parameters, the "z" identifier ends.

The format is "? Functionname @@ yg ***** @@ yg * xz", for example

Int test1 (char * var1, unsigned long) ----- "? test1 @@ yghpadk @ z"

Void Test2 () ----- "? test2 @@ ygxxz"

__cdecl call convention:

The rule is the same as the above _stdcall call, but the start identification of the parameter table is changed from the "@@ yg" above "@@ ya".

__fastcall call convention:

The rule is the above _stdcall call convention, but the beginning identification of the parameter table is changed from the "@@ yg" above "@@ yi".

The province of VC to the function is "__CEDCL", which will only be called by C / C .

CB uses four modified symbols when the output function declaration

// __ cdecl

The default value of the CB, it will be preceded in the output function name and retain this function name unchanged, and the parameter sequentially passes the stack in the order from the right to left, or writes _cDecl and CDECL form.

// __ fastcall

The parameters of her modified function will use the register in touch with the registers, and their function is preceding @, and the parameters are placed in the order from left to right;

// __Pascal It designs the function name that uses the naming agreement in the PASCAL format. At this time, the function is all capitalized. The parameters are placed in order from left to right;

// __ stdcall

Use the standard agreed function name. The function name will not change. When using __stdcall modification. The parameter is placed in the order of right to left, or it can be _stdcall;

转载请注明原文地址:https://www.9cbs.com/read-127156.html

New Post(0)