我們知道在進(jìn)行函數(shù)調(diào)用時(shí),有幾種調(diào)用方法,分為C式,Pascal式。在C和C++中C式調(diào)用是缺省的,除非特殊聲明。二者是有區(qū)別的,下面我們用實(shí)例說明一下:
?1. __cdecl :C和C++缺省調(diào)用方式
? 例子:
?void Input( int &m,int &n);/*相當(dāng)于void __cdecl Input(int &m,int &n);*/
?? 以下是相應(yīng)的匯編代碼:
?? 00401068?? lea???????? eax,[ebp-8] ;取[ebp-8]地址(ebp-8),存到eax
?? 0040106B?? push??????? eax???????? ;然后壓棧
?? 0040106C?? lea???????? ecx,[ebp-4] ;取[ebp-4]地址(ebp-4),存到ecx
?? 0040106F?? push??????? ecx???????? ;然后壓棧
?? 00401070?? call??????? @ILT+5(Input) (0040100a);然后調(diào)用Input函數(shù)
?? 00401075?? add???????? esp,8?????? ;恢復(fù)棧
??
? 從以上調(diào)用Input函數(shù)的過程可以看出:在調(diào)用此函數(shù)之前,首先壓棧ebp-8,然后壓棧ebp-4,然后調(diào)用函數(shù)Input,最后Input函數(shù)調(diào)用結(jié)束后,利用esp+8恢復(fù)棧。由此可見,在C語(yǔ)言調(diào)用中默認(rèn)的函數(shù)修飾_cdecl,由主調(diào)用函數(shù)進(jìn)行參數(shù)壓棧并且恢復(fù)堆棧。
? 下面看一下:地址ebp-8和ebp-4是什么?
? 在VC的VIEW下選debug windows,然后選Registers,顯示寄存器變量值,然后在選debug windows下面的Memory,輸入ebp-8的值和ebp-4的值(或直接輸入ebp-8和-4),看一下這兩個(gè)地址實(shí)際存儲(chǔ)的是什么值,實(shí)際上是變量 n 的地址(ebp-8),m的地址(ebp-4),由此可以看出:在主調(diào)用函數(shù)中進(jìn)行實(shí)參的壓棧并且順序是從右到左。另外,由于實(shí)參是相應(yīng)的變量的引用,也證明實(shí)際上引用傳遞的是變量的地址(類似指針)。
?總結(jié):在C或C++語(yǔ)言調(diào)用中默認(rèn)的函數(shù)修飾_cdecl,由主調(diào)用函數(shù)進(jìn)行參數(shù)壓棧并且恢復(fù)堆棧,實(shí)參的壓棧順序是從右到左,最后由主調(diào)函數(shù)進(jìn)行堆棧恢復(fù)。由于主調(diào)用函數(shù)管理堆棧,所以可以實(shí)現(xiàn)變參函數(shù)。另外,命名修飾方法是在函數(shù)前加一個(gè)下劃線(_).
? 2. WINAPI (實(shí)際上就是PASCAL,CALLBACK,_stdcall)
? 例子:
?void WINAPI Input( int &m,int &n);
?看一下相應(yīng)調(diào)用的匯編代碼:
?00401068?? lea???????? eax,[ebp-8]
?0040106B?? push??????? eax
?0040106C?? lea???????? ecx,[ebp-4]
?0040106F?? push??????? ecx
?00401070?? call??????? @ILT+5(Input) (0040100a)
??? 從以上調(diào)用Input函數(shù)的過程可以看出:在調(diào)用此函數(shù)之前,首先壓棧ebp-8,然后壓棧ebp-4,然后調(diào)用函數(shù)Input,在調(diào)用函數(shù)Input之后,沒有相應(yīng)的堆棧恢復(fù)工作(為其它的函數(shù)調(diào)用,所以我沒有列出)
??? 下面再列出Input函數(shù)本身的匯編代碼:(實(shí)際此函數(shù)不大,但做匯編例子還是大了些,大家可以只看前和后,中間代碼與此例子無(wú)關(guān))
39: void WINAPI Input( int &m,int &n)
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<1 ) continue;
00401153?? mov???????? edx,dword ptr [ebp+8]
00401156?? cmp???????? dword ptr [edx],1
00401159?? jge???????? Input+4Dh (0040115d)
0040115B?? jmp???????? Input+18h (00401128)
49:?????? printf("\nPlease input the first number n:");
0040115D?? push??????? offset string "\nPlease input the first number n"... (0042608c)
00401162?? call??????? printf (00401530)
00401167?? add???????? esp,4
50:?????? scanf("%d",&n);
0040116A?? mov???????? eax,dword ptr [ebp+0Ch]
0040116D?? push??????? eax
0040116E?? push??????? offset string "%d" (004260b4)
00401173?? call??????? scanf (004015f0)
00401178?? add???????? esp,8
51:
52:?????? if ( n<1 ) continue;
0040117B?? mov???????? ecx,dword ptr [ebp+0Ch]
0040117E?? cmp???????? dword ptr [ecx],1
00401181?? jge???????? Input+75h (00401185)
00401183?? jmp???????? Input+18h (00401128)
53:
54:?????? for(i=1,s=0;i<=n;i++)
00401185?? mov???????? dword ptr [ebp-8],1
0040118C?? mov???????? dword ptr [ebp-4],0
00401193?? jmp???????? Input+8Eh (0040119e)
00401195?? mov???????? edx,dword ptr [ebp-8]
00401198?? add???????? edx,1
0040119B?? mov???????? dword ptr [ebp-8],edx
0040119E?? mov???????? eax,dword ptr [ebp+0Ch]
004011A1?? mov???????? ecx,dword ptr [ebp-8]
004011A4?? cmp???????? ecx,dword ptr [eax]
004011A6?? jg????????? Input+0A3h (004011b3)
55:?????????? s=s+i;
004011A8?? mov???????? edx,dword ptr [ebp-4]
004011AB?? add???????? edx,dword ptr [ebp-8]
004011AE?? mov???????? dword ptr [ebp-4],edx
004011B1?? jmp???????? Input+85h (00401195)
56:?????? 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
最后,我們看到在函數(shù)末尾部分,有ret 8,明顯是恢復(fù)堆棧,由于在32位C++中,變量地址為4個(gè)字節(jié)(int也為4個(gè)字節(jié)),所以彈棧兩個(gè)地址即8個(gè)字節(jié)。
? 由此可以看出:在主調(diào)用函數(shù)中負(fù)責(zé)壓棧,在被調(diào)用函數(shù)中負(fù)責(zé)恢復(fù)堆棧。因此不能實(shí)現(xiàn)變參函數(shù),因?yàn)楸徽{(diào)函數(shù)不能事先知道彈棧數(shù)量,但在主調(diào)函數(shù)中是可以做到的,因?yàn)閰?shù)數(shù)量由主調(diào)函數(shù)確定。
? 下面再看一下,ebp-8和ebp-4這兩個(gè)地址實(shí)際存儲(chǔ)的是什么值,ebp-8地址存儲(chǔ)的是n 的值,ebp -4存儲(chǔ)的是m的值。說明也是從右到左壓棧,進(jìn)行參數(shù)傳遞。
?? 總結(jié):在主調(diào)用函數(shù)中負(fù)責(zé)壓棧,在被調(diào)用函數(shù)中負(fù)責(zé)彈出堆棧中的參數(shù),并且負(fù)責(zé)恢復(fù)堆棧。因此不能實(shí)現(xiàn)變參函數(shù),參數(shù)傳遞是從右到左。另外,命名修飾方法是在函數(shù)前加一個(gè)下劃線(_),在函數(shù)名后有符號(hào)(@),在@后面緊跟參數(shù)列表中的參數(shù)所占字節(jié)數(shù)(10進(jìn)制),如:void Input(int &m,int &n),被修飾成:_Input@8
?? 對(duì)于大多數(shù)api函數(shù)以及窗口消息處理函數(shù)皆用 CALLBACK ,所以調(diào)用前,主調(diào)函數(shù)會(huì)先壓棧,然后api函數(shù)自己恢復(fù)堆棧。
??
?? 如:
????? push edx
????? push edi
????? push eax
????? push ebx
????? call getdlgitemtexta
?? 你可以想一下,這幾個(gè)寄存器中存的都是什么?
參考:msdn
例子為在VC6.0下debug模式下的Win32 Console反匯編代碼。