main.c
- #include <stdlib.h>
- #include <stdio.h>
- int add(int a, int b)
- {
- int c;
- c = a;
- c += b;
- return c;
- }
- int main()
- {
- int a, b, c;
- a = 0x12;
- b = 0x34;
- c = add(a, b);
- return 0;
- }
- .file "main.c"
- .text
- .globl add
- .type add, @function
- add:
- pushl %ebp
- movl %esp, %ebp
- subl $16, %esp
- movl 8(%ebp), %eax
- movl %eax, -4(%ebp)
- movl 12(%ebp), %eax
- addl %eax, -4(%ebp)
- movl -4(%ebp), %eax
- leave
- ret
- .size add, .-add
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- subl $24, %esp
- movl $18, -4(%ebp)
- movl $52, -8(%ebp)
- movl -8(%ebp), %eax
- movl %eax, 4(%esp)
- movl -4(%ebp), %eax
- movl %eax, (%esp)
- call add
- movl %eax, -12(%ebp)
- movl $0, %eax
- leave
- ret
- .size main, .-main
- .ident "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"
- .section .note.GNU-stack,"",@progbits
- (gdb) disassemble main
- Dump of assembler code for function main:
- 0x080483ab <+0>: push %ebp
- 0x080483ac <+1>: mov %esp,%ebp
- 0x080483ae <+3>: sub $0x18,%esp
- 0x080483b1 <+6>: movl $0x12,-0x4(%ebp)
- 0x080483b8 <+13>: movl $0x34,-0x8(%ebp)
- 0x080483bf <+20>: mov -0x8(%ebp),%eax
- 0x080483c2 <+23>: mov %eax,0x4(%esp)
- 0x080483c6 <+27>: mov -0x4(%ebp),%eax
- 0x080483c9 <+30>: mov %eax,(%esp)
- 0x080483cc <+33>: call 0x8048394 <add>
- 0x080483d1 <+38>: mov %eax,-0xc(%ebp)
- 0x080483d4 <+41>: mov $0x0,%eax
- 0x080483d9 <+46>: leave
- 0x080483da <+47>: ret
- End of assembler dump.
- (gdb) list
- 6 int c;
- 7 c = a;
- 8 c += b;
- 9 return c;
- 10 }
- 11
- 12 int main()
- 13 {
- 14 int a, b, c;
- 15 a = 0x12;
- 16 b = 0x34;
- 17 c = add(a, b);
- 18 return 0;
- 19 }
- (gdb) break 17
- Breakpoint 1 at 0x80483bf: file main.c, line 17.
- (gdb) run
- Starting program: /home/my_project/test/main
- Breakpoint 1, main () at main.c:17
- 17 c = add(a, b);
- Missing separate debuginfos, use: debuginfo-install glibc-2.12.90-17.i686
- (gdb) info registers esp
- esp 0xbffff1e0 0xbffff1e0
- (gdb) info registers ebp
- ebp 0xbffff1f8 0xbffff1f8
- (gdb) x/6xw 0xbffff1e0
- 0xbffff1e0: 0x080483e0 0x080482e0 0x080483eb 0x00394ff4
- 0xbffff1f0: 0x00000034 0x00000012
- (gdb) step
- add (a=18, b=52) at main.c:7
- 7 c = a;
- (gdb) x/6xw 0xbffff1e0
- 0xbffff1e0: 0x00000012 0x00000034 0x080483eb 0x00394ff4
- 0xbffff1f0: 0x00000034 0x00000012
- (gdb) info registers esp
- esp 0xbffff1c8 0xbffff1c8
- (gdb) info registers ebp
- ebp 0xbffff1d8 0xbffff1d8
- (gdb) tep
- Undefined command: "tep". Try "help".
- (gdb) step
- 8 c += b;
- (gdb) step
- 9 return c;
- (gdb) x/16xw 0xbffff1c0
- 0xbffff1c0: 0x00235f95 0x08049624 0xbffff1f8 0x080483f9
- 0xbffff1d0: 0x08048215 0x00000046 0xbffff1f8 0x080483d1
- 0xbffff1e0: 0x00000012 0x00000034 0x080483eb 0x00394ff4
- 0xbffff1f0: 0x00000034 0x00000012 0xbffff278 0x0021ce16
- (gdb)step
- 10 }
- (gdb) step
- main () at main.c:18
- 18 return 0;
- (gdb) info registers esp
- esp 0xbffff1e0 0xbffff1e0
- (gdb) info registers ebp
- ebp 0xbffff1f8 0xbffff1f8
- (gdb)

红色是main函数分配的堆栈,绿色是add函数分配的堆栈
