C++函数返回值是如何传出规则

C++函数返回值是如何传出规则

C++函数返回值是如何传出规则

1、返回值<=4字节,通过一个寄存器返回;
2、返回值>4 且<=8字节,通过两个寄存器返回;
3、返回值>8 字节,主调函数通过生成临时变量然后被调用函数将返回值写进临时变量;

代码示例

#include<stdio.h>
typedef struct NODE2
{
	int a[2];
}NODE2;
typedef struct NODE10
{
	int a[20];
}NODE10;

int sum(int a, int b)
{
	return a + b;
}


NODE2 func2(NODE2 mm)
{
	NODE2 mm1 = {0};
	return mm1;
}

NODE10 func10(NODE10 mm)
{
	NODE10 mm1 = { 0 };
	return mm1;
}

int main()
{
	int s = sum(10, 20);

	NODE2 s1;
	s1.a[0] = 0;
	NODE2 m = func2(s1);

	NODE10 s10;
	s10.a[0] = 0;
	NODE10 m = func10(s10);
}

汇编

	int s = sum(10, 20);
00EB1888  push        14h  
00EB188A  push        0Ah  
00EB188C  call        _sum (0EB1294h)  
00EB1891  add         esp,8  
00EB1894  mov         dword ptr [s],eax  //通过eax 传给s


	NODE2 m2 = func2(s1);
00EB18A7  mov         eax,dword ptr [ebp-14h]  
00EB18AA  push        eax  
00EB18AB  mov         ecx,dword ptr [s1]  
00EB18AE  push        ecx  
00EB18AF  call        _func2 (0EB10A0h)  
00EB18B4  add         esp,8  
00EB18B7  mov         dword ptr [m2],eax  
00EB18BA  mov         dword ptr [ebp-24h],edx  //通过eax edx返回

	NODE10 m10 = func10(s10);
00EB18CD  sub         esp,50h  
00EB18D0  mov         ecx,14h  
00EB18D5  lea         esi,[s10]  
00EB18D8  mov         edi,esp  
00EB18DA  rep movs    dword ptr es:[edi],dword ptr [esi]  
00EB18DC  lea         eax,[ebp-1F0h]  
00EB18E2  push        eax  
00EB18E3  call        _func10 (0EB13B1h)  
00EB18E8  add         esp,54h  
00EB18EB  mov         ecx,14h  
00EB18F0  mov         esi,eax  
00EB18F2  lea         edi,[m10]  
00EB18F8  rep movs    dword ptr es:[edi],dword ptr [esi]  //将返回值拷贝给临时量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值