Reading Assembly

Part 0 · Orientation

The four fictions

Assembly is a contract, not a description. It presents four convenient lies, and fluency means knowing both the lie and where it breaks — plus a pretest you are meant to fail.

40 minnotional machinepretesting effectPOE
What this lesson is trying to break (3)
  • Assembly is what the CPU executes.
  • The listing tells you what happens, in the order it happens.
  • One instruction is one unit of work.

Here is the organising claim of this course.

Assembly is a contract, not a description. It records what the program asked for. The silicon decides what happens.

Everything else follows from taking that seriously.

The four fictions

Assembly presents four convenient lies. Each is useful — you must reason in them to read anything at all — and each is wrong in a way that eventually costs you money.

Varies: which fiction is being told

#The fictionWhat is actually trueWhere you meet the gap
1There are 16 registers, and each holds one value at a time.Hundreds of physical registers, dynamically renamed. Two instructions using eax may be completely independent.Why xor eax, eax is an idiom and not just a zero
2Instructions execute in the order listed.Out-of-order, pipelined and speculative. The listing is a dependency graph.Why reordering source lines usually changes nothing
3A load is a load. Memory is a flat array.L1 ≈ 4 cycles, DRAM ≈ 200+. One instruction, two orders of magnitude.Why the fast version has the same instruction count
4One instruction is one unit of work.x86 instructions decode to µops; some fuse, some split. add and div differ ~40×.Why counting instructions predicts nothing

Parts 1 to 3 make you fluent in the fictions, because you cannot read a listing without them. Part 4 takes all four apart. Most courses stop after fiction 2 and never mention 3 and 4 — which is how you get someone who can read assembly but cannot explain why the fast code is fast.

Read that table before continuing. The order matters: you have to accept the fictions before you can usefully lose them.

Why the fictions exist

Not incompetence or legacy — they are a deliberate, load-bearing abstraction.

The ISA is a stable interface. Intel and AMD ship radically different silicon that both run the same x86-64 instructions. Apple redesigns its cores every year while ARM64 binaries keep working. If assembly described the machine, every binary would break on every new chip.

So the contract is: you write requests in this fixed vocabulary; we will honour them by whatever means we like, as fast as we can, and we promise the observable result matches what a naive sequential reading would produce.

That promise is why fictions 1 and 2 are safe to reason in for correctness and useless for performance. Your program will behave as if execution were in order with 16 registers. It will not perform as if that were true.

Beaconsee this → think this, without decoding it

xor eax, eax

Zero — and break the dependency.

Setting a register to zero could be mov eax, 0. Compilers overwhelmingly emit xor eax, eax instead, and there are two reasons, one from each of the first two fictions.

The boring reason: the encoding is shorter (2 bytes versus 5).

The real reason: the CPU recognises this exact pattern and treats the register as having no dependency on its previous value. Under fiction 1 that sentence is meaningless — a register just holds what it holds. Under register renaming it is the whole point: the instruction gets a fresh physical register and nothing waiting on the old value can stall it.

This is your first example of an idiom that is inexplicable in the fiction and obvious once you drop it.

Pretest — one question per part

Six questions, one from each remaining part. You have been taught none of it. Do not look anything up, and do not press any compile buttons. Guess, commit, and rate your confidence honestly.

This feels pointless. It is not: being tested on material before studying it measurably improves how much you learn afterwards, including for the items you answer wrong. The wrong answers you are about to give are doing work.

Predict firstcommit in writing, then look
lea     eax, [rdi+rdi*2]

The mnemonic stands for “Load Effective Address”.

Predict firstcommit in writing, then look

A C function takes an int as its first parameter, compiled for Linux or macOS on x86-64. Which register is that parameter in when the function starts?

Predict firstcommit in writing, then look
int d(int x) {
    return x / 3;
}
Predict firstcommit in writing, then look

Both loops do 1024 additions of the same numbers.

// A — one accumulator
int a(int *p) { int s = 0; for (int i = 0; i < 1024; i++) s += p[i]; return s; }

// B — four accumulators, combined at the end
int b(int *p) {
    int s0=0, s1=0, s2=0, s3=0;
    for (int i = 0; i < 1024; i += 4) { s0+=p[i]; s1+=p[i+1]; s2+=p[i+2]; s3+=p[i+3]; }
    return s0+s1+s2+s3;
}

Same instruction count, near enough. Same memory traffic. Is one faster?

Predict firstcommit in writing, then look

A ymm register on x86-64 is 256 bits wide. How many 32-bit integers does it hold, and therefore how many additions can a single vpaddd instruction perform?

Predict firstcommit in writing, then look

A colleague says: “It executed but never retired.” What are they describing?

Score yourself honestlywrite it — thinking it does not count

How many of the six did you get? More usefully: on how many were you confident and wrong?

Write those down. They are not gaps in knowledge — they are places where a model you trust produces wrong output, and they are the highest-value part of this course for you.

Check Calibration now for your baseline, and come back to it after Part 3 and after Part 4. The number that matters is not your accuracy — it is the distance between your confidence and your accuracy.

Next: 1.1 — Registers are a scarce physical resource.

In the wildgo read the real thing now, while it is fresh
  • uops.infoMeasured latency, throughput and port usage for every x86 instruction on every recent microarchitecture. Look up `imul` and then `div` and compare. This page is fiction four, quantified.
  • Agner Fog — microarchitecture manualThe reference work on how x86 chips actually behave. You will not read it front to back; you will look things up in it for years. Skim the table of contents so you know what is in there.
Scheduled for recall3 items added to your review queue

These are not shown to you now on purpose. They will surface in Review tomorrow, mixed in among items from other parts of the course — not grouped by topic. Sorting out which idea applies is the part that transfers.