C与汇编:简单例子
简单例子
- 源码
#include <stdio.h>
int TEST_Add(int a, int b)
{
return a + b;
}
int main()
{
int a = 0;
int b = 1;
int c = TEST_Add(a, b);
printf(" \nc:%u", c);
return 0;
}
- 汇编
Dump of assembler code for function main:
5 return a + b;
6 }
7
8 int main()
9 {
0x00000000004078c0 <+0>: sub $0x28,%rsp
0x00000000004078c4 <+4>: callq 0x401660 <__main>
10 int a = 0;
11 int b = 1;
12 int c = TEST_Add(a, b);
13
14 printf(" \nc:%u", c);
0x00000000004078c9 <+9>: mov $0x1,%edx
0x00000000004078ce <+14>: lea 0x172b(%rip),%rcx # 0x409000
0x00000000004078d5 <+21>: callq 0x401550 <printf>
15 return 0;
0x00000000004078da <+26>: xor %eax,%eax
0x00000000004078dc <+28>: add $0x28,%rsp
0x00000000004078e0 <+32>: retq
End of assembler dump.
Dump of assembler code for function TEST_Add:
4 {
5 return a + b;
0x00000000004015a0 <+0>: lea (%rcx,%rdx,1),%eax
0x00000000004015a3 <+3>: retq
0x00000000004015a4 <+4>: nop
0x00000000004015a5 <+5>: nop
常用命令
gcc -g -std=c99 'main.c' -o 'main.exe' -Wall -O2 -m64 -lm -static-libgcc -fexec-charset=GBK -D__USE_MINGW_ANSI_STDIO
gcc -g -std=c99 asm2.c -o asm2.o
// 编译源码(-g:debug版本)
gdb -q ./main.exe |tee main.txt
// gdb调试(-q:不输出gdb版本信息;tee:将输出结果保存到ams2.txt)
disass /m main
// 查看汇编(/m:输出对应的源码)
参考资料:https://blog.csdn.net/qq_31865983/article/details/91453963
程序是如何运行的
剑指 Offer