多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 概述 学习汇编有段时间,看着汇编代码的时候还是懵懵懂懂的,之道看到别人根据图文来描述一段汇编代码才豁然开朗。 具体问题是因为: 1.没有记忆点 2.知道指令的意思,不知道怎么使用 3.知道栈是先入后出,但就是跟汇编代码联想不起来 ## 写一段C代码,转成汇编 C代码 ``` //stack.c #include <stdio.h> int sum(int a, int b) { int c = a + b; return c; } int main() { int a = sum(2, 3); printf("sum1: %d\n", a); return 1; } ``` 编译成可执行文件,查看汇编 ``` $ gcc -o stack stack.c $ objdump -S stack ``` >也可以使用 gcc -S stack.c 截取其中的一段 ``` 0000000000400526 <sum>: 400526: 55 push %rbp 400527: 48 89 e5 mov %rsp,%rbp 40052a: 89 7d ec mov %edi,-0x14(%rbp) 40052d: 89 75 e8 mov %esi,-0x18(%rbp) 400530: 8b 55 ec mov -0x14(%rbp),%edx 400533: 8b 45 e8 mov -0x18(%rbp),%eax 400536: 01 d0 add %edx,%eax 400538: 89 45 fc mov %eax,-0x4(%rbp) 40053b: 8b 45 fc mov -0x4(%rbp),%eax 40053e: 5d pop %rbp 40053f: c3 retq 0000000000400540 <main>: 400540: 55 push %rbp 400541: 48 89 e5 mov %rsp,%rbp 400544: 48 83 ec 10 sub $0x10,%rsp 400548: be 03 00 00 00 mov $0x3,%esi 40054d: bf 02 00 00 00 mov $0x2,%edi 400552: e8 cf ff ff ff callq 400526 <sum> 400557: 89 45 fc mov %eax,-0x4(%rbp) 40055a: 8b 45 fc mov -0x4(%rbp),%eax 40055d: 89 c6 mov %eax,%esi 40055f: bf 04 06 40 00 mov $0x400604,%edi 400564: b8 00 00 00 00 mov $0x0,%eax 400569: e8 92 fe ff ff callq 400400 <printf@plt> 40056e: b8 01 00 00 00 mov $0x1,%eax 400573: c9 leaveq 400574: c3 retq 400575: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 40057c: 00 00 00 40057f: 90 nop ``` ## 函数的调用 ``` 400540: 55 push %rbp 400541: 48 89 e5 mov %rsp,%rbp 400544: 48 83 ec 10 sub $0x10,%rsp 400548: be 03 00 00 00 mov $0x3,%esi 40054d: bf 02 00 00 00 mov $0x2,%edi ``` ## 函数的返回 参考: https://segmentfault.com/a/1190000007977460 https://blog.csdn.net/tell_me_404/article/details/107597875