[System] Three function call conventions

Source: Internet
Author: User

Three function call conventions

_ Cdecl, _ stdcall, and _ fastcall are the three common function call methods in C/C ++. _ Cdecl is the default call Method for C/C ++, and __stdcall is the call Method for Windows API functions, however, when we view the declaration of these APIs in the header file, we use the winapi macro instead, and this macro is actually _ stdcall.

I believe you should understand the differences between the three call methods. This article mainly describes the manifestations of these differences from the perspective of examples and compilation, so that their different understandings can be transitioned from theory to practice.

_ Cdecl: Default Mode for C/C ++. parameters are pushed from right to left. The main function is responsible for Stack balancing.

_ Stdcall: Windows API default mode. parameters are pushed from right to left into the stack. The called function is responsible for Stack balancing.

_ Fastcall: quick call method. The so-called fast, this method selects to pass the parameter first from the register (ECx and EDX), and the remaining parameters are then passed from right to left from the stack. Because the stack is located in the memory area and the register is located in the CPU, the access method is faster than the memory, so its name is "_ fastcall ".

The following describes the three call conventions from the instance. Let's take a look at a simple program:

The content of the three functions is the same. The difference is that three call methods are used. Let's take a look at the assembly code when the main function calls three functions:

 

As mentioned above, __cdecl enters the stack area from right to left according to the parameters. Note the difference between fun1 () and fun3 (). fun1 () is in call fun1 () then, add ESP, 8 is executed. This operation balances the stack as we mentioned earlier. Before calling a function, we perform two push operations consecutively to push the real parameters 5 and 2 required by the function into the stack. After the call is complete, we need to restore the status before the call, you need to adjust the position of the top pointer esp of the stack. The two function calling methods are determined by who will complete this operation: _ cdecl (the main function is completed) and _ stdcall (completed by the called function. We can see that _ cdecl is completed by the main call function, SO _ stdcall is switched to the code at the end of the called function in the called function fun3, we can see this sentence:

 

So what is the end of fun1?

 

 

As you can see, the RET command is followed by a value which determines that the function return is the amount that ESP needs to increase. In this way, the Add command does not need to be called by the main function to balance the stack zone for the ESP operation, saving the program overhead. the overhead of one command is small. If 100,000 million such calls are made, this overhead is obvious.

After _ cdecl and _ stdcall are finished, let's take a look at _ fastcall. As shown in the figure above, we didn't use the push command to pass parameters to the stack, but used

MoV edX, 5

MoV ECx, 2

Two commands. In this way, the parameter is directly passed into the register. When the called function is executed, it can directly take the value from the register, saving the need to retrieve the value from the stack to the register, and then fetch the value from the Register into the memory.

However, to put it aside, the ECX register is often used as a medium for counting and passing this pointer in C ++. In this case, what is the situation? We will discuss the next analysis of the c ++ operator new. When ECx is used as a counter, it needs to push the real parameters stored in ECx into the stack area first, and then pop out after counting is complete. As a result, this fastcall seems less fast. Haha.

Of course, all the operations mentioned above are performed by the compiler behind us. Developers do not need to care about these operations and are transparent to us. However, you can never be defeated by knowing what it is!

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.