反汇编四:函数返回值

内置类型

由eax寄存器保存返回值
在这里插入图片描述

结构体传值方式返回

在这里插入图片描述
由调用者的栈申请空间,接收返回值

struct big
{
   char s[20];
};

struct big fun()
{

  struct big b;
  b.s[0]='a';
......
  return b;
}
int main(int argc, char** argv)
{
  struct big b=fun();
}

如果main函数这样写

int main(int argc, char** argv)
{
  struct big b;
  b=fun();
}

main的栈预留2个big的空间。
低地址的为局部变量b。//struct big b;
高地址为预留空间接收返回值。
最后再拷贝一下//b=fun();

   0x0804846f <+14>:	sub    $0x48,%esp
24	  struct big b;
25	  b.s[0]='x';
   0x08048472 <+17>:	movb   $0x78,-0x1c(%ebp)  //struct big b
26	  b=fun();
   0x08048476 <+21>:	lea    -0x48(%ebp),%eax  //存放返回值的空间
   0x08048479 <+24>:	mov    %eax,(%esp)
   0x0804847c <+27>:	call   0x8048444 <fun()>
   0x08048481 <+32>:	sub    $0x4,%esp
   0x08048484 <+35>:	mov    -0x48(%ebp),%eax   //b=fun();
   0x08048487 <+38>:	mov    %eax,-0x1c(%ebp)   //返回值到b的拷贝
   0x0804848a <+41>:	mov    -0x44(%ebp),%eax
   0x0804848d <+44>:	mov    %eax,-0x18(%ebp)
   0x08048490 <+47>:	mov    -0x40(%ebp),%eax
   0x08048493 <+50>:	mov    %eax,-0x14(%ebp)
   0x08048496 <+53>:	mov    -0x3c(%ebp),%eax
   0x08048499 <+56>:	mov    %eax,-0x10(%ebp)
   0x0804849c <+59>:	mov    -0x38(%ebp),%eax
   0x0804849f <+62>:	mov    %eax,-0xc(%ebp)
   0x080484a2 <+65>:	mov    $0x0,%eax

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值