C/C ++ function call Methods

Source: Internet
Author: User

We know that when calling a function, the computer often uses stacks to store the parameters required for function execution. Because the stack space is limited, the stack in windows is a data structure extended to a low address, is a continuous memory area. This statement indicates that the stack top address and the maximum stack capacity are pre-defined by the system. The stack size in windows is 2 MB (OR 1 MB ), if the requested space exceeds the remaining space of the stack, overflow is displayed.
When calling a function, the first entry to the stack is the address of the next instruction in the main function (the next executable statement of the function call statement), and then the parameters of the function, in most C compilers, parameters are written from right to left into the stack, followed by local variables in the function. Note that static variables are not included in the stack.
Two important issues in parameter transmission must be clarified:
1. When there are more than one parameter, in what order will the parameter be pushed into the stack;
2. Who will restore the stack to its original state after the function is called.
In advanced languages, the two problems are described through the function call method. Common call methods include:
Stdcall
Cdecl
Fastcall
Thiscall
Thiscall
Naked call
The following describes the call methods respectively:
1. stdcall
The stdcall call method is also called the Pascal call method. In Microsoft C ++ C/C ++ compilers, PASCAL macro, WINAPI macro, and CALLBACK macro are used to specify the function call method as stdcall.
The function declaration of the stdcall call method is:
Int _ stdcall function (int a, int B );
The call method of stdcall means:
(1) The parameters are pushed to the stack from right to left.
(2) The called function restores the stack by itself.
(3) The function name is automatically followed by a leading underline followed by a @, followed by the parameter size
The above function translated into an assembly language will become:
Push B to the second parameter first
Push a and then press the first parameter.
Call function
During compilation, the function name is translated as _ function @ 8.
2. cdecl
The cdecl call method is also called the C call method, which is the default call method of C language. Its syntax is:
Int function (int a, int B) // The C call method is used without modifiers.
Int _ cdecl function (int a, int B) // specify the C call Method
The cdecl call method determines the following:
(1) The parameters are pushed to the stack from right to left.
(2) stack restoration by the caller
(3) automatically add a leading underline to the function name
Since the stack is restored by the caller, the number of parameters allowed for the function in the C call method is not fixed, which is a major feature of C language.
Functions in this mode are translated:
Push B // press the second parameter first
Push a // press the first parameter
Call funtion // call a function
Add esp, 8 // clear the stack ..... You need to be familiar with the esp register function. It is recommended that you take a look at the compilation-related books.
During compilation, this function is translated into: _ function
3. fastcall
Fastcall can be understood by name. It is a fast call method. The first and second DWORD parameters of this function are passed through ecx and edx,
The following parameters are pushed to the stack from right to left.
The called function clears the stack.
Function Name repair rules are the same as stdcall
Its statement syntax is:
Int fastcall function (int a, int B );
4. thiscall
The thiscall method is the only method that cannot display the specified modifier. It is the default call Method for c ++ class member functions. Since the member function call also has a this pointer, this special call method must be used.
The thiscall method means:
The parameter is pushed from right to left into the 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 is pushed to the stack after all parameters are pushed to the stack.
If the number of parameters is not fixed, the caller clears the stack. Otherwise, the function clears the stack by itself.
As you can see, when the number of parameters is fixed, it is similar to stdcall, and sometimes it is similar to cdecl.
5. naked call
It is a rare call method, which is not common in advanced programming languages.
The declared call method of the function must be consistent with the actual call method, and the compiler will inevitably produce confusion.
Function Name modification rules:
1. Rules for modifying function names during C Compilation:
The _ stdcall call Convention adds an underline prefix before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is _ function @ 8.
The _ cdecl call Convention only adds an underline prefix before the output function name, in the format of _ function.
_ Fastcall: add the "@" symbol before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is @ function @ 8.
They do not change the case sensitivity of the output function name. This is different from PASCAL's call conventions. PASCAL's output function names are not modified and all are capitalized.
2. Specification rules for function name modification during C ++ Compilation:
_ Stdcall call conventions:
(1) Take "?" Start of the function name, followed by the function name;
(2) The function name starts with "@ YG" to identify the parameter table, followed by the parameter table;
(3) The parameter table is represented 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 -- indicates the pointer, and the code behind it indicates the pointer type. If a pointer of the same type appears consecutively, it is replaced by "0", which is a "0" generation.
The table is repeated once;
(4) the first item of the parameter table is the type of the return value of the function, followed by the Data Type of the parameter, and the pointer ID is before the data type referred to by the function;
(5) "@ Z" indicates the end of the entire name after the parameter table. If this function does not have a parameter, it ends with "Z.
The format is "? Functionname @ YG ***** @ Z "or"? Functionname @ YG * XZ ", for example
Int Test1 (char * var1, unsigned long) ----- "? Test1 @ YGHPADK @ Z"
Void Test2 () ----- "? Test2 @ YGXXZ"
_ Cdecl:
The rules are the same as the _ stdcall call Convention above, except that the start mark of the parameter table is changed from "@ YG" to "@ YA ".
_ Fastcall:
The rules are the same as the _ stdcall call Convention above, but the start mark of the parameter table is changed from "@ YG" to "@ YI ".
The "_ cedcl" Declaration for the function can only be called by C/C ++.

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.