View call agreements through compilation

Source: Internet
Author: User

When calling a function, there are various call conventions. They specify the parameter transfer method, whether the parameter is variable, and who handles the stack. There are two common call Conventions: C language and Pascal Language.
You can set the call rules for custom functions in the Project Settings, you can also add keywords or API macro definitions (such as _ cdecl, _ stdcall, _ fastcall, WINAPI, and APIENTRY) before function names during function declaration and definition) specify the call protocol of the function.
The following sections describe the usage and significance of various call Protocols respectively, and describe the relevant Assembly code analysis.

1. C language call conventions

When C language is used for programming, the C/C ++ language call Convention is used by default. You can also manually specify it. This requires the _ cdecl keyword to be added to the function declaration. When the local timing is adopted, the number of parameters is variable from right to left. Because the function body cannot know the number of parameters passed in advance, the caller must be responsible for Stack cleaning when using this convention. Because the parameters are variable, this Convention is more flexible, but the performance is relatively low. In the generated code, the letter number has a _ (underline) prefix.
Example:
Int _ cdecl Add (int a, int B)
{
Return (a + B );
}

Function call:
Add (1, 2 );

Push 2
Push 1
Call @ Add; in fact, the expression used by the compiler to locate the function is omitted here.
Add esp, 8; clear Stack

Function body:

Push ebp
Mov ebp, esp
Sub esp, 40 h; the function uses a stack of 40 H (16*4) by default, and adds a variable plus 4 byteswin32 with a stack granularity of 4 bytes.
Push ebx
Push esi
Push EDI

Lea EDI, [ebp-40h]; initializes the stack space for this function to 0 xcccccccc
MoV ECx, 10 h
MoV eax, 0 cccccccch
Rep STOs dword ptr [EDI]

Return (A + B );

MoV eax, dword ptr [EBP + 8]
Add eax, dword ptr [EBP + 0ch]

Pop EDI
Pop ESI
Pop EBX
Mov esp, ebp; if you operate ESP in this function, add esp, 40 h cmp esp, ebp call chkesp, and check whether the pop-up ESP pointer is the same as EBP, if the difference exists, chkesp is called and an exception is thrown.
Pop ebp

Ret

The usage of the function stack is pointed out:

2. Pascal Language call conventions

Most Windows APIs use Pascal Language call conventions. When using C Programming, if you want to use this call convention, you need to add the _ stdcall keyword when declaring the function. The windows. h header file also defines a WINAPI macro, which plays the same role. When the local timing is adopted, the number of parameters is fixed from right to left. Therefore, the function itself can know the number of passed parameters, and you can use a ret n command to directly clean the stack. At the cost of flexibility, performance is improved. In the generated code, A _ (underline) function name is used as the prefix, A @, and the total number of bytes of the parameter (decimal) as the suffix (the number of parameters multiplied by 4 ). Winapi callback apientry pascal is a macro definition of _ stdcall, and _ pascal is an obsolete keyword.
Example:
Int _ stdcall Add (int a, int B)
{
Return (a + B );
}

Function call:
Push 2
Push 1
Call _ Add @ 8; the function name used for connection is used here.

Function body:
; Like _ cdecl, the last line is as follows:
Ret 8; clear Stack

3. This call Convention

It is used for non-static class member functions in C ++. It can only be used by the compiler without corresponding keywords. In the Intel ia32 architecture, this call convention is the same as the PASCAL Language call convention, but an additional parameter-This pointer is transferred through the ECX register. Stack cleanup in functions cannot be used in C, and there is no naming rule for C. Therefore, non-static member functions of classes cannot be used as Callback functions.
Example:
Struct csum
{
Int add (int A, int B)
{
Return (A + B );
}
};
Function call:
Csum sum;
Sum. Add (1, 2 );

Push 2
Push 1
Lea ecx, [ebp-4]; ecx stores this pointer
Call? Add @ CSum @ QAEHHH @ Z; when you repeatedly define a class member function, you can get the name in the output box.

Function body:
Push ebp
Mov ebp, esp
Sub esp, 44 h; A 4 bytes space is used to store this pointer
Push ebx
Push esi
Push EDI
Push ECx
Lea EDI, [ebp-44h]
MoV ECx, 11 h
MoV eax, 0 cccccccch
Rep STOs dword ptr [EDI]
Pop ECx
MoV dword ptr [ebp-4], ECx
 
Return (A + B );

MoV eax, dword ptr [EBP + 8]
Add eax, dword ptr [EBP + 0ch]

Pop EDI
Pop ESI
Pop EBX
MoV ESP, EBP
Pop EBP
RET 8; clear Stack

4. Quick call conventions

This call Convention is used for scenarios with high performance requirements. The keyword is _ fastcall. It requires that parameters be placed in registers to increase the speed. In intel ia32 architecture, this call Convention places the two leftmost parameters of the function smaller than 4 bytes (DWORD size) in the ECX and EDX registers, and other provisions are the same as Pascal's call convention. The generated code contains one @ prefix and one @ with parameters.

The total number of bytes (decimal) is used as the suffix.
Example:
Int _ fastcall add (int A, int B, int C)
{
Return (A + B + C );
}
Function call:
Add (1, 2, 3 );

Push 3
MoV edX, 2
MoV ECx, 1
Call @ add @ 8; the function name used for connection is used here.

Function body:
Push EBP
Mov ebp, esp
Sub esp, 48 h; uses two more variables in [ebp-4], [ebp-8]
Push ebx
Push esi
Push edi
Push ecx
Lea edi, [ebp-48h]
Mov ecx, 12 h
Mov eax, 0 CCCCCCCCh
Rep stos dword ptr [edi]
Pop ecx
Mov dword ptr [ebp-8], edx; or put the content of edx, ecx in
Mov dword ptr [ebp-4], ecx; stack does not seem to increase speed

Return (a + B + c );

Mov eax, dword ptr [ebp-4]
Add eax, dword ptr [ebp-8]
Add eax, dword ptr [ebp + 8]

Pop edi
Pop esi
Pop ebx
Mov esp, ebp
Pop ebp
Ret 4; clear Stack

5. Bare call conventions

This method is used when embedded assembly programmers need to manually control the call conventions. It does not have a keyword, but there is a _ declspec (naked) used to specify this call convention. It cannot be used for function declaration, but for function definition. That is to say, it can only prevent the compiler from generating stack management code in the function body, however, the caller still needs a previous call convention to generate the code for calling the function. I have never used this agreement. I hope you can add it to it.

Reference:
Http://www.codeproject.com/cpp/calling_conventions_demystified.asp
And answers from many CSDN users.

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.