Conventions for using C language arguments

Source: Internet
Author: User
Tags microsoft c

In C, suppose we have a function like this:

Int function (int a, int B)


You can use this function by using result = function. However, when a high-level language is compiled into a machine code that can be recognized by a computer, a problem arises: In the CPU, the computer cannot know how many or what parameters are required for a function call, and there is no hardware to save these parameters. That is to say, 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.


Stack is an advanced post-release data structure with a storage zone and a stack top pointer. The stack top Pointer Points to the first available data item (called the stack top) in the stack ). You can add data to the stack at the top of the stack. This operation is called the Push operation. After the stack is pressed, the top of the stack automatically becomes the position of the newly added data item, the stack top pointer is also modified. You can also remove the top stack from the stack, which is called pop. After the stack pops up, an element at the top of the stack changes to the top of the stack, and the top pointer of the stack changes accordingly.


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:

 

When there are more than one parameter, in what order will the parameter be pushed into the stack?
Who will restore the original stack after the function is called?

In advanced languages, the two problems are described through the function call convention. Common call conventions include:


Stdcall
Cdecl
Fastcall
Thiscall
Naked call

Stdcall call conventions

Stdcall is often referred to as pascal's call convention, because pascal is a very common programming language for teaching in the early days, and its syntax is rigorous. 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 Convention Declaration is (the previous function is used as an example ):


Int _ stdcall function (int a, int B)


The call Convention of stdcall means: 1) the parameter is pushed from right to left into the stack, 2) the function modifies the stack itself. 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 conventions

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


When I wrote this article, I was surprised to find that the parameter pressure stack sequence in cdecl call conventions is the same as that in stdcall. The parameter is 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. Therefore, the function name is recorded as _ function in the symbol table, but this change does not appear to me during compilation.


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 in CRT is defined:

Int sprintf (char * buffer, const char * format ,...)


Since all the indefinite parameters can be determined through format, it is no problem to use the indefinite number of parameters.


Fastcall

The fastcall call Convention is similar to stdcall, which means:


The first and second DWORD parameters (or smaller ones) of the function are passed through ecx and edx, and other parameters are pushed from right to left
Call function to clear the stack
Function Name modification rules are the same as stdcall

Statement Syntax: int fastcall function (int a, int B)


Thiscall

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:


Parameter from right to left into 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 parameter pressure stacks.
If the number of parameters is not fixed, the caller clears the stack. Otherwise, the function clears the stack by itself.

To illustrate this call Convention, define the following classes and use code:

Class
{
Public:
Int function1 (int a, int B );
Int function2 (int ,...);
};
Int A: function1 (int a, int B)
{
Return a + B;
}
# Include

Int A: function2 (int ,...)
{
Va_list ap;
Va_start (ap, );
Int I;
Int result = 0;
For (I = 0; I <a; I ++)
{
Result + = va_arg (ap, int );
}
Return result;
}
Void callee ()
{
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 cdecl.


Naked call

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. 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,
_ 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 and used conventions are inconsistent, the stack will be damaged, leading to serious problems. The following are two common problems:


The function prototype declaration and function body definition are inconsistent.

Different function conventions are defined during DLL Function Import.


For example, if we declare a function in dll:

_ Declspec (dllexport) int func (int a, int B); // note that stdcall is not found here and cdecl is used.

The time code used is:


Typedef int (* winapi dllfunc) func (int a, int B );
HLib = LoadLibrary (...);
DLLFUNC func = (DLLFUNC) GetProcAddress (...) // The call conventions are modified here
Result = func (); // cause Error

Related Article

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.