测试-1-ch03
- 任务详情
- 通过输入
gcc -S -o main.s main.c
,将下面c程序“week04学号.c”编译成汇编代码
int g(int x){ return x+3; } int f(int x){ int i = 学号后两位; return g(x)+i; } int main(void){ return f(8)+1; }
- 删除汇编代码中 . 开头的代码,提交f函数的汇编代码截图,图中用矩形标出函数栈帧的形成和销毁的代码
- 知识点
- 测试截图
![image](https://i-blog.csdnimg.cn/blog_migrate/7247de5d0675e0ea9fa0823d59ff12f7.png)
测试-2-ch03
- 任务详情
- 通过输入
gcc -S -o main.s main.c
,将下面c程序“week04学号.c”编译成汇编代码
int g(int x){ return x+3; } int f(int x){ int i = 学号后两位; return g(x)+i; } int main(void){ return f(8)+1; }
- 参考http://www.cnblogs.com/lxm20145215----/p/5982554.html,使用
gdb
跟踪汇编代码,在纸上画出f中每一条语句引起的eip(rip),ebp(rbp),esp(rsb),eax(rax)的值和栈的变化情况。提交照片,要有学号信息。
- 知识点
- 参考GDB调试汇编堆栈过程分析,使用
gcc - g XX.c -o XX -m32
指令在64位的机器上产生32位汇编 - 使用
gdb XX
指令进入gdb调试器 - gdb相关指令
- r:运行
- b n:在n行设置断点
- i r:查看各寄存器的值
- disassemble:获取汇编代码
- 过程
- 查看f函数的汇编代码、
![image](https://i-blog.csdnimg.cn/blog_migrate/0c3ab48c4fc092e7ce3e96d7be57cf2a.png)
- 入栈
![image](https://i-blog.csdnimg.cn/blog_migrate/23a11ed88126f61c1b92ab89bdf29c0c.png)
- 将上一个函数的基址入栈,从当前%esp开始作为新基址
![image](https://i-blog.csdnimg.cn/blog_migrate/f34dcd0316154bdcd1aaefbc38672c55.png)
- 为传参做准备
![image](https://i-blog.csdnimg.cn/blog_migrate/d6be4f480567f14a13f9ff66e68ab39e.png)
- 实参入栈
![image](https://i-blog.csdnimg.cn/blog_migrate/9e5fa397aa65ede374fecd24cfb0fa1a.png)
- 计算short+int
![image](https://i-blog.csdnimg.cn/blog_migrate/e80b8e900cf6416f72a962dc329bb6f8.png)
- pop %ebp指令将栈顶弹到%ebp中,同时%esp增加4字节
![image](https://i-blog.csdnimg.cn/blog_migrate/65ed44e8b186d11c77bdcf0131a2859e.png)
- ret指令将栈顶弹给%eip
![image](https://i-blog.csdnimg.cn/blog_migrate/696cdbeab6926114cb5e80c2b9314c8a.png)
- 用leave指令恢复
![image](https://i-blog.csdnimg.cn/blog_migrate/ddda995159b5746d9d387b5b629cb4b9.png)
- 变化情况
push %ebp | 0x80483e6 | 0xbfffef58 | 0xbfffef50 | 0xb7fbcdbc | 0x8048411 0x8 0x0 |
mov %esp,%ebp | 0x80483e7 | 0xbfffef58 | 0xbfffef4c | 0xb7fbcdbc | 0xbfffef58 0x8048411 0x8 0x0 |
sub $0x10,%esp | 0x80483e9 | 0xbfffef4c | 0xbfffef4c | 0xb7fbcdbc | 0xbfffef58 0x8048411 0x8 0x0 |
movl $0xc,-0x4(%ebp) | 0x80483ec | 0xbfffef4c | 0xbfffef3c | 0xb7fbcdbc | 0x8048441 0xb7fbb3dc 0x80481ec 0x8048429 |
pushl 0x8(%ebp) | 0x80483f3 | 0xbfffef4c | 0xbfffef3c | 0xb7fbcdbc | 0x8048441 0xb7fbb3dc 0x80481ec 0x1e |
call 0x80483db | 0x80483f6 | 0xbfffef4c | 0xbfffef38 | 0xb7fbcdbc | 0x8 0x8048441 0xb7fbb3dc 0x80481ec |
add $0x4,%esp | 0x80483fb | 0xbfffef4c | 0xbfffef38 | 0xb 11 | 0x8 0x8048441 0xb7fbb3dc 0x80481ec |
mov %eax,%edx | 0x80483fe | 0xbfffef4c | 0xbfffef3c | 0xb 11 | 0x8048441 0xb7fbb3dc 0x80481ec 0x1e |
mov -0x4(%ebp),%eax | 0x8048400 | 0xbfffef4c | 0xbfffef3c | 0xb 11 | 0x8048441 0xb7fbb3dc 0x80481ec 0x1e |
add %edx,%eax | 0x8048403 | 0xbfffef4c | 0xbfffef3c | 0x1e 30 | 0x8048441 0xb7fbb3dc 0x80481ec 0x1e |
leave | 0x8048406 | 0xbfffef58 | 0xbfffef50 | 0x29 41 | 0x8048411 0x8 0x0 |
ret | 0x8048417 | 0xbfffef58 | 0xbfffef58 | 0x2a 42 | 0x0 |
转载于:https://www.cnblogs.com/besty-zyx/p/9859360.html