[Function call method] _ stdecl _ stdcall _ fastcall _ thiscall

Source: Internet
Author: User

 

_ CdeclIt is the abbreviation of c Declaration (Declaration), which indicates the default function call method of C language: All parameters are pushed to the stack from right to left. These parameters are cleared by the caller, which is called manual stack clearing. The called function does not require the caller to pass many parameters. Too many or too few parameters are passed by the caller, and even different parameters do not produce compilation errors.

 

_ StdcallIt is the abbreviation of standardcall, which is the standard call method of c ++. All parameters are pushed to the stack from right to left. If it is a call class member, the next incoming stack is the this pointer. The parameters in the stack are cleared when the called function returns. The command is retnx, and X indicates the number of bytes occupied by the parameter, the CPU automatically pops up X bytes of stack space after ret. It is called automatic stack clearing. During compilation, the function must determine the number of parameters, and the caller must strictly control the generation of parameters. There must be no more or fewer parameters. Otherwise, an error will occur after the return.

 

_ FastcallIs the quick call method specified by the compiler. Because most function parameters are few, it is time-consuming to use stack transfer. Therefore_ FastcallIt is usually required that the first two (or several) parameters are passed by the register, and the other parameters are passed through the stack. The registers defined by programs compiled by different compilers are different. Return method and_ StdcallEquivalent.

 

 

_ ThiscallIt is specified to solve this pointer passing in class member calls._ ThiscallThis pointer must be placed in a specific register, which is determined by the compiler. VC uses ECx and Borland's c ++ compiler uses eax. The return method is equivalent to _ stdcall.

 

 

  _ FastcallAnd_ ThiscallThe involved registers are determined by the compiler and therefore cannot be used as cross-compiler interfaces. Therefore, the COM object interface on Windows is defined as the _ stdcall call method.

 

 

Call type

Parameter stack order

Who is responsible for Stack clearance

Register transfer

_ Stdecl

Stack entry from right to left

Call a function

This

_ Stdcall

Stack entry from right to left

QuiltCall a function

This

_ Fastcall

Stack entry from right to left

QuiltCall a function

This
And Parameters

_ Thiscall

Stack entry from right to left

QuiltCall a function

This

Default (vs2005)

Stack entry from right to left

QuiltCall a function

This

 

 

 

 

 

 

 

 

The following code shows how they pass parameters and access stacks:

 

 

Class testcalltype <br/>{< br/> Public: <br/> int default_call (INT, INT); <br/> int _ cdecl _ cdecl_call (int, int B); <br/> int _ stdcall _ stdcall_call (int A, int B); <br/> int _ fastcall _ fastcall_call (int A, int B ); <br/> int _ thiscall _ thiscall_call (INT, INT); <br/> int X; <br/>}; <br/> int testcalltype :: default_call (int A, int B) <br/>{< br/> This-> X = a + B; <br/> return this-> X; <br/>}< br/> int testcalltype: :__ cdecl_call (int A, int B) <br/>{< br/> This-> X = A + B; <br/> return this-> X; <br/>}< br/> int testcalltype: _ stdcall_call (int A, int B) <br/>{< br/> This-> X = a + B; <br/> return this-> X; <br/>}< br/> int testcalltype:: _ fastcall_call (int A, int B) <br/>{< br/> This-> X = a + B; <br/> return this-> X; <br/>}< br/> int testcalltype: _ thiscall_call (int A, int B) <br/>{< br/> This-> X = A + B; <br/> return this-> X; <br/>} 

 

The called location is decompiled as follows:

Int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> 004115b0 push EBP <br/> 004115b1 mov EBP, ESP <br/> 004115b3 sub ESP, 0cch <br/> 004115b9 push EBX <br/> 004115ba push ESI <br/> 004115bb push EDI <br/> 004115bc Lea EDI, [ebp-0CCh] <br/> 004115c2 mov ECx, 33 H <br/> 004115c7 mov eax, 0 cccccccch <br/> 004115cc rep STOs dword ptr es: [EDI] <br/> testcalltype call_type; <br/> call_type.default_call (1, 2 ); <br/> 004115ce push 2/* parameter stack entry */<br/> 004115d0 Push 1/* parameter stack entry */<br/> 004115d2 Lea ECx, [call_type]/* This pointer */<br/> 004115d5 call testcalltype: default_call (411113 h) <br/> call_type. _ cdecl_call (1, 2 ); <br/> 004115da push 2/* parameter stack entry */<br/> 004115dc Push 1/* parameter stack entry */<br/> 004115de Lea eax, [call_type]/* This pointer */<br/> 004115e1 push eax <br/> 004115e2 call testcalltype :__ cdecl_call (41112ch) <br/> 004115e7 add ESP, 0ch/* the caller is responsible for Stack clearing */<br/> call_type. _ fastcall_call (1, 2); <br/> 004115ea push 2/* parameter stack entry */<br/> 004115ec mov edX, 1/* The first parameter is passed through the Register */<br/> 004115f1 Lea ECx, [call_type]/* This pointer */<br/> 004115f4 call testcalltype :: _ fastcall_call (41100ah) <br/> call_type. _ stdcall_call (1, 2); <br/> 004115f9 push 2 <br/> 004115fb Push 1 <br/> 004115fd Lea eax, [call_type]/* This pointer */<br/> 00411600 pushes eax <br/> 00411601 call testcalltype: _ stdcall_call (4110ebh) <br/> call_type. _ thiscall_call (1, 2); <br/> 00411606 push 2 <br/> 00411608 Push 1 <br/> 0041160a Lea ECx, [call_type]/* This pointer */<br/> 0041160d call testcalltype: _ thiscall_call (41117ch) <br/> return 0; <br/> 0044251e XOR eax, eax <br/>} 

 

Disassembly of function definitions:

Int testcalltype: default_call (int A, int B) <br/>{< br/> 004113d0 push EBP <br/> 004113d1 mov EBP, ESP <br/> 004113d3 sub ESP, 0cch <br/> 004113d9 push EBX <br/> 004113da push ESI <br/> 00411db push EDI <br/> 004113dc push ECx <br/> 004113dd Lea EDI, [ebp-0CCh] <br/> 004113e3 mov ECx, 33 H <br/> 004113e8 mov eax, 0 cccccccccch <br/> 004113ed rep STOs dword ptr es: [EDI] <br/> 004113ef pop ECx <br/> 004113f0 mov dword ptr [ebp-8], ECx <br/> This-> X = A + B; <br/> 004113f3 mov eax, dword ptr [a] <br/> 004113f6 add eax, dword ptr [B] <br/> 004113f9 mov ECx, dword ptr [this] <br/> 004113fc mov dword ptr [ECx], eax <br/> return this-> X; <br/> 004113fe mov eax, dword ptr [this] <br/> 00411401 mov eax, dword ptr [eax] <br/>}< br/> 00411403 pop EDI <br/> 00411404 pop ESI <br/> 00411405 pop EBX <br/> 00411406 mov ESP, EBP <br/> 00411408 pop EBP/* the default call is _ stdcall. The called function is responsible for Stack clearing */<br/> 00411409 RET 8 <br/> --- no source file restart </P> <p> int testcalltype: :__ cdecl_call (int A, int B) <br/>{< br/> 00411420 push EBP <br/> 00411421 mov EBP, esp <br/> 00411423 sub ESP, 0c0h <br/> 00411429 push EBX <br/> 0041142a push ESI <br/> 0041142b push EDI <br/> 0041142c Lea EDI, [ebp-0C0h] <br/> 00411432 mov ECx, 30 h <br/> 00411437 mov eax, 0 cccccccch <br/> 0041143c rep STOs dword ptr es: [EDI] <br/> This-> X = a + B; <br/> 0041143e mov eax, DWORD PTR [a] <br/> 00411441 add eax, dword ptr [B] <br/> 00411444 mov ECx, DWORD PTR [this] <br/> 00411447 mov DWORD PTR [ECx], eax <br/> return this-> X; <br/> 00411449 mov eax, dword ptr [this] <br/> 0041144c mov eax, dword ptr [eax] <br/>}< br/> 0041144e pop EDI <br/> 0041144f pop ESI <br/> 00411450 pop EBX <br/> 00411451 mov ESP, EBP <br/> 00411453 pop EBP <br/> 00411454 RET/* _ cdecl is not responsible for Stack clearing */<br/> --- no source file exist </P> <p> int testcalltype:: _ stdcall_call (int A, int B) <br/>{< br/> 00411470 push EBP <br/> 00411471 mov EBP, esp <br/> 00411473 sub ESP, 0c0h <br/> 00411479 push EBX <br/> 0041147a push ESI <br/> 0041147b push EDI <br/> 0041147c Lea EDI, [ebp-0C0h] <br/> 00411482 mov ECx, 30 h <br/> 00411487 mov eax, 0 cccccccch <br/> 0041148c rep STOs dword ptr es: [EDI] <br/> This-> X = a + B; <br/> 0041148e mov eax, DWORD PTR [a] <br/> 00411491 add eax, dword ptr [B] <br/> 00411494 mov ECx, DWORD PTR [this] <br/> 00411497 mov DWORD PTR [ECx], eax <br/> return this-> X; <br/> 00411499 mov eax, dword ptr [this] <br/> 0041149c mov eax, dword ptr [eax] <br/>}< br/> 0041149e pop EDI <br/> 0041149f pop ESI <br/> 004114a0 pop EBX <br/> 004114a1 mov ESP, EBP <br/> 004114a3 pop EBP <br/> 004114a4 RET 0ch // clear the stack, <br/> --- no source file records </P> <p> int testcalltype: _ fastcall_call (int A, int B) <br/>{< br/> 004114c0 push EBP <br/> 004114c1 mov EBP, esp <br/> 004114c3 sub ESP, 0d8h <br/> 004114c9 push EBX <br/> 004114ca push ESI <br/> 004114cb push EDI <br/> 004114cc push ECx <br/> 004114cd Lea EDI, [ebp-0D8h] <br/> 004114d3 mov ECx, 36 h <br/> 004114d8 mov eax, 0 cccccccccch <br/> 004114dd rep STOs dword ptr es: [EDI] <br/> 004114df pop ECx <br/> 004114e0 mov dword ptr [ebp-8], EDX <br/> 004114e3 mov dword ptr [ebp-14h], ECX <br/> This-> X = a + B; <br/> 004114e6 mov eax, DWORD PTR [a] <br/> 004114e9 add eax, dword ptr [B] <br/> 004114ec mov ECx, DWORD PTR [this] <br/> 004114ef mov DWORD PTR [ECx], eax <br/> return this-> X; <br/> 004114f1 mov eax, dword ptr [this] <br/> 004114f4 mov eax, dword ptr [eax] <br/>}< br/> 004114f6 pop EDI <br/> 004114f7 pop ESI <br/> 004114f8 pop EBX <br/> 004114f9 mov ESP, EBP <br/> 004114fb pop EBP <br/> 004114fc RET 4 // clear the stack, <br/> --- no source file records </P> <p> int testcalltype: _ thiscall_call (int A, int B) <br/>{< br/> 00411510 push EBP <br/> 00411511 mov EBP, esp <br/> 00411513 sub ESP, 0cch <br/> 00411519 push EBX <br/> 0041151a push ESI <br/> 0041151b push EDI <br/> 0041151c push ECx <br/> 0041151d Lea EDI, [ebp-0CCh] <br/> 00411523 mov ECx, 33 H <br/> 00411528 mov eax, 0 cccccccch <br/> 0041152d rep STOs dword ptr es: [EDI] <br/> 0041152f pop ECx <br/> 00411530 mov dword ptr [ebp-8], ECx <br/> This-> X = A + B; <br/> 00411533 mov eax, dword ptr [a] <br/> 00411536 add eax, dword ptr [B] <br/> 00411539 mov ECx, dword ptr [this] <br/> 0041153c mov dword ptr [ECx], eax <br/> return this-> X; <br/> 0041153e mov eax, dword ptr [this] <br/> 00411541 mov eax, dword ptr [eax] <br/>}< br/> 00411543 pop EDI <br/> 00411544 pop ESI <br/> 00411545 pop EBX <br/> 00411546 mov ESP, EBP <br/> 00411548 pop EBP <br/> 00411549 RET 8 // clear the stack. The number of bytes corresponds to the parameter inbound stack <br/> --- no source file ------------------------------------------------------------- 

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.