说完利用堆栈传递参数了,下面该说说使用寄存器传递参数的话题了.寄存器传递传输的方式并没有一个标准,所有与平台相关的方法都是由IDE(也就是编译器)开发人员制定的.尽管没有统一的标准,但绝大多数编译器提供商都在不对兼容性声明的情况下,遵循相应的规范,吉fastcall规范._fastcall顾名思义,特点就是快,因为他是靠寄存器来传递参数的.
不同编译器实现的fastcall稍有不同,如Microsoft Visual C++编译器采用fastcall规范传递参数时,最左边的两个不大于4个字节(DWORD)的参数分别放在ecx和edx寄存器.当寄存器用完后,就要使用堆栈,其余参数仍然按照从右到左的顺序压入堆栈,被调用的函数在返回前清理传送参数的堆栈.浮点值员指针和int64类型总是通过对战来传递的.而Borland Delphi/C++编译器总是通过将寄存器来传递参数的,其最左边的三个不大于4个字节(DWORD)的参数分别放在eax edx 和 ecx寄存器,寄存器用完后,参数按照从左到右的PASCAL方式来压栈.另外一款编译器Watcom C总是通过寄存器来传递参数的,严格为每一个参数分配一个寄存器,默认时第一个参数用eax,第二个参数用edx,第三个参数用ebx,第四个参数用 ecx,如果寄存器用完了,就会用堆栈来传递参数.Watcom C可以由程序员指定任意一个寄存器传递参数,因此,其参数实际上可能通过任何寄存器进行传递.
来看一个手动指定fastcall调用参数的实例:
C源码如下:
- int _fastcall Add(char,long,int,int);
- main(void)
- {
- Add(1,2,3,4);
- return 0;
- }
- int _fastcall Add(char</