函数调用约定

来源:互联网 发布:火鹊网络法人 编辑:程序博客网 时间:2024/05/19 09:17

#include "stdafx.h"

void __cdecl cmax(const int &a, const int &b)
{
 cout << (a>b ? a:b) <<" is greater!" <<endl;
}

 

 void __stdcall smax(const int &a, const int &b)
{
 cout << (a>b ? a:b) <<" is greater!" <<endl;
}

 

 void __fastcall fmax(const int &a, const int &b, const int &c)
 {
  if(a >b)
  {
  if(a >c)
   cout <<a <<" is max" <<endl;
  else
   cout <<c <<" is max" <<endl;
  }
  else
  {
   if(b >c)
    cout <<b <<" is max" <<endl;
   else
    cout <<c <<" is max" <<endl;
  }
 }

 

int main()
{
 /************************************************************************/
 /*
  阐述调用约定的区别
 
  __stdcall与__cdecl:函数调用者在调用函数时先将函数的所有参数按从右到左的顺序依次压入堆栈,然后调用函数
  __fastcall:它用ECX和EDX传送前两个双字(DWORD)或更小的参数,剩下的参数仍旧自右向左压栈传送

 */
 /************************************************************************/

 int x=10;
 int y=20;
 int z=30;

 

 /*
  0123166C  lea         eax,[y]
  0123166F  push        eax 
  01231670  lea         ecx,[x]
  01231673  push        ecx 
  01231674  call        cmax (1231073h)
  01231679  add         esp,8
    //函数调用者要负责将所有参数弹出堆栈
 */
 cmax(x,y);

 

 /*
  0123167C  lea         eax,[y]
  0123167F  push        eax 
  01231680  lea         ecx,[x]
  01231683  push        ecx 
  01231684  call        smax (1231140h)
 //__stdcall是由被调用函数将参数弹出堆栈的
 */
 smax(x,y);

 

 /*
  00F11670  lea         eax,[z]
  00F11673  push        eax 
  00F11674  lea         edx,[y]
  00F11677  lea         ecx,[x]
   //用ECX和EDX传送前两个双字(DWORD)或更小的参数
  00F1167A  call        fmax (0F11267h)
 */
 fmax(x,y,z);
}

原创粉丝点击