Skip to content

Assembly Listing

You'll read what the compiler actually emitted for your code, watch an optimizer delete a loop, and measure what a cache miss costs you.

About

Between your C and the CPU sits a compiler with opinions, and it's worth seeing what it decided. You'll compile the same function with no optimization and with -O2, read both listings, and find the loop the optimizer removed because it worked out the answer ahead of time. Then you'll run the experiment every systems programmer does once: walk a big array row by row and column by column, and watch the second one run several times slower for the same work. None of this needs you to write assembly, only to read it slowly.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
5
Takes
One weekend
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Systems Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Compile a function that sums an array at -O0 and -O2 and read both listings. Use gcc -S -O0 sum.c and gcc -S -O2 sum.c, or paste it into Compiler Explorer at godbolt.org, which is free and colors the lines to match the source. Find the loop in each. The -O0 version loads and stores to the stack every iteration, and the -O2 version keeps the total in a register. Take a screenshot of both with the loop marked. Photo
  2. 2
    Write a loop that computes a constant and show the optimizer replacing the whole loop with the answer. Sum the numbers from 1 to 100 in a loop and return the total. At -O2 the function body becomes a single move of 5050 into a register. Take a screenshot of the listing with that instruction. Photo
  3. 3
    Find your function in the compiled binary with objdump or otool and match it to the source. Build with -g and run objdump -d -S ./program (or otool -tV on a Mac) to see the assembly interleaved with the source lines. Find the call to your function from main. Take a screenshot of the disassembly with the call marked. Photo
  4. 4
    Measure the cache effect by summing a 4,096 by 4,096 array row-major and then column-major. Allocate 64 MB of ints, fill it, and time both traversals. The column-major walk touches a new cache line on every access and runs several times slower for identical work. Run each three times and keep the median. Take a screenshot of the timings and a sentence on why. Photo
  5. 5
    Pick one instruction you don't recognize, look it up, and explain it to someone. The Intel and ARM manuals are free online, and so is the Compiler Explorer tooltip. Explain what the instruction does and why the compiler chose it, to a friend or to the camera, in under two minutes. Film the explanation. Video