通常在使用VC進行函數定義時會指定該函數調用方式,諸如:
{
return a>b?a:b;
}
{
return a<b?a:b;
}
{
return a=b?true:false;
}
{
return a>b?a:b;
}
{
return a<b?a:b;
}
__thiscall對每個函數都增加了一個類指針參數
class aa
{
void bb(int cc);
};
實際上bb的函數原形是void bb(aa &this, int cc);
__cdecl的調用方式介紹: C和C++缺省調用方式
例子:
void Input( int &m,int &n);/*相當於void __cdecl Input(int &m,int &n);*/
以下是相應的彙編代碼:
00401068 lea eax,[ebp-8] ;取[ebp-8]地址(ebp-8),存到eax |
例子:
void WINAPI Input( int &m,int &n);
看一下相應調用的彙編代碼:
00401068 lea eax,[ebp-8] |
40: {
00401110 push ebp
00401111 mov ebp,esp
00401113 sub esp,48h
00401116 push ebx
00401117 push esi
00401118 push edi
00401119 lea edi,[ebp-48h]
0040111C mov ecx,12h
00401121 mov eax,0CCCCCCCCh
00401126 rep stos dword ptr [edi]
41: int s,i;
42:
43: while(1)
00401128 mov eax,1
0040112D test eax,eax
0040112F je Input+0C1h (004011d1)
44: {
45: printf("\nPlease input the first number m:");
00401135 push offset string "\nPlease input the first number m"... (004260b8)
0040113A call printf (00401530)
0040113F add esp,4
46: scanf("%d",&m);
00401142 mov ecx,dword ptr [ebp+8]
00401145 push ecx
00401146 push offset string "%d" (004260b4)
0040114B call scanf (004015f0)
00401150 add esp,8
47:
48: if ( m= s )
004011B3 mov eax,dword ptr [ebp+8]
004011B6 mov ecx,dword ptr [eax]
004011B8 cmp ecx,dword ptr [ebp-4]
004011BB jl Input+0AFh (004011bf)
57: break;
004011BD jmp Input+0C1h (004011d1)
58: else
59: printf(" m < n*(n+1)/2,Please input again!\n");
004011BF push offset string " m < n*(n+1)/2,Please input agai"... (00426060)
004011C4 call printf (00401530)
004011C9 add esp,4
60: }
004011CC jmp Input+18h (00401128)
61:
62: }
004011D1 pop edi
004011D2 pop esi
004011D3 pop ebx
004011D4 add esp,48h
004011D7 cmp ebp,esp
004011D9 call __chkesp (004015b0)
004011DE mov esp,ebp
004011E0 pop ebp
004011E1 ret 8
如:
push edx
push edi
push eax
push ebx
call getdlgitemtexta
1、__stdcall調用約定相當於16位動態庫中經常使用的PASCAL調用約定。在32位的VC++5.0中PASCAL調用約定不再被支持(實際上它已被定義爲__stdcall。除了__pascal 外,__fortran和__syscall也不被支持),取而代之的是__stdcall調用約定。兩者實質上是一致的,即函數的參數自右向左通過棧傳遞,被調用的函數在返回前清理傳送參數的內存棧,但不同的是函數名的修飾部分(關於函數名的修飾部分在後面將詳細說明)。
2、C調用約定(即用__cdecl關鍵字說明)按從右至左的順序壓參數入棧,由調用者把參數彈出棧。對於傳送參數的內存棧是由調用者來維護的(正因爲如此,實現可變參數的函數只能使用該調用約定)。另外,在函數名修飾約定方面也有所不同。
3、__fastcall調用約定是“人”如其名,它的主要特點就是快,因爲它是通過寄存器來傳送參數的(實際上,它用ECX和EDX傳送前兩個雙字(DWORD)或更小的參數,剩下的參數仍舊自右向左壓棧傳送,被調用的函數在返回前清理傳送參數的內存棧),在函數名修飾約定方面,它和前兩者均不同。