当前位置: 首页 > news >正文

Linux栈帧

相关寄存器&指令

寄存器

rax(accumulator):return value
rbx(base)
rcx(count):4st argument
rdx(data):3st argument
rsi(source index):2st argument
rdi(destination index):1st argument
rbp(base pointer)
rsp(stack pointer)
r8:5st argument
r9:6st argument
r10-r15

rip(instruction pointer)
rflags

传参顺序

前6个参数使用rdi、rsi、rdx、rcx、r8、r9,第7个及以后参数使用栈

call指令

相当于执行以下指令:
push %rip
jmp

ret指令

相当于执行以下指令:
pop %rip

leave指令

相当于执行以下指令:
mov %rbp,%rsp
pop %rbp

nop指令

no operation,空指令

指令后缀

b(8位)
w(16位)
l(32位)
q(64位)

c代码

int g(int a, int b)
{return a + b;
}int f(int a, int b)
{return g(a, b);
}int main()
{f(1, 2);return 0;
}

编译&反汇编

gcc test.c -o test -g
objdump -dS test

c&汇编代码

00000000004004ed <g>:
int g(int a, int b)
{4004ed:	55                   	push   %rbp4004ee:	48 89 e5             	mov    %rsp,%rbp4004f1:	89 7d fc             	mov    %edi,-0x4(%rbp)4004f4:	89 75 f8             	mov    %esi,-0x8(%rbp)return a + b;4004f7:	8b 45 f8             	mov    -0x8(%rbp),%eax4004fa:	8b 55 fc             	mov    -0x4(%rbp),%edx4004fd:	01 d0                	add    %edx,%eax
}4004ff:	5d                   	pop    %rbp400500:	c3                   	retq0000000000400501 <f>:int f(int a, int b)
{400501:	55                   	push   %rbp400502:	48 89 e5             	mov    %rsp,%rbp400505:	48 83 ec 08          	sub    $0x8,%rsp400509:	89 7d fc             	mov    %edi,-0x4(%rbp)40050c:	89 75 f8             	mov    %esi,-0x8(%rbp)return g(a, b);40050f:	8b 55 f8             	mov    -0x8(%rbp),%edx400512:	8b 45 fc             	mov    -0x4(%rbp),%eax400515:	89 d6                	mov    %edx,%esi400517:	89 c7                	mov    %eax,%edi400519:	e8 cf ff ff ff       	callq  4004ed <g>
}40051e:	c9                   	leaveq40051f:	c3                   	retq0000000000400520 <main>:int main()
{400520:	55                   	push   %rbp400521:	48 89 e5             	mov    %rsp,%rbpf(1, 2);400524:	be 02 00 00 00       	mov    $0x2,%esi400529:	bf 01 00 00 00       	mov    $0x1,%edi40052e:	e8 ce ff ff ff       	callq  400501 <f>return 0;400533:	b8 00 00 00 00       	mov    $0x0,%eax
}400538:	5d                   	pop    %rbp400539:	c3                   	retq40053a:	66 0f 1f 44 00 00    	nopw   0x0(%rax,%rax,1)

栈帧变化过程

不同颜色代表不同函数的栈帧,从上到下依次是__libc_start_main、main、f、g的栈帧
下一级函数的rbp指向上一级函数的rbp
在这里插入图片描述

http://www.lryc.cn/news/504716.html

相关文章:

  • leetcode刷题——回溯算法(1)
  • 3D相框案例讲解(详细)
  • 制作安装包
  • P8615 拼接平方数 P8699 排列数
  • 【C语言】拆解C语言的编译过程
  • 【C++】青蛙跳跃问题解析与解法
  • 自动驾驶AVM环视算法--python版本的俯视TOP投影模式
  • Go 语言与时间拳击理论下的结对编程:开启高效研发编程之旅
  • Qt+OPC开发笔记(一):OPCUA介绍、open62541介绍、编译与基础环境Demo
  • ElasticSearch 常见故障解析与修复秘籍
  • 序列模型的使用示例
  • 对rust的全局变量使用drop方法
  • Node.js教程入门第一课:环境安装
  • Visual Studio 使用 GitHub Copilot 扩展
  • 【Qualcomm】IPQ5018获取TR069 WiFi 接口Stats状态方法
  • 数字营销咨询,照亮企业营销数字化每一步
  • 修改vscode中emmet中jsx和tsx语法中className的扩展符号从单引号到双引号 - HTML代码补全 - 单引号双引号
  • 【Cmake】
  • Flutter 内嵌 unity3d for android
  • sqlite加密-QtCipherSqlitePlugin 上
  • 正交投影 (Orthographic Projection) 详解
  • 盛元广通畜牧与水产品检验技术研究所LIMS系统
  • 三维空间刚体运动4-1:四元数表示变换(各形式相互转换加代码——下篇)
  • PyTorch如何通过 torch.unbind 和torch.stack动态调整张量的维度顺序
  • 【Unity3D】报错libil2cpp.so找不到问题
  • 事件冒泡机制详解
  • 红米Note 9 Pro5G刷LineageOS
  • 6.3.1 MR实战:计算总分与平均分
  • ARM循环程序和子程序设计
  • 静态路由、RIP、OSPF、BGP的区别