Difference and connection between function call convention, such as cdecl and stdcall

Source: Internet
Author: User

We know that function calls are implemented by stacks. The Compiler helps us to press stacks and restore stacks so that when we call any function, do not perform stack pressure or stack recovery. Since there are many implementation methods, how can we tell the compiler in which way?


The answer is:Function call Convention (Function
Call Convention ).


Function call Convention (Function
What is call convention?

 
The function call Convention essentially tells the compiler how to compile advanced languages into assembly code.

Here the compiler should solve several problems:

 
1Determine the parameter pressure stack sequence.If there are more than one function parameter, it is to press the stack from right to left, or other.

 
2Who is responsible for function call stack restoration.Is the caller or the function itself restored?


First, let's take a look at the cdecl method, which is the default Processing Method for C/C ++. Cdecl tells the compiler that the parameter needs to be pressed from right to left, and the caller is responsible for restoring the stack (because of this, variable parameters can be implemented, support
Variable parameter length, refer to parameter ).

In C/C ++, we can use cdecl in this way.

Int _ cdecl F (int A, int B );

In fact, cdecl can be omitted, because this is the default method.


For the stdcall method, this is Pascal's processing method. stdcall tells the compiler that the parameter needs to be pressed from right to left and the function itself restores the stack.

For function f, we can change its call conventions:

Int _ stdcall F (int A, int B );

As for stdcall, there is another description, that is, to tell the compiler how to handle the function name: add an underscore before, add @ after, and then follow the total size of the parameter, such as the above function F, it becomes _ f @ 8.

The biggest difference between cdecl and stdcall is that cdecl restores the stack by the caller, while stdcall restores the stack by the function itself. They are both a method of function call conventions.


Common other function call conventions include:

Fastcall
Thiscall


Fastcall is similar to stdcall. The difference is that fastcall specifies that the first and second parameters smaller than the double byte parameters are passed through registers, but the way of over-pressure stack is not supported.


Thiscall is the default call Convention for member functions in C ++. The member function also has a this pointer, which is also a part of the parameter, so special processing is required.

It is like this:

1. input the stack from right to left.

2. If the number of parameters is determined, this pointer is passed to the function through ECx, and the function restores the stack by itself, similar to the stdcall method.

3. If the number of parameters is unknown, this pointer is pushed to the stack after all parameters are pressed. This is equivalent to T * Const. This is the first parameter. The caller restores the stack, similar to the cdecl method.


Reprinted please indicate the source:

Original article: http://blog.csdn.net/hongchangfirst/article/details/8765549

Author: hongchangfirst



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.