Skip to content

Instantly share code, notes, and snippets.

@peterc
Created May 11, 2012 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/2661539 to your computer and use it in GitHub Desktop.
Save peterc/2661539 to your computer and use it in GitHub Desktop.
C addition disassembly
int i = 1;
i += ++i + ++i;
.. disassembly of above (compiled with gcc 4.2 with no optimizations):
>> x = 1
0000000100000f14 movl $0x00000001,0xf8(%rbp)
>> ++x
0000000100000f1b movl 0xf8(%rbp),%eax
0000000100000f1e addl $0x01,%eax
0000000100000f21 movl %eax,0xf8(%rbp)
>> ++x
0000000100000f24 movl 0xf8(%rbp),%eax
0000000100000f27 addl $0x01,%eax
0000000100000f2a movl %eax,0xf8(%rbp)
>> now adds the value of the newly incremented x (now 3) to itself twice
0000000100000f2d movl 0xf8(%rbp),%eax
0000000100000f30 movl 0xf8(%rbp),%ecx
0000000100000f33 addl %ecx,%eax
0000000100000f35 movl 0xf8(%rbp),%ecx
0000000100000f38 addl %ecx,%eax
>> puts 9 back into x
0000000100000f3a movl %eax,0xf8(%rbp)
..
WITH optimizations at -O4, it just turns into 9 right away ;-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment