函數調用約定主要約束了兩件事:
1.參數傳遞順序
2.調用堆棧由誰(調用函數或被調用函數)清理
常見的函數調用約定:stdcall cdecl fastcall thiscall naked call
著重講一下stdcall cdecl fastcall,
stdcall 與cdecl有相同的參數傳遞順序 ,參數從右向左壓入堆棧,不同的是清理堆棧工作由誰完成。stdcall由調用的子函數完成,cdecl由調用的函數完成。 www.2cto.com
VC中 C/C++ 默認調用約定是cdecl.
上代碼,如下。
#include <stdio.h>
//__fastcall 調用
int __fastcall sum_fas(int a, int b, int c)
{
return (a + b + c);
}
//__stdcall 調用
int __stdcall sum_std(int a, int b, int c)
{
return (a + b + c);
}
//__cdecl 調用
int __cdecl sum_cde(int a, int b, int c)
{
return (a + b + c);
}
//VC中 C/C++ 默認調用
int sum (int a, int b, int c)
{
return (a + b + c);
}
int main()
{
int c1,c2,c3,c4;
c1 = sum_fas(2, 3, 5);
c2 = sum_std(2, 3, 5);
c3 = sum_cde(2, 3, 5);
c4 = sum(2,3,5);
return 0;
}
匯編代碼:
int main()
{
011D1500 push ebp
011D1501 mov ebp,esp
011D1503 sub esp,0F0h
011D1509 push ebx
011D150A push esi
011D150B push edi
011D150C lea edi,[ebp-0F0h]
011D1512 mov ecx,3Ch
011D1517 mov eax,0CCCCCCCCh
011D151C rep stos dword ptr es:[edi]
int c1,c2,c3,c4;
c1 = sum_fas(2, 3, 5);
011D151E push 5
011D1520 mov edx,3
011D1525 mov ecx,2
011D152A call sum_fas (011D1037h)
011D152F mov dword ptr [c1],eax
c2 = sum_std(2, 3, 5);
011D1532 push 5
011D1534 push 3
011D1536 push 2
011D1538 call sum_std (011D10E6h)
011D153D mov dword ptr [c2],eax
c3 = sum_cde(2, 3, 5);
011D1540 push 5
011D1542 push 3 www.2cto.com
011D1544 push 2
011D1546 call sum_cde (011D10B4h)
011D154B add esp,0Ch
011D154E mov dword ptr [c3],eax
c4 = sum(2,3,5);
011D1551 push 5
011D1553 push 3
011D1555 push 2
011D1557 call sum (011D1113h)
011D155C add esp,0Ch
011D155F mov dword ptr [c4],eax
return 0;
011D1562 xor eax,eax
}