Fun with Assembly

I still remember an interview I had around February 2001, in which +Ron Garnett talked about how his team wrote code:
We write stuff in Assembler, because we're too lazy to write stuff in C.
Wait...what?  I thought the whole purpose of C was to have portable Assembly, so you could control the bare metal correctly.  I did get an inkling if you were that good, assembly could be seductive in your ability to do whatever you want.

This came to mind again when +Seth Moore posed a similar question on Facebook the other night:
Pop quiz:When you run this, what prints out? 

int i = 5;
int j = 10;
while (--j) { printf("%d %d\n", i, j); } while (--i); 
Basically, the above is a quiz to determine if you understand loops, expressions -v- statements, and the pre-decrement operator (--).  Pre-decrement specifies that the lvalue of the expression is the current value minus one and the post-state of that variable is assigned that decremented value.  Post-decrement has the same result (decrementing the value), but the lvalue of the expression is the PREVIOUS value.

As is my wont, I got the above wrong, but that's not the point.   To check my answer, I sucked it into quick c program using vim:
Compiling that program and using mac's otool to dump the assembly gives you this
Some things to note in the above:

  • The compiler has done a faithful job of translating exactly the program (as-is) to assembler:
    • We load the variables in lines 9 and 10
    • We have the first loop in lines 11-22
    • The second loop (despite being a no-op) is in line 24-29

Things get slightly more interesting when you pass the -O (optimize) flag

Some things to note:

  • This looks nothing like the C code.  There are no loops (or indeed, branch instructions) at all.
  • The compiler determined the second loop to be a no-op, and compiled it away completely.
  • Our stack variables are gone.  The compiler is using x64 CPU registers exclusively. 
  • The compiler has analyzed the loop and unrolled it into discrete calls to callq for the printf function.
Lastly:  The answer to the quiz is in the assembly if you look hard enough:

5 9
5 8
5 7
5 6
5 5
5 4
5 3
5 2
5 1

Pretty cool....I never get to look at assembly in my day-job, so getting this close to the CPU is neat.

Popular posts from this blog

Monday Mope

RegisterForPrintAsyncNotifications

Review: The Southeast Christian Church Easter Pageant