Bryand and O'Hallaron Chapter 3 Section 2 Code Examples colosus:~/bando/chap3/sec2>more code.c int accum = 0; int sum(int x, int y) { int t = x + y; accum += t; return t; } ------------------------------------------------------- colosus:~/bando/chap3/sec2>gcc -O2 -S code.c colosus:~/bando/chap3/sec2>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 20070502 (Red Hat 4.1.2-12)" .section .note.GNU-stack,"",@progbits colosus:~/bando/chap3/sec2> -------------------------------------------------------- colosus:~/bando/chap3/sec2>gcc -O2 -c code.c colosus:~/bando/chap3/sec2>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 # mov %ebp,%esp in book, but not lucas f: 5d pop %ebp 10: c3 ret colosus:~/bando/chap3/sec2> ------------------------------------------------------- colosus:~/bando/chap3/sec2>more main.c int main() { return sum(1,3); } colosus:~/bando/chap3/sec2>gcc -O2 -o prog code.o main.c colosus:~/bando/chap3/sec2>objdump -d prog prog: file format elf32-i386 Disassembly of section .init: 08048250 <_init>: 8048250: 55 push %ebp 8048251: 89 e5 mov %esp,%ebp 8048253: 83 ec 08 sub $0x8,%esp 8048256: e8 69 00 00 00 call 80482c4 804825b: e8 f0 00 00 00 call 8048350 8048260: e8 eb 01 00 00 call 8048450 <__do_global_ctors_aux> 8048265: c9 leave 8048266: c3 ret Disassembly of section .plt: 08048268 <__gmon_start__@plt-0x10>: 8048268: ff 35 f8 95 04 08 pushl 0x80495f8 804826e: ff 25 fc 95 04 08 jmp *0x80495fc 8048274: 00 00 add %al,(%eax) ... 08048278 <__gmon_start__@plt>: 8048278: ff 25 00 96 04 08 jmp *0x8049600 804827e: 68 00 00 00 00 push $0x0 8048283: e9 e0 ff ff ff jmp 8048268 <_init+0x18> 08048288 <__libc_start_main@plt>: 8048288: ff 25 04 96 04 08 jmp *0x8049604 804828e: 68 08 00 00 00 push $0x8 8048293: e9 d0 ff ff ff jmp 8048268 <_init+0x18> Disassembly of section .text: 080482a0 <_start>: 80482a0: 31 ed xor %ebp,%ebp 80482a2: 5e pop %esi 80482a3: 89 e1 mov %esp,%ecx 80482a5: 83 e4 f0 and $0xfffffff0,%esp 80482a8: 50 push %eax 80482a9: 54 push %esp 80482aa: 52 push %edx 80482ab: 68 d0 83 04 08 push $0x80483d0 80482b0: 68 e0 83 04 08 push $0x80483e0 80482b5: 51 push %ecx 80482b6: 56 push %esi 80482b7: 68 a0 83 04 08 push $0x80483a0 80482bc: e8 c7 ff ff ff call 8048288 <__libc_start_main@plt> 80482c1: f4 hlt 80482c2: 90 nop 80482c3: 90 nop 080482c4 : 80482c4: 55 push %ebp 80482c5: 89 e5 mov %esp,%ebp 80482c7: 53 push %ebx 80482c8: 83 ec 04 sub $0x4,%esp 80482cb: e8 00 00 00 00 call 80482d0 80482d0: 5b pop %ebx 80482d1: 81 c3 24 13 00 00 add $0x1324,%ebx 80482d7: 8b 93 fc ff ff ff mov 0xfffffffc(%ebx),%edx 80482dd: 85 d2 test %edx,%edx 80482df: 74 05 je 80482e6 80482e1: e8 92 ff ff ff call 8048278 <__gmon_start__@plt> 80482e6: 58 pop %eax 80482e7: 5b pop %ebx 80482e8: c9 leave 80482e9: c3 ret 80482ea: 90 nop 80482eb: 90 nop 80482ec: 90 nop 80482ed: 90 nop 80482ee: 90 nop 80482ef: 90 nop 080482f0 <__do_global_dtors_aux>: 80482f0: 55 push %ebp 80482f1: 89 e5 mov %esp,%ebp 80482f3: 53 push %ebx 80482f4: 83 ec 04 sub $0x4,%esp 80482f7: 80 3d 10 96 04 08 00 cmpb $0x0,0x8049610 80482fe: 75 3f jne 804833f <__do_global_dtors_aux+0x4f> 8048300: b8 20 95 04 08 mov $0x8049520,%eax 8048305: 2d 1c 95 04 08 sub $0x804951c,%eax 804830a: c1 f8 02 sar $0x2,%eax 804830d: 8d 58 ff lea 0xffffffff(%eax),%ebx 8048310: a1 0c 96 04 08 mov 0x804960c,%eax 8048315: 39 c3 cmp %eax,%ebx 8048317: 76 1f jbe 8048338 <__do_global_dtors_aux+0x48> 8048319: 8d b4 26 00 00 00 00 lea 0x0(%esi),%esi 8048320: 83 c0 01 add $0x1,%eax 8048323: a3 0c 96 04 08 mov %eax,0x804960c 8048328: ff 14 85 1c 95 04 08 call *0x804951c(,%eax,4) 804832f: a1 0c 96 04 08 mov 0x804960c,%eax 8048334: 39 c3 cmp %eax,%ebx 8048336: 77 e8 ja 8048320 <__do_global_dtors_aux+0x30> 8048338: c6 05 10 96 04 08 01 movb $0x1,0x8049610 804833f: 83 c4 04 add $0x4,%esp 8048342: 5b pop %ebx 8048343: 5d pop %ebp 8048344: c3 ret 8048345: 8d 74 26 00 lea 0x0(%esi),%esi 8048349: 8d bc 27 00 00 00 00 lea 0x0(%edi),%edi 08048350 : 8048350: 55 push %ebp 8048351: 89 e5 mov %esp,%ebp 8048353: 83 ec 08 sub $0x8,%esp 8048356: a1 24 95 04 08 mov 0x8049524,%eax 804835b: 85 c0 test %eax,%eax 804835d: 74 12 je 8048371 804835f: b8 00 00 00 00 mov $0x0,%eax 8048364: 85 c0 test %eax,%eax 8048366: 74 09 je 8048371 8048368: c7 04 24 24 95 04 08 movl $0x8049524,(%esp) 804836f: ff d0 call *%eax 8048371: c9 leave 8048372: c3 ret 8048373: 90 nop 8048374: 90 nop 8048375: 90 nop 8048376: 90 nop 8048377: 90 nop 8048378: 90 nop 8048379: 90 nop 804837a: 90 nop 804837b: 90 nop 804837c: 90 nop 804837d: 90 nop 804837e: 90 nop 804837f: 90 nop 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 8048392: 90 nop 8048393: 90 nop 8048394: 90 nop 8048395: 90 nop 8048396: 90 nop 8048397: 90 nop 8048398: 90 nop 8048399: 90 nop 804839a: 90 nop 804839b: 90 nop 804839c: 90 nop 804839d: 90 nop 804839e: 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>: 80483d0: 55 push %ebp 80483d1: 89 e5 mov %esp,%ebp 80483d3: 5d pop %ebp 80483d4: c3 ret 80483d5: 8d 74 26 00 lea 0x0(%esi),%esi 80483d9: 8d bc 27 00 00 00 00 lea 0x0(%edi),%edi 080483e0 <__libc_csu_init>: 80483e0: 55 push %ebp 80483e1: 89 e5 mov %esp,%ebp 80483e3: 57 push %edi 80483e4: 56 push %esi 80483e5: 53 push %ebx 80483e6: e8 5e 00 00 00 call 8048449 <__i686.get_pc_thunk.bx> 80483eb: 81 c3 09 12 00 00 add $0x1209,%ebx 80483f1: 83 ec 1c sub $0x1c,%esp 80483f4: e8 57 fe ff ff call 8048250 <_init> 80483f9: 8d 83 20 ff ff ff lea 0xffffff20(%ebx),%eax 80483ff: 89 45 f0 mov %eax,0xfffffff0(%ebp) 8048402: 8d 83 20 ff ff ff lea 0xffffff20(%ebx),%eax 8048408: 29 45 f0 sub %eax,0xfffffff0(%ebp) 804840b: c1 7d f0 02 sarl $0x2,0xfffffff0(%ebp) 804840f: 8b 55 f0 mov 0xfffffff0(%ebp),%edx 8048412: 85 d2 test %edx,%edx 8048414: 74 2b je 8048441 <__libc_csu_init+0x61> 8048416: 31 ff xor %edi,%edi 8048418: 89 c6 mov %eax,%esi 804841a: 8d b6 00 00 00 00 lea 0x0(%esi),%esi 8048420: 8b 45 10 mov 0x10(%ebp),%eax 8048423: 83 c7 01 add $0x1,%edi 8048426: 89 44 24 08 mov %eax,0x8(%esp) 804842a: 8b 45 0c mov 0xc(%ebp),%eax 804842d: 89 44 24 04 mov %eax,0x4(%esp) 8048431: 8b 45 08 mov 0x8(%ebp),%eax 8048434: 89 04 24 mov %eax,(%esp) 8048437: ff 16 call *(%esi) 8048439: 83 c6 04 add $0x4,%esi 804843c: 39 7d f0 cmp %edi,0xfffffff0(%ebp) 804843f: 75 df jne 8048420 <__libc_csu_init+0x40> 8048441: 83 c4 1c add $0x1c,%esp 8048444: 5b pop %ebx 8048445: 5e pop %esi 8048446: 5f pop %edi 8048447: 5d pop %ebp 8048448: c3 ret 08048449 <__i686.get_pc_thunk.bx>: 8048449: 8b 1c 24 mov (%esp),%ebx 804844c: c3 ret 804844d: 90 nop 804844e: 90 nop 804844f: 90 nop 08048450 <__do_global_ctors_aux>: 8048450: 55 push %ebp 8048451: 89 e5 mov %esp,%ebp 8048453: 53 push %ebx 8048454: bb 14 95 04 08 mov $0x8049514,%ebx 8048459: 83 ec 04 sub $0x4,%esp 804845c: a1 14 95 04 08 mov 0x8049514,%eax 8048461: 83 f8 ff cmp $0xffffffff,%eax 8048464: 74 0c je 8048472 <__do_global_ctors_aux+0x22> 8048466: 83 eb 04 sub $0x4,%ebx 8048469: ff d0 call *%eax 804846b: 8b 03 mov (%ebx),%eax 804846d: 83 f8 ff cmp $0xffffffff,%eax 8048470: 75 f4 jne 8048466 <__do_global_ctors_aux+0x16> 8048472: 83 c4 04 add $0x4,%esp 8048475: 5b pop %ebx 8048476: 5d pop %ebp 8048477: c3 ret Disassembly of section .fini: 08048478 <_fini>: 8048478: 55 push %ebp 8048479: 89 e5 mov %esp,%ebp 804847b: 53 push %ebx 804847c: 83 ec 04 sub $0x4,%esp 804847f: e8 00 00 00 00 call 8048484 <_fini+0xc> 8048484: 5b pop %ebx 8048485: 81 c3 70 11 00 00 add $0x1170,%ebx 804848b: e8 60 fe ff ff call 80482f0 <__do_global_dtors_aux> 8048490: 59 pop %ecx 8048491: 5b pop %ebx 8048492: c9 leave 8048493: c3 ret colosus:~/bando/chap3/sec2> ---------------------------------------------------- colosus:~/bando/chap3/sec2>more simple.c int simple(int *xp, int y) { int t = *xp + y; *xp = t; return t; } colosus:~/bando/chap3/sec2>gcc -O2 -S simple.c colosus:~/bando/chap3/sec2>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 20070502 (Red Hat 4.1.2-12)" .section .note.GNU-stack,"",@progbits colosus:~/bando/chap3/sec2> ----------------------------------------------------- simple: pushl %ebp #save frame pointer movl %esp, %ebp #create new frame pointer movl 8(%ebp), %edx #get xp movl 12(%ebp), %eax #get y addl (%edx), %eax #add *xp and y to get t movl %eax, (%edx) #store t at *xp popl %ebp #reset frame pointer ret $return --------------------------------------------------------