Fast Track 2: Processors

Lectures 10 to 15 in about 50 minutes: one idea, one picture, one recipe and three questions per lecture. Answer every question before moving on, because the questions are where the understanding gets tested.

L10 · Assembly and the calling convention

A function call is a contract: each register has an owner, and whoever wants a value to survive must save it according to that register's rule.

Why it works

RegistersUseWho saves itRule of thumb
a0–a7Arguments, return value in a0CallerGone after any call
t0–t6TemporariesCallerGone after any call
raReturn addressCallerEvery jal overwrites it, so a function that calls must save it
s0–s11Saved valuesCalleeSafe across calls, but costs a save and restore if you use it
spStack pointerCalleeSubtract on entry, add the same amount on exit

The move: write or check a function

  1. List the calls the function makes. If there is at least one, ra goes on the stack.
  2. List every value needed after a call. Put it in an s register (and save that register) or save the a/t register around the call.
  3. Prologue: addi sp, sp, -N, then store each saved register. Epilogue: load them back, addi sp, sp, N, ret.
  4. Check: every register stored is loaded from the same offset, and sp ends where it started.
TrapAssuming a t or a register still holds your value after a call because the function you called "does not use it". The contract lets it, so you must not rely on it.

Function f computes a value in t2, calls g, then uses t2. What must happen?

  1. g must save and restore t2
  2. Nothing, t registers survive calls
  3. f must save t2 before the call, or keep the value in an s register

Temporaries are caller-saved. The callee owes nothing for them, so the function that needs the value protects it.

A leaf function (it calls nothing) uses a0, t1 and s3. Which registers must it save on the stack?

  1. s3 only
  2. ra and s3
  3. a0, t1 and s3
  4. None

Only the callee-saved register it overwrites. ra is not overwritten because the function makes no call.

A recursive function forgets to save ra. What goes wrong?

  1. Arguments arrive in the wrong registers
  2. After the inner call returns, the outer ret jumps back into the function itself and never reaches the original caller
  3. The stack pointer ends 4 bytes too low

The inner jal overwrote ra with an address inside the function. The original return address is lost.

More depth: L10 in Processor Foundations

L11 · The single-cycle processor

Every instruction is the same loop of fetch, decode, read registers, compute, optionally touch memory, write back, and the control signals only choose which paths are used.

Why it works

InstructionALU second inputWrite register?Value writtenMemoryNext PC
addrs2 valueyesALU resultunusedPC + 4
addiimmediateyesALU resultunusedPC + 4
lwimmediateyesmemory datareadPC + 4
swimmediatenononewritePC + 4
beqrs2 value (compare)nononeunusedPC + imm if taken
jalnoneyesPC + 4unusedPC + imm

The move: control signals or clock period for an instruction

  1. Say in words what the instruction does: which registers it reads, what it computes, what it writes.
  2. Walk the datapath left to right and set each mux and enable to make that happen. Anything unused is "don't care", except write enables, which must be off.
  3. For timing, add the delays of every component on that instruction's path. tCLK is the largest sum over all instructions, usually the load.
TrapLeaving a write enable as "don't care". A mux input that is ignored can be anything. A write enable that is ignored corrupts a register or memory.

The register write enable is stuck at 1. Which instructions now corrupt state?

  1. Loads, because they write twice
  2. Stores and branches, because their rd field holds immediate bits and some register gets overwritten
  3. None, extra writes are harmless

Stores and branches have no destination. Bits [11:7] are part of their immediate, so a random register is written.

Delays: instruction memory 300 ps, decode 100, register read 150, ALU 250, data memory 350, register write setup 50. What is the single-cycle clock period?

  1. 350 ps
  2. 850 ps
  3. 1200 ps

A load uses every component: 300 + 100 + 150 + 250 + 350 + 50 = 1200. The clock must fit the slowest instruction, even though add would finish in 850.

You add an instruction that writes PC + 4 to rd and jumps to rs1 + imm. Which settings does it need?

  1. ALU second input = immediate, write register with PC + 4, next PC = ALU result
  2. ALU second input = rs2, write register with ALU result, next PC = PC + imm
  3. No register write, next PC = ALU result

This is jalr. The target is computed like a load address, and the link value is written like jal.

More depth: L11 in Processor Foundations

L12 · The memory hierarchy

No memory is both big and fast, so a small fast memory holds what was used recently and a big slow one holds everything, and locality makes that look big and fast.

Why it works

LevelSizeSpeedHolds
RegisterstinyfastestThe values in use right now
Cache (SRAM)smallfastRecently used blocks of memory
Main memory (DRAM)largeabout 100× slowerEverything the running programs need
Flash or diskhugethousands to millions of times slowerEverything, permanently

The move: average memory access time

  1. Write AMAT = HitTime + MissRatio × MissPenalty.
  2. With two levels, the first level's MissPenalty is the second level's AMAT. Compute the outer level first.
  3. If asked for a required miss ratio, solve the same formula for MissRatio.
  4. Check units: all cycles or all nanoseconds.
TrapMultiplying the hit time by the hit ratio. Every access pays the hit time, because the cache must be checked before a miss is known.

HitTime is 2 cycles, MissRatio 5%, MissPenalty 100 cycles. What is the AMAT?

  1. 5 cycles
  2. 7 cycles
  3. 6.9 cycles
  4. 102 cycles

2 + 0.05 × 100 = 7. The answer 6.9 comes from scaling the hit time by 95%, which is the trap above.

L1: hit 1 cycle, 10% misses. L2: hit 10 cycles, 20% misses. Main memory: 200 cycles. What is the overall AMAT?

  1. 4 cycles
  2. 5 cycles
  3. 6 cycles
  4. 22 cycles

L2 AMAT = 10 + 0.2 × 200 = 50. That is L1's miss penalty: 1 + 0.1 × 50 = 6.

A C program sums a large 2-D array. Version A walks along each row. Version B walks down each column. Which has fewer cache misses, and why?

  1. A, because a row is contiguous in memory, so each fetched block serves several accesses
  2. B, because columns are shorter
  3. The same, both touch every element once

Spatial locality. Walking down a column jumps a whole row's worth of bytes each step and can miss every time.

More depth: L12 in Processor Foundations

L13 · Caches

A cache finds data by cutting the address into fields: the index picks where to look, the tag confirms it is the right block, and the offset picks the byte inside it.

Why it works

tagwhat is left of the address
stored and compared
indexlog2(number of sets)
chooses the set
block offsetlog2(block size in bytes)
chooses the byte in the block

Address bits from left to right: tag, index, offset. Number of sets = lines ÷ ways.

The move: address split, then a trace

  1. Offset bits = log2(block bytes). Index bits = log2(sets). Tag bits = address bits − index − offset.
  2. For each access, write the address in binary and cut it into the three fields.
  3. Look in that set. Valid line with the same tag is a hit. Otherwise it is a miss: fill an empty way, or evict the least recently used one.
  4. Update the LRU order on every hit and every fill. Count hits ÷ accesses.
TrapUsing the number of lines for the index when the cache is set-associative. The index counts sets. Doubling the ways at the same size halves the sets, so the index loses a bit and the tag gains one.

32-bit addresses, direct-mapped, 128 lines, 8-word (32-byte) blocks. How many tag, index and offset bits?

  1. 22, 7, 3
  2. 20, 8, 4
  3. 21, 6, 5
  4. 20, 7, 5

Offset = log2(32) = 5. Index = log2(128) = 7. Tag = 32 − 7 − 5 = 20. The answer with offset 3 counted words, not bytes.

That cache is rebuilt as 2-way with the same 128 lines. What happens to the tag?

  1. It stays 20 bits
  2. It grows to 21 bits
  3. It shrinks to 19 bits

128 lines in 2 ways is 64 sets, so the index is 6 bits. The bit the index lost moves into the tag.

Blocks A and B have the same index and different tags. The access order is A, B, A, B, A, B on an empty 2-way cache. How many hits?

  1. 0
  2. 3
  3. 4
  4. 6

The first A and first B miss and fill the two ways. The remaining four hit. A direct-mapped cache would get 0, because each access evicts the other block.

More depth: L13 in Processor Foundations · your own cache in Lab 6

L14 · Pipelining and data hazards

Pipelining shortens the clock by overlapping instructions, and a data hazard is an instruction reading a register before the earlier instruction that produces it has finished.

Why it works

full bypassing12345678
lw x1, 0(x2)IFDECEXEMEMWB ↓
add x3, x1, x4IFDECDECDEC ←EXEMEMWB
nextIFIFIFDECEXEMEM

A load followed by its user: 2 stall cycles even with full bypassing, because the data first exists in WB. An ALU instruction followed by its user: 0 stalls.

The move: fill in a pipeline diagram

  1. For each instruction, find the latest earlier instruction that writes one of its source registers.
  2. Find the first cycle the value is available: with no bypassing, the cycle after the producer's WB. With full bypassing, the producer's EXE for ALU results and its WB for loads.
  3. The reader's DEC must be in that cycle or later. Repeat DEC until it is. Everything behind it repeats its stage too.
  4. Performance: time = instructions × CPI × tCLK, with CPI = 1 + stall cycles per instruction and tCLK = slowest stage.
TrapTreating a load like an ALU instruction. A bypass cannot forward data that has not come out of memory yet.

No bypassing. addi x1, x0, 5, then an unrelated instruction, then add x2, x1, x1. How many stall cycles?

  1. 3
  2. 2
  3. 1
  4. 0

The producer's WB is cycle 5, so the value is readable in cycle 6. The reader is two instructions behind, with DEC normally in cycle 4. It waits 2 cycles. Directly behind, it would wait 3.

Full bypassing. lw x5, 0(x6), then sub x7, x8, x9, then or x1, x5, x7. How many stall cycles?

  1. 1
  2. 2
  3. 0

Load data exists in cycle 5 (WB). The or is two behind, so its DEC is normally cycle 4. One stall. Its other source, x7, is then bypassed from sub with no extra wait.

Stage delays are 300, 250, 250, 350 and 100 ps. The single-cycle version takes 1250 ps. With a CPI of 1.2, how much faster is the pipeline?

  1. 5×
  2. About 3.6×
  3. About 3×

tCLK is the slowest stage, 350 ps. Time per instruction = 1.2 × 350 = 420 ps. 1250 ÷ 420 ≈ 3.0. It is not 5× because the stages are unbalanced and stalls add cycles.

More depth: L14 in Processor Foundations · bypassing in your Lab 6 code

L15 · Control hazards and cache-miss stalls

The processor does not know the next PC until a branch executes, so it guesses PC + 4, keeps going, and throws away the wrong instructions if the guess was wrong.

Why it works

taken branch12345678
beq (taken)IFDECEXEMEMWB
PC + 4IFDEC ✗
PC + 8IF ✗
targetIFDECEXEMEMWB

A taken branch or a jump costs 2 cycles. A not-taken branch costs 0.

The move: cycles per loop iteration, or CPI

  1. Count the instructions executed in one iteration.
  2. Add the stall cycles from data hazards (L14 rules).
  3. Add 2 for every taken branch and every jump. Add nothing for not-taken branches.
  4. CPI = cycles ÷ instructions. From frequencies: CPI = 1 + Σ (fraction of instructions × cycles lost each).
  5. If the question changes the pipeline (branch resolved in DEC, a predictor), redo step 3 with the new number of wrong-path instructions.
TrapCharging the penalty to every branch. Only a wrong guess costs cycles, and with "predict PC + 4" the wrong guesses are the taken ones.

A loop body is 5 instructions ending in a branch back to the top. No data hazards. How many cycles per iteration while it keeps looping?

  1. 5
  2. 7
  3. 6
  4. 10

5 instructions plus 2 annulled after the taken branch. The last iteration, where the branch falls through, costs only 5.

20% of instructions are branches and 60% of those are taken. 10% of instructions are loads followed directly by a user. Full bypassing. What is the CPI?

  1. 1.24
  2. 1.34
  3. 1.44
  4. 1.60

Branches: 0.2 × 0.6 × 2 = 0.24. Load-use: 0.1 × 2 = 0.20. CPI = 1 + 0.24 + 0.20 = 1.44. The answer 1.60 charged every branch.

A redesign resolves branches in DEC. What changes?

  1. A taken branch annuls 1 instruction, not 2, but DEC now holds the comparison and may lengthen the clock
  2. Taken branches become free
  3. Nothing, the penalty depends only on the predictor

The penalty equals the number of stages before the branch is resolved. Moving the decision earlier trades fewer lost cycles for more logic in one stage.

More depth: L15 in Processor Foundations · annulment in your Lab 6 code

Have these memorised