Some knowledge about call conventions (cdecl, fastcall, stcall, and thiscall)

Source: Internet
Author: User
Tags microsoft c
When a high-level language function is compiled into a machine code, there is a problem that must be solved: Because the CPU cannot know how many and what parameters a function call requires. That is, the computer does not know how to pass parameters to this function. The operation of passing parameters must be coordinated by the function caller and the function itself. Therefore, the computer provides a data structure called Stack to support parameter transmission.

When a function is called, the caller pushes the parameter to the stack in sequence and then calls the function. After the function is called, the caller obtains data in the stack for calculation. After function compute is completed, the caller or the function modifies the stack to restore the original stack. Two important issues in parameter passing 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 original stack after the function is called.

3) Where is the function return value?

In advanced languages, calling conventions are used to illustrate these two problems. Common call specifications include:

            stdcall

cdecl

fastcall

thiscall

naked call
Stdcall call Specification

Stdcall is often referred to as Pascal's call specification, because Pascal is a very common language used for teaching computer programming. Its syntax is rigorous, and the function call Convention used is stdcall. In Microsoft C ++ C/C ++ compilers, Pascal macro is often used to declare this call Convention. Similar macros include winapi and callback.

The syntax of the stdcall call specification declaration is:


int __stdcall function(int a,int b)
The call conventions of stdcall mean:
1) The parameter is pushed from right to left into the stack;
2) The function modifies the stack;
3) The function name is automatically followed by a leading underline followed by a @ symbol, followed by the parameter size.
The preceding function is used as an example. parameter B is first pushed to the stack, and parameter A is followed. The function call function (1, 2) is translated into an assembly language:
Push 2 second parameter into Stack
Push 1 first parameter into Stack
Call function call parameters. Note that CS: EIP is automatically added to the stack.
For the function itself, you can translate it:
Push EBP to save the EBP register, which will be used to save the stack top pointer of the stack and can be restored when the function exits.
MoV EBP, esp save Stack pointer
MoV eax, [EBP + 8 h] EBP in the stack is stored in order with EBP, CS: EIP, A, B, EBP + 8 pointing to
Add eax, [EBP + 0ch] B is saved at EBP + 12 in the stack
MoV ESP and EBP resume ESP
Pop EBP
RET 8

During compilation, the function name is translated into _ FUNCTION @ 8.

Note that different compilers insert their own compilation code to provide compilation versatility, but the general code is as follows. The ESP is reserved to EBP at the beginning of the function, and the restoration at the end of the function is a common method used by the compiler.

From the perspective of function calls, 2 and 1 are pushed into the stack sequentially, and in the function, they access parameters through the offset relative to EBP (that is, the stack pointer when the function is just introduced. After the function is completed, RET 8 indicates that the 8-byte stack is cleared and the function restores the stack.

Cdecl call Specification

The cdecl call Convention, also known as the C call Convention, is the default call Convention of C language. Its definition syntax is:

Int function (int A, int B) // The C call convention is not modified.
Int _ cdecl function (int A, int B) // specify C call conventions

The parameter pressure stack sequence in cdecl call conventions is the same as that in stdcall. parameters are first pushed to the stack from the left. The difference is that the function itself does not clean up the stack, and the caller is responsible for clearing the stack. Due to this change, the number of parameters allowed by the C call convention is not fixed, which is also a major feature of C language. For the previous function, the assembly code after cdecl is changed:

Call Process
Push 1
Push 2
Call Function
Add ESP, 8 Note: here the caller is restoring the stack
Called function _ Function
Push EBP to save the EBP register, which will be used to save the stack top pointer of the stack and can be restored when the function exits.
MoV EBP, esp save Stack pointer
MoV eax, [EBP + 8 h] EBP in the stack is stored in order with EBP, CS: EIP, A, B, EBP + 8 pointing to
Add eax, [EBP + 0ch] B is saved at EBP + 12 in the stack
MoV ESP and EBP resume ESP
Pop EBP
RET note: the stack is not modified here

According to msdn, this modifier is automatically prefixed with a leading underline before the function name, so the function name is recorded as _ function in the symbol table.

Because the parameters are pressed from the right to the left, the initial parameter is at the position closest to the top of the stack. Therefore, when an indefinite number of parameters are used, the position of the first parameter in the stack must be known. As long as the number of parameters is determined based on the explicit parameters of the first and later, the parameter can be used, for example, the sprintf function is defined:

 


int sprintf(char* buffer,constchar* format,...)
Since all the indefinite parameters can be determined through format, it is no problem to use the indefinite number of parameters. The fastcall call specification fastcall call Convention is similar to stdcall, which means:
1) The first and second DWORD parameters (or smaller ones) of the function are passed through ECx and EDX, and other parameters are pushed to the stack from the right to the left;
2) The called function clears the stack;
3) The function name modification rule is the same as stdcall.
The statement syntax is: int _ fastcall function (int A, int B) thiscall call specification.

Thiscall is the only function modifier that cannot be explicitly specified, because thiscall is not a keyword. It is the default call Convention for C ++ class member functions. Because the member function call has a this pointer, special processing is required. thiscall means:

1) The parameters are pushed to the stack from right to left;
2) 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 parameter pressure stacks;
3) if the number of parameters is not fixed, the caller clears the stack. Otherwise, the function cleans the stack by itself.
To illustrate this call Convention, define the following classes and use code:

class A
{
public:
int function1(int a,int b);
int function2(int a,...);
};
int A::function1 (int a,int b)
{
return a+b;
}
int A::function2(int a,...)
{
va_list ap;
va_start(ap,a);
int i;
int result = 0;
for(i = 0 ; i < a ; i ++)
{
result += va_arg(ap,int);
}
return result;
}
void callee()
{
A a;
a.function1(1,2);
a.function2(3,1,2,3);
}
After the callee function is translated into an assembly, it becomes:
// Function function1 call
0401c1d push 2
00401c1f Push 1
00401c21 Lea ECx, [ebp-8]
00401c24 call function1 note that this is not included in the stack
// Function function2 call
00401c29 Push 3
00401c2b push 2
00401c2d Push 1
00401c2f Push 3
00401c31 Lea eax, [ebp-8] Here introducing this pointer
00401c34 push eax
00401c35 call function2
00401c3a add ESP, 14 h

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 the cdecl naked call specification. This is a rare call Convention. Generally, it is recommended that you do not use it. The compiler does not add initialization and cleanup code for such functions. More specifically, you cannot use return to return values. Instead, you can only use insert assembly to return results. This is generally used for real-Mode Driver Design. Suppose we define a sum addition program, which can be defined:
 __declspec(naked) int  add(int a,int b)
   {
       __asm mov eax,a
       __asm add eax,b
       __asm ret
   }
Note: This function does not return an explicit return value. The returned result is implemented by modifying the eax register, and the RET commands that exit the function must be explicitly inserted. The code above is translated into an assembly and then changed:
   mov    eax,[ebp+8]
   add eax,[ebp+12]
   ret 8
Note that this modifier is used in combination with _ stdcall and cdecl. The code used in combination with cdecl is mentioned earlier. For the code combined with stdcall, it becomes:
_ Declspec (naked) int _ stdcall function (int A, int B)
{
_ ASM mov eax,
_ ASM add eax, B
_ Asm ret 8 // pay attention to the following 8
}
This function is called in the same way as the common cdecl and stdcall call functions. Common problems caused by function call conventions if the defined conventions are inconsistent with those used, the stack will be damaged, resulting in serious problems. The following are two common problems:
1) The function prototype declaration is inconsistent with the function body definition.
2) different function conventions are defined when DLL is used to import functions.

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.