C中的调用约定

常见的几种调用约定:

调用约定 参数压栈顺序 平衡堆栈
__cdecl 从右至左入栈 调用者清理栈(谁调用,谁做堆栈平衡,也称外平栈)
__stdcall 从右至左入栈 自身清理堆栈 (函数内部进行堆栈平衡,也称内平栈)
__fastcall ECX/EDX传送前两个 自身清理堆栈(函数内部进行堆栈平衡,也称内平栈)
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			

猜你喜欢

转载自blog.csdn.net/qq1119278087/article/details/83796733
今日推荐