常见的函数调用方式有__cdecl、__stdcall、__fastcall(前面为双下划线)。
1.__cdecl:
按从右至左的顺序压参数入栈,由调用者把参数弹出栈.
int __cdecl func1(int a,int b,int c) { return a+b+c; } int main() { //函数调用 func1(1,2,3); return 0; }
a.压栈顺序
//main
.text:00401108 push 3 ; int .text:0040110A push 2 ; int .text:0040110C push 1 ; int .text:0040110E call func1(int,int,int)
b.清除栈上参数
//main
. .text:00401113 add esp, 0Ch
2.__stdcall:
按从右至左的顺序压参数入栈,由被调用者把参数弹出栈
#include <stdio.h> int __stdcall func2(int a,int b,int c) { return a+b+c; } int main() { func2(1,2,3); return 0; }
a.压栈顺序
//main
.text:00401116 push 3 ; int .text:00401118 push 2 ; int .text:0040111A push 1 ; int .text:0040111C call func2(int,int,int)
b.清除栈上参数
//func2
text:00401097 retn 0Ch
3.__fastcall:
从右到左,前两个参数放在ecx, edx,由调用者把参数弹出栈.
#include <stdio.h>
int __fastcall func3(int a,int b,int c)
{
return a+b+c;
}
int main()
{
func3(1,2,3);
return 0;
}
a.压栈顺序
//main
.text:00401121 push 3 ; int .text:00401123 mov edx, 2 ; int .text:00401128 mov ecx, 1 ; int .text:0040112D call func3(int,int,int)
b 清除栈上参数
//func3
.text:004010DF retn 4