[Migration post] Call Convention Summary (1)

Source: Internet
Author: User

Calling Convention determines the following:
1) parameter pressure stack sequence (from right to left or from left to right)
2) When a function is returned, the parameters of the called function or the called function are cleared into the stack.
3) function name conversion during compilation

There are a total of five Call conventions, which will be described in detail below.

1. _ stdcall
Parameter stack pressure from right to left
The called function clears the stack parameters before return.
Function Name conversion during C Compilation: _ FUNCTION @ number
Function indicates the function name, and number indicates the number of bytes of the parameter.
Example: int myfunc (int A, int B)
_ Myfucn @ 8
Function Name conversion during C ++ Compilation :? Function @ YG *** @ Z or? Function @ YG * xz
If a function has a parameter, it ends with @ Z. If the function does not have a parameter, it ends with Z.
Function indicates the function name, * indicates the parameter table, which has the following values:
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 -- indicates the pointer. The code behind the pointer indicates the pointer type. If a pointer of the same type appears consecutively, it is replaced by "0". A "0" indicates a repetition.
The first item of the parameter table is the return type, followed by the type of the parameter. The pointer ID is before the Data Type
Example: int myfunc1 (unsigned char * arg1, unsigned long arg2)
? Myfunc1 @ yghpaek @ Z
Void myfunc2 (char * arg1, char * arg2, char * arg3)
? Myfunc2 @ ygxpad00 @ Z
Void myfunc3 ()
? Myfunc3 @ ygxxz
When the C ++ compiler converts a function name, more parameters are taken into account, mainly to facilitate function overloading, while the C language does not have the function overloading problem.

2. _ cdecl
Parameter stack pressure from right to left
Call the function to clear the stack parameters after the function returns
Function Name conversion during C Compilation: _ Function
Function indicates the function name.
Example: int myfunc (int A, int B)
_ Myfucn
Function Name conversion during C ++ Compilation: Same _ stdcall, change YG to ya
Note: For Variable Parameter member functions, always use the _ cdecl conversion method.

3. _ fastcall
Use ECx to pass the first parameter, edX to pass the second parameter, and other parameters are pressed from right to left.
The called function clears the stack parameters before return.
Function Name conversion during C Compilation: @ function @ number
Function indicates the function name, and number indicates the number of bytes of the parameter.
Example: int myfunc (int A, int B)
@ Myfucn @ 8
Function Name conversion during C ++ Compilation: Change YG to Yi

Call conventions can be set in calling convention in project> setting...> C/C ++> code generation. The default status is _ cdecl.

4. thiscall
Thiscall is not a keyword, so it cannot beProgramIt is explicitly specified in, it is the default call Convention of C ++ class member functions. Since a member function call involves a this pointer, special processing is required.
Parameter stack pressure from right to left
If the number of parameters is determined, this pointer is passed to the caller through ECx. If the number of parameters is not fixed, this pointer is pushed to the stack after all parameter stacks are pressed.
If the number of parameters is determined, the called function clears the stack by itself. If the number of parameters is not fixed, the called function clears the stack.
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.

5. Naked call
The compiler generatesCodeTo save the ESI, EDI, EBX, and EBP registers. When exiting the function, the code is generated to restore the contents of these registers. Naked call does not generate such code. More specifically, return results cannot be returned using return, but can only be returned using insert assembly.
The naked call must be used with _ declspec, that is, _ declspec (naked). The Naked call can also be used with other call conventions, such:
_ Declspec (naked) int _ stdcall function (int A, int B)

Usage:
1. _ beginthread requires the thread function address of _ cdecl, and _ stdcall for _ beginthreadex and createthread.
2. The main function must be _ cdecl. Generally, the Win32 function and callback function are both _ stdcall, and a macro is defined to identify them:
# Define callback _ stdcall
# Define winapi _ stdcall
3. if a function is compiled in the C language compiler and used in the C ++ file, because the two compilers do not parse function names, therefore, you must use extern "c" in the C ++ file for declaration. Otherwise, a link error occurs:
# Ifdef _ cplusplus
Extern "C "{
# Endif
Int func (int A, int B );
# Ifdef _ cplusplus
}
# Endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.