Three key function call conventions and key function call conventions

Source: Internet
Author: User

Three key function call conventions and key function call conventions

After the advanced language is translated into a machine code, the computer cannot know the number, type, and hardware of the parameters called by the function.

In addition, in C ++, the function naming method is different from that in general C language because of overload. This method is called name adaptation.

When calling an interface between a function caller and a function, especially when calling an interface across languages, a Protocol Convention is required to pass the parameter-stack.

 

Key process:

During the call, the caller presses the parameter stack in sequence and then calls the function,

The called function retrieves data from the stack and performs computation.

After function compute is completed, the caller or the function modifies the stack to restore the original stack.

 

Common function call conventions:

  • Stdcall
  • Cdecl
  • Fastcall
  • Thiscall
  • Naked call

 

The keywords _ cdecl, _ stdcall, and _ fastcall can be directly added before the function to be output,

You can also select the Setting...> C/C ++> Code Generation item in the compiling environment.

Their corresponding command line parameters are/Gd,/Gz, And/Gr. The default status is/Gd, Which is _ cdecl.

When the keywords added before the output function are different from those selected in the compiling environment, the keywords directly added before the output function are valid.

 

There are two key issues:

Generally, the stack is written from right to left.

The key difference lies in who is responsible for clearing, which corresponds to two typical Conventions: stdcall and cdecl.

 

Stdcall Convention-pascal call convention. WINAPI, CALLBACK

  • The parameter is pushed to the stack from right to left.
  • The function modifies the stack.
  • The function name is automatically prefixed with the underscore "_" and suffix "@ parameter size ".

Cdecl Convention-C call convention, C language default

  • The parameter is pushed to the stack from right to left.
  • The function itself does not clean the stack, and the caller is responsible for clearing the stack.
  • The function name is automatically prefixed with an underscore "_" without a suffix.

The function itself does not clean 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.

Because the parameters are pressed from right to left, the initial parameter is at the position closest to the top of the stack.

When an indefinite number of parameters is used, the position of the first parameter in the stack is certainly known, as long as the number of parameters can be determined based on the first or subsequent explicit parameters, you can use an indefinite parameter,

For example, the sprintf function,

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.

 

Thiscall

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.

Since the member function call has another this pointer, special processing is required,

  • 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.

 

WINAPI
It can be translated into appropriate call conventions for function use. This macro is defined in windef. h. Some content in windef. h is as follows:

#define CDECL _cdecl#define WINAPI CDECL#define CALLBACK __stdcall#define WINAPI __stdcall#define APIENTRY WINAPI

 

Rules for modifying function names during C ++ Compilation:

_ Stdcall call conventions:

1. Take "?" Start of the function name, followed by the function name;

2. The parameter table starts with "@ YG" after the function name, followed by the parameter table;

3. The parameter table is represented in 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. The pointer ID is before the Data Type

;

5. mark the end of the entire name with "@ Z" after the parameter table. If this function has no 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 ++.

 

 

If the defined and used conventions are inconsistent, the stack will be damaged.


Function call conventions

Baidu searched for a large piece of information. But it is not necessary for beginners to know. Come on, and it is hard to understand many things that you have no experience.

In the following function call statement, the number of real arguments of the func function is f (f2 (u1, u2), (u3, u4), (u6, max (u7, u8 ))); a) 3 B) 4 C) 5 D) 8

A) 3
Are the following three
F2 (u1, u2) // function expression
(U3, u4) // comma expression
(U6, max (u7, u8) // comma expression

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.