__cdecl,__stdcall和__fastcall

  __cdecl  is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall , because it requires each function call to include stack cleanup code.
  The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack, so the compiler makes vararg functions __cdecl. Functions that use this calling convention require a function prototype.
  一下实验实验三种调用方式:
#include <iostream>
#include <string>
using std::cout;
using std::endl;using std::string;

int __stdcall maxs(int a,int b)

 return a>b?a:b;
}
int __cdecl maxc(int a,int b)
{
 return a>b?a:b;
}
int __fastcall maxi(int a,int b)
{
 return a>b?a:b;
}
int main()
{
 maxs(1,2);
 maxc(2,3);
 maxi(3,4);
}
反汇编代码:
int main()
{
00411B60  push        ebp 
00411B61  mov         ebp,esp
00411B63  sub         esp,0C0h
00411B69  push        ebx 
00411B6A  push        esi 
00411B6B  push        edi 
00411B6C  lea         edi,[ebp-0C0h]
00411B72  mov         ecx,30h
00411B77  mov         eax,0CCCCCCCCh
00411B7C  rep stos    dword ptr [edi]
 maxs(1,2);
00411B7E  push        2   
00411B80  push        1   
00411B82  call        maxs (4110D2h)
 maxc(2,3);
00411B87  push        3   
00411B89  push        2   
00411B8B  call        maxc (411361h)
00411B90  add         esp,8
 maxi(3,4);
00411B93  mov         edx,4
00411B98  mov         ecx,3
00411B9D  call        maxi (411519h)
}int main()
{
00411B60  push        ebp 
00411B61  mov         ebp,esp
00411B63  sub         esp,0C0h
00411B69  push        ebx 
00411B6A  push        esi 
00411B6B  push        edi 
00411B6C  lea         edi,[ebp-0C0h]
00411B72  mov         ecx,30h
00411B77  mov         eax,0CCCCCCCCh
00411B7C  rep stos    dword ptr [edi]
 maxs(1,2);
00411B7E  push        2   
00411B80  push        1   
00411B82  call        maxs (4110D2h)
 maxc(2,3);
00411B87  push        3   
00411B89  push        2   
00411B8B  call        maxc (411361h)
00411B90  add         esp,8
 maxi(3,4);
00411B93  mov         edx,4
00411B98  mov         ecx,3
00411B9D  call        maxi (411519h)
}
int __stdcall maxs(int a,int b)

00411A30  push        ebp 
00411A31  mov         ebp,esp
00411A33  sub         esp,0C4h
00411A39  push        ebx 
00411A3A  push        esi 
00411A3B  push        edi 
00411A3C  lea         edi,[ebp-0C4h]
00411A42  mov         ecx,31h
00411A47  mov         eax,0CCCCCCCCh
00411A4C  rep stos    dword ptr [edi]
 return a>b?a:b;
00411A4E  mov         eax,dword ptr [a]
00411A51  cmp         eax,dword ptr [b]
00411A54  jle         maxs+31h (411A61h)
00411A56  mov         ecx,dword ptr [a]
00411A59  mov         dword ptr [ebp-0C4h],ecx
00411A5F  jmp         maxs+3Ah (411A6Ah)
00411A61  mov         edx,dword ptr [b]
00411A64  mov         dword ptr [ebp-0C4h],edx
00411A6A  mov         eax,dword ptr [ebp-0C4h]
}
00411A70  pop         edi 
00411A71  pop         esi 
00411A72  pop         ebx 
00411A73  mov         esp,ebp
00411A75  pop         ebp 
00411A76  ret         8   
int __cdecl maxc(int a,int b)
{
00411A90  push        ebp 
00411A91  mov         ebp,esp
00411A93  sub         esp,0C4h
00411A99  push        ebx 
00411A9A  push        esi 
00411A9B  push        edi 
00411A9C  lea         edi,[ebp-0C4h]
00411AA2  mov         ecx,31h
00411AA7  mov         eax,0CCCCCCCCh
00411AAC  rep stos    dword ptr [edi]
 return a>b?a:b;
00411AAE  mov         eax,dword ptr [a]
00411AB1  cmp         eax,dword ptr [b]
00411AB4  jle         maxc+31h (411AC1h)
00411AB6  mov         ecx,dword ptr [a]
00411AB9  mov         dword ptr [ebp-0C4h],ecx
00411ABF  jmp         maxc+3Ah (411ACAh)
00411AC1  mov         edx,dword ptr [b]
00411AC4  mov         dword ptr [ebp-0C4h],edx
00411ACA  mov         eax,dword ptr [ebp-0C4h]
}
00411AD0  pop         edi 
00411AD1  pop         esi 
00411AD2  pop         ebx 
00411AD3  mov         esp,ebp
00411AD5  pop         ebp 
00411AD6  ret       
int __fastcall maxi(int a,int b)
{
00411AF0  push        ebp 
00411AF1  mov         ebp,esp
00411AF3  sub         esp,0DCh
00411AF9  push        ebx 
00411AFA  push        esi 
00411AFB  push        edi 
00411AFC  push        ecx 
00411AFD  lea         edi,[ebp-0DCh]
00411B03  mov         ecx,37h
00411B08  mov         eax,0CCCCCCCCh
00411B0D  rep stos    dword ptr [edi]
00411B0F  pop         ecx 
00411B10  mov         dword ptr [ebp-14h],edx
00411B13  mov         dword ptr [ebp-8],ecx
 return a>b?a:b;
00411B16  mov         eax,dword ptr [a]
00411B19  cmp         eax,dword ptr [b]
00411B1C  jle         maxi+39h (411B29h)
00411B1E  mov         ecx,dword ptr [a]
00411B21  mov         dword ptr [ebp-0DCh],ecx
00411B27  jmp         maxi+42h (411B32h)
00411B29  mov         edx,dword ptr [b]
00411B2C  mov         dword ptr [ebp-0DCh],edx
00411B32  mov         eax,dword ptr [ebp-0DCh]
}
00411B38  pop         edi 
00411B39  pop         esi 
00411B3A  pop         ebx 
00411B3B  mov         esp,ebp
00411B3D  pop         ebp 
00411B3E  ret   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值