Several common calling conventions:
Calling convention | Parameter stacking order | Balance stack |
---|---|---|
__cdecl | Stack from right to left | The caller cleans up the stack (who calls, who balances the stack, also known as the external flat stack) |
__stdcall | Stack from right to left | Clean up the stack by itself (stack balancing within the function, also known as internal flat stack) |
__fastcall | ECX/EDX transmits the first two | Clean up the stack by itself (stack balancing within the function, also known as internal flat stack) |
1、c,c++默认的调用约定
int __cdecl Plus(int a, int b)
{
return a+b;
}
//汇编
push 2
push 1
call @ILT+15(Plus) (00401014)
add esp,8 //这里在函数外部进行堆栈平衡
2、
int __stdcall Plus(int a, int b)
{
return a+b;
}
//汇编
push 2
push 1
call @ILT+10(Plus) (0040100f)
函数内部:
ret 8 //这句是在call内部,即函数内部进行的堆栈平衡
3、fastcall,参数会放在寄存器中,运算速度会非常快,如果参数超过两个,那其中一个参数就会被放入到内存中,达不到提升速度的效果。因此fastcall 只有两个参数时候才有优化效果
int __fastcall Plus(int a, int b)
{
return a+b;
}
//汇编
mov edx,2
mov ecx,1
call @ILT+0(Plus) (00401005)
函数内部:
ret
4、int __fastcall Plus4(int a, int b,int c,int d)
{
return a+b+c+d;
}
push 4
push 3
mov edx,2
mov ecx,1
call @ILT+5(Plus) (0040100a)
函数内部:
ret 8