Calling convention (Calling Convention) is the programming language in order to achieve the function call and the establishment of a protocol. This agreement provides the function of the parameters of language transmission, parameters are variable and stack who deal with such issues. Different language defines different calling convention.
In C + +, in order to allow operator overloading and function overloading, C + + compilers are in accordance with certain rules rewritten for each entry point symbol name, in order to allow the same name (with different parameter types or different scope) more a usage, but it will not break the existing C-based linker. This technique is often called the name of adaptation (Name Mangling) or the name of the modification (Name Decoration). Many C + + compiler vendors chose their name modification program.
Therefore, in order to make other language modules (such as Visual Basic applications, Pascal or Fortran applications, etc.) can call C / C + + written DLL function, you must use the correct calling convention for exported functions, and do not let the compiler on the function you want to export any names modified.
1. Calling convention (Calling Convention)
Calling convention to a decision to transfer function parameters and the stack when the stack order (by the caller or callee pop the argument stack), and the compiler used to identify the name of the function name modification agreement and other issues. In Microsoft VC + + 6.0 is defined in the following several calling conventions, we will combine the assembly language to 11 of them:
1, __cdecl
__cdecl is the C / C + + and MFC programs use the default calling convention, you can also add in the function declaration to manually specify __cdecl keyword. Use __cdecl convention, the function parameters in accordance with the order from right to left stack, and call the function are the parameters from the stack to clean up the stack pop. Therefore, the realization of a variable function of the parameter can only use the calling convention. Since each function using the __cdecl convention should clean up the stack contains the code, something the size of the executable file will be bigger. __cdecl can be written as _cdecl.
The following specific examples will be analyzed through a __cdecl convention:
In VC + +, create a new Win32 Console project, named cdecl. The code is as follows:
int __cdecl Add (int a, int b); file / / function declaration
void main ()
(
Add (1,2); file / / function call
)
int __cdecl Add (int a, int b) file / / function implementation
(
return (a + b);
)
Department disassembled code function call is as follows:
; Add (1,2);
push 2; parameter stack from right to left, first pressed into 2
push 1; pressed into a
call @ ILT +0 (Add) (00401005); call the function to achieve
add esp, 8; clear from the function call stack
2, __stdcall
__stdcall calling convention used to call the Win32 API function. Agreement with __stdcal, the function parameters in accordance with the order from right to left stack, the called function returns transport parameters of the stack before the clean-up, the number of fixed-function parameters. As the body of the function itself, the number of parameters that pass came, so the called function can return to the previous instruction with a ret n pass parameters directly to clean up the stack. __stdcall can be written as _stdcall.
Or the example of the __cdecl convention replaced __stdcall:
int __stdcall Add (int a, int b)
(
return (a + b);
)
Function call Department disassembled code:
; Add (1,2);
push 2; parameter stack from right to left, first pressed into 2
push 1; pressed into a
call @ ILT +10 (Add) (0040100f); call the function to achieve
Functions is part of the anti-assembly code:
; Int __stdcall Add (int a, int b)
push ebp
mov ebp, esp
[Next]
sub esp, 40h
push ebx
push esi
push edi
lea edi, [ebp-40h]
mov ecx, 10h
mov eax, 0CCCCCCCCh
rep stos dword ptr [edi]
; Return (a + b);
mov eax, dword ptr [ebp +8]
add eax, dword ptr [ebp +0 Ch]
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 8; clear stack
3, __fastcall
__fastcall convention used for very high performance requirements of the occasion. __fastcall convention will function from the left of the first two size not larger than 4 bytes (DWORD) of the parameters are placed in ECX and EDX registers, the rest of the parameters are still stacking transmission from right to left, the called function returns transmission parameters prior to clean up the stack. __fastcall can be written _fastcall.
Is still similar to the example, then the function calling convention for the __fastcall, increase the number of function parameters 2:
int __fastcall Add (int a, double b, int c, int d)
(
return (a + b + c + d);
)
Function call part of the assembly code:
; Add (1, 2, 3, 4);
push 4; two parameters after the stack from right to left, first pressed into 4
mov edx, 3; the int type 3 into edx
push 40000000h; pressed into double type 2
push 0
mov ecx, 1; the int type 1 into ecx
call @ ILT +0 (Add) (00401005); call the function to achieve
Functions is part of the anti-assembly code:
; Int __fastcall Add (int a, double b, int c, int d)
push ebp
mov ebp, esp
sub esp, 48h
push ebx
push esi
push edi
push ecx
lea edi, [ebp-48h]
mov ecx, 12h
mov eax, 0CCCCCCCCh
rep stos dword ptr [edi]
pop ecx
mov dword ptr [ebp-8], edx
mov dword ptr [ebp-4], ecx
[Next]
; Return (a + b + c + d);
fild dword ptr [ebp-4]
fadd qword ptr [ebp +8]
fiadd dword ptr [ebp-8]
fiadd dword ptr [ebp +10 h]
call __ftol (004011b8)
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0Ch; clear stack
Keyword __cdecl, __stdcall, and __fastcall can be added to the output of the function before, you can also build environment Setting ...-> C / C + + -> Code Generation option. Their corresponding command-line parameters are / Gd, / Gz and / Gr. The default state / Gd, that is __cdecl. When added to the output function keyword before the build environment of choice and not at the same time, a direct increase in output before the keyword function effectively.
Recommended links:
Command LINE "" and "-" symbol represents the meaning of
hp Pavilion dv6000 notebook reviews pics specs
ASF to MPG
Picked Business Databases And TOOLS
MKV to WMV
Wave of mergers BI have a happy life
Top Registry TOOLS
GIS integration OF the western expedition geographic resources
Recommend Tools And Editors
MP4 To WMP
Beijing: refused to sign the contract is terminated will receive compensation
Five-stroke Input Method Is Not Just Pole
LIVEMOTION font production: shadow word
Ali power to help her mother five years beyond the traditional media blog
Dell's green innovation-up plastic bag recycling to say "no"
Optional U disk watch "shell" scam