[stafford@localhost chap3]$ nano code.c int accum = 0; int sum(int x, int y) { int t = x + y; accum += t; return t; } [stafford@localhost chap3]$ ls code.c [stafford@localhost chap3]$ gcc -O2 -S code.c [stafford@localhost chap3]$ ls code.c code.s [stafford@localhost chap3]$ more code.s $ more code.s .file "code.c" .text .p2align 4,,15 .globl sum .type sum, @function sum: pushl %ebp movl %esp, %ebp movl 12(%ebp), %eax addl 8(%ebp), %eax addl %eax, accum popl %ebp ret .size sum, .-sum .globl accum .bss .align 4 .type accum, @object .size accum, 4 accum: .zero 4 .ident "GCC: (GNU) 4.1.2 20070925 (Red Hat 4.1.2-27)" .section .note.GNU-stack,"",@progbits $ [stafford@localhost chap3]$ gcc -O2 -c code.c [stafford@localhost chap3]$ ls code.c code.o code.s [stafford@localhost chap3]$ od -tx1 -Ax code.o 000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 000010 01 00 03 00 01 00 00 00 00 00 00 00 00 00 00 00 000020 cc 00 00 00 00 00 00 00 34 00 00 00 00 00 28 00 000030 0a 00 07 00 00 00 00 00 00 00 00 00 00 00 00 00 000040 55 89 e5 8b 45 0c 03 45 08 01 05 00 00 00 00 5d first 16 000050 c3 00 00 00 00 47 43 43 3a 20 28 47 4e 55 29 20 last 1 000060 34 2e 31 2e 32 20 32 30 30 37 30 39 32 35 20 28 000070 52 65 64 20 48 61 74 20 34 2e 31 2e 32 2d 32 37 000080 29 00 00 2e 73 79 6d 74 61 62 00 2e 73 74 72 74 000090 61 62 00 2e 73 68 73 74 72 74 61 62 00 2e 72 65 0000a0 6c 2e 74 65 78 74 00 2e 64 61 74 61 00 2e 62 73 0000b0 73 00 2e 63 6f 6d 6d 65 6e 74 00 2e 6e 6f 74 65 0000c0 2e 47 4e 55 2d 73 74 61 63 6b 00 00 00 00 00 00 0000d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 000300 0b 00 00 00 01 08 00 00 000308 [stafford@localhost chap3]$ [stafford@localhost chap3]$ ls code.c code.o code.s [stafford@localhost chap3]$ gdb code.o GNU gdb Red Hat Linux (6.6-15.fc7rh) ... Using host libthread_db library "/lib/libthread_db.so.1". (gdb) x/17xb sum 0x0 : 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x8 : 0x08 0x01 0x05 0x00 0x00 0x00 0x00 0x5d 0x10 : 0xc3 (gdb) q [stafford@localhost chap3]$ ls code.c code.o code.s [stafford@localhost chap3]$ objdump -d code.o code.o: file format elf32-i386 Disassembly of section .text: 00000000 : 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 8b 45 0c mov 0xc(%ebp),%eax 6: 03 45 08 add 0x8(%ebp),%eax 9: 01 05 00 00 00 00 add %eax,0x0 f: 5d pop %ebp 10: c3 ret [stafford@localhost chap3]$ [stafford@localhost chap3]$ nano main.c int main() { return sum(1, 3); } [stafford@localhost chap3]$ gcc -O2 -o prog code.o main.c [stafford@localhost chap3]$ ls code.c code.o code.s main.c prog [stafford@localhost chap3]$ objdump -d prog prog: file format elf32-i386 Disassembly of section .init: 08048250 <_init>: ... Disassembly of section .plt: 08048268 <__gmon_start__@plt-0x10>: ... Disassembly of section .text: 080482a0 <_start>: 80482a0: 31 ed xor %ebp,%ebp ... 080482c4 : ... 080482f0 <__do_global_dtors_aux>: ... 08048350 : ... 08048380 : 8048380: 55 push %ebp 8048381: 89 e5 mov %esp,%ebp 8048383: 8b 45 0c mov 0xc(%ebp),%eax 8048386: 03 45 08 add 0x8(%ebp),%eax 8048389: 01 05 14 96 04 08 add %eax,0x8049614 804838f: 5d pop %ebp 8048390: c3 ret 8048391: 90 nop ... 804839f: 90 nop 080483a0
: 80483a0: 8d 4c 24 04 lea 0x4(%esp),%ecx 80483a4: 83 e4 f0 and $0xfffffff0,%esp 80483a7: ff 71 fc pushl 0xfffffffc(%ecx) 80483aa: 55 push %ebp 80483ab: 89 e5 mov %esp,%ebp 80483ad: 51 push %ecx 80483ae: 83 ec 14 sub $0x14,%esp 80483b1: c7 44 24 04 03 00 00 movl $0x3,0x4(%esp) 80483b8: 00 80483b9: c7 04 24 01 00 00 00 movl $0x1,(%esp) 80483c0: e8 bb ff ff ff call 8048380 80483c5: 83 c4 14 add $0x14,%esp 80483c8: 59 pop %ecx 80483c9: 5d pop %ebp 80483ca: 8d 61 fc lea 0xfffffffc(%ecx),%esp 80483cd: c3 ret 80483ce: 90 nop 80483cf: 90 nop 080483d0 <__libc_csu_fini>: ... 080483e0 <__libc_csu_init>: ... 08048449 <__i686.get_pc_thunk.bx>: ... 08048450 <__do_global_ctors_aux>: Disassembly of section .fini: 08048478 <_fini>: ... [stafford@localhost chap3]$ [stafford@localhost chap3]$ nano simple.c int simple(int *xp, int y) { int t = *xp + y; *xp = t; return t; } [stafford@localhost chap3]$ gcc -O2 -S simple.c [stafford@localhost chap3]$ ls code.c code.o code.s main.c prog simple.c simple.s [stafford@localhost chap3]$ more simple.s .file "simple.c" .text .p2align 4,,15 .globl simple .type simple, @function simple: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax addl (%edx), %eax movl %eax, (%edx) popl %ebp ret .size simple, .-simple .ident "GCC: (GNU) 4.1.2 20070925 (Red Hat 4.1.2-27)" .section .note.GNU-stack,"",@progbits [stafford@localhost chap3]$ nano simple.txt simple: pushl %ebp Save frame pointer movl %esp, %ebp Create new frame pointer movl 8(%ebp), %edx Get xp (an address) movl 12(%ebp), %eax Get y (an int) addl (%edx), %eax Add *xp to copy of y movl %eax, (%edx) Store sum in *xp popl %ebp Reset frame pointer ret Return [stafford@localhost chap3]$