[C ++ Basics] 041 _ function call rules (_ cdecl ,__ stdcall ,__ fastcall ,__ Pascal)

Source: Internet
Author: User

You do not need to know about the Function calling rules (call conventions) most of the time. However, if you need cross-language programming, such as calling a DLL written by VC using Delphi, you need to know.

Microsoft's VC adopts the _ cdecl method by default, while Windows API adopts the _ stdcall method. If you use VC to develop DLL for other languages, you should specify the _ stdcall method. It is very important to know who clears the stack. If you want to write an assembly function to call C, you must be careful with Stack clearing. If it is a function in the _ cdecl mode, then the function itself (if you do not need to compile the Code) does not need to care about clearing the stack for storing parameters. However, if it is a _ stdcall rule, you must exit the function (RET) stack.

1. _ cdecl
The so-called C call rule. The parameter is pushed from right to left to the stack. The caller pushes the parameter to the stack. Remember: The Memory stack of the transfer parameter is maintained by the caller. The returned value is in eax. Therefore, this rule must be used for functions that change parameters like printf. When the compiler is hungry for the function generation modifier of this call rule during compilation, it only adds an underline prefix before the output function name in the format of _ functionname.

2. _ stdcall
The parameter is pushed from right to left to the stack. The parameter is pushed to the stack by the caller. _ Stdcall is the default calling method of the PASCAL program. It is usually used in Win32 APIs. Remember: The function clears the stack when it exits, and the returned value is in eax. The _ stdcall call Convention adds an underline prefix before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is _ functionname @ number. For example, the modifier of int func (int A, double B) is _ FUNC @ 12.

3. _ fastcall
_ Fastcall is fast because it transmits parameters through registers (in fact, it uses ECx and EDX to transmit the first two DWORD or smaller parameters, the remaining parameters are still transmitted from the right to the left pressure stack, and the called function clears the memory stack of the transfer parameter before returning ). _ Fastcall: add the "@" symbol before the output function name, followed by the "@" symbol and the number of bytes of the parameter. The format is @ functionname @ number. This is very similar to _ stdcall. The only difference is that the first two parameters are transmitted through registers. Note that the two parameters transmitted through the Register are left-to-right, that is, the first parameter is added to ECx, and the third parameter is imported to EDX. Other parameters are transmitted from right to left into the stack. The returned results still pass through eax.

4. _ Pascal
This rule transmits parameters from left to right and is returned through eax. the stack is cleared by the caller.

 

5. _ thiscall

Applies only to "C ++" member functions. This pointer is stored in the Cx register and the parameter is pressed from right to left. Thiscall is not a keyword and cannot be specified by programmers.

 

See: http://blog.csdn.net/jia_xiaoxin/article/details/2868216

 

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.