# === original files $ ls 4 diary.txt 4 main.c # compile with options # -save-temps keep intermediate files # .i text after preprocessor # .s text after assembler # .o binary after compiler (before linking) # -m32 generate I32 assembly (as used in textbook) # -O0 no opimization (so assembly matches C) # -g include C symbol names for use in gdb $ gcc -save-temps -g -m32 -O0 main.c -o main # === dump binary to assembly ; then look at ext file main.dump $ objdump -d -g main > main.dump 4 diary.txt 12 main* 4 main.c 12 main.dump 4 main.i 4 main.o 8 main.s # === run program with gdb and examine it there. $ gdb main (gdb) start Temporary breakpoint 1 at 0x80483f3: file main.c, line 15. Starting program: .../main Temporary breakpoint 1, main () at main.c:15 15 int a = 1; (gdb) info frame Stack level 0, frame at 0xbffff840: eip = 0x80483f3 in main (main.c:15); saved eip 0xb7e88bd6 source language c. Arglist at 0xbffff838, args: Locals at 0xbffff838, Previous frame's sp is 0xbffff840 Saved registers: ebp at 0xbffff838, eip at 0xbffff83c (gdb) info registers eax 0xbffff8e4 -1073743644 ecx 0x497c7d03 1232895235 edx 0x1 1 ebx 0xb7fc6ff4 -1208193036 esp 0xbffff820 0xbffff820 ebp 0xbffff838 0xbffff838 esi 0x0 0 edi 0x0 0 eip 0x80483f3 0x80483f3 eflags 0x200282 [ SF IF ID ] cs 0x73 115 ss 0x7b 123 ds 0x7b 123 es 0x7b 123 fs 0x0 0 gs 0x33 51 (gdb) list 10 *i = *j; 11 *j = temp; 12 } 13 14 int main(){ 15 int a = 1; 16 int b = 2; 17 int c; 18 swap(&a, &b); 19 c = sum(a,b); (gdb) disass sum Dump of assembler code for function sum: 0x080483b4 <+0>: push %ebp 0x080483b5 <+1>: mov %esp,%ebp 0x080483b7 <+3>: sub $0x10,%esp 0x080483ba <+6>: mov 0xc(%ebp),%eax 0x080483bd <+9>: mov 0x8(%ebp),%edx 0x080483c0 <+12>: lea (%edx,%eax,1),%eax 0x080483c3 <+15>: mov %eax,-0x4(%ebp) 0x080483c6 <+18>: mov -0x4(%ebp),%eax 0x080483c9 <+21>: leave 0x080483ca <+22>: ret End of assembler dump. (gdb)