Assembly Reading Comprehension¶
In [7]:
!rm -Rf tmp
!mkdir -p tmp
Here is a simple snippet of C:
In [25]:
%%writefile tmp/asm-reading-comprehension.c
int do_stuff()
{
int sum = 0;
for (int i = 1; i <= 100; ++i)
sum += i*i;
return sum;
}
Overwriting tmp/asm-reading-comprehension.c
Compile the file:
In [26]:
!cd tmp; gcc -c -O0 asm-reading-comprehension.c
Now disassemble:
In [27]:
!objdump --disassemble tmp/asm-reading-comprehension.o
tmp/asm-reading-comprehension.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <do_stuff>: 0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) b: c7 45 f8 01 00 00 00 movl $0x1,-0x8(%rbp) 12: eb 0e jmp 22 <do_stuff+0x22> 14: 8b 45 f8 mov -0x8(%rbp),%eax 17: 0f af 45 f8 imul -0x8(%rbp),%eax 1b: 01 45 fc add %eax,-0x4(%rbp) 1e: 83 45 f8 01 addl $0x1,-0x8(%rbp) 22: 83 7d f8 64 cmpl $0x64,-0x8(%rbp) 26: 7e ec jle 14 <do_stuff+0x14> 28: 8b 45 fc mov -0x4(%rbp),%eax 2b: 5d pop %rbp 2c: c3 retq
How does optimization change the outcome?
In [28]:
!cd tmp; gcc -c -O asm-reading-comprehension.c
!objdump --disassemble tmp/asm-reading-comprehension.o
tmp/asm-reading-comprehension.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <do_stuff>: 0: b8 64 00 00 00 mov $0x64,%eax 5: 83 e8 01 sub $0x1,%eax 8: 75 fb jne 5 <do_stuff+0x5> a: b8 ae 29 05 00 mov $0x529ae,%eax f: c3 retq
... even more optimization?
In [29]:
!cd tmp; gcc -c -O3 asm-reading-comprehension.c
!objdump --disassemble tmp/asm-reading-comprehension.o
tmp/asm-reading-comprehension.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <do_stuff>: 0: b8 ae 29 05 00 mov $0x529ae,%eax 5: c3 retq
In [ ]: