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.
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.
a, t, ra) may be destroyed by any call. If the caller still needs one afterwards, the caller saves it.s, sp) must look untouched when a function returns. If the callee wants to use one, the callee saves and restores it.sp exactly where it found it.| Registers | Use | Who saves it | Rule of thumb |
|---|---|---|---|
| a0–a7 | Arguments, return value in a0 | Caller | Gone after any call |
| t0–t6 | Temporaries | Caller | Gone after any call |
| ra | Return address | Caller | Every jal overwrites it, so a function that calls must save it |
| s0–s11 | Saved values | Callee | Safe across calls, but costs a save and restore if you use it |
| sp | Stack pointer | Callee | Subtract on entry, add the same amount on exit |
ra goes on the stack.s register (and save that register) or save the a/t register around the call.addi sp, sp, -N, then store each saved register. Epilogue: load them back, addi sp, sp, N, ret.sp ends where it started.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?
g must save and restore t2t registers survive callsf must save t2 before the call, or keep the value in an s registerTemporaries 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?
s3 onlyra and s3a0, t1 and s3Only 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?
ret jumps back into the function itself and never reaches the original callerThe inner jal overwrote ra with an address inside the function. The original return address is lost.
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.
rs1, rs2 and rd in the same bit positions, so the register file can be read before the instruction type is fully known.rd, and the next PC. An instruction is defined by its mux settings and write enables.| Instruction | ALU second input | Write register? | Value written | Memory | Next PC |
|---|---|---|---|---|---|
| add | rs2 value | yes | ALU result | unused | PC + 4 |
| addi | immediate | yes | ALU result | unused | PC + 4 |
| lw | immediate | yes | memory data | read | PC + 4 |
| sw | immediate | no | none | write | PC + 4 |
| beq | rs2 value (compare) | no | none | unused | PC + imm if taken |
| jal | none | yes | PC + 4 | unused | PC + imm |
tCLK is the largest sum over all instructions, usually the load.The register write enable is stuck at 1. Which instructions now corrupt state?
rd field holds immediate bits and some register gets overwrittenStores 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?
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?
This is jalr. The target is computed like a load address, and the link value is written like jal.
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.
| Level | Size | Speed | Holds |
|---|---|---|---|
| Registers | tiny | fastest | The values in use right now |
| Cache (SRAM) | small | fast | Recently used blocks of memory |
| Main memory (DRAM) | large | about 100× slower | Everything the running programs need |
| Flash or disk | huge | thousands to millions of times slower | Everything, permanently |
HitTime is 2 cycles, MissRatio 5%, MissPenalty 100 cycles. What is the AMAT?
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?
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?
Spatial locality. Walking down a column jumps a whole row's worth of bytes each step and can miss every time.
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.
Address bits from left to right: tag, index, offset. Number of sets = lines ÷ ways.
32-bit addresses, direct-mapped, 128 lines, 8-word (32-byte) blocks. How many tag, index and offset bits?
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?
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?
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
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.
| full bypassing | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| lw x1, 0(x2) | IF | DEC | EXE | MEM | WB ↓ | |||
| add x3, x1, x4 | IF | DEC | DEC | DEC ← | EXE | MEM | WB | |
| next | IF | IF | IF | DEC | EXE | MEM |
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.
No bypassing. addi x1, x0, 5, then an unrelated instruction, then add x2, x1, x1. How many stall cycles?
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?
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?
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
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.
| taken branch | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| beq (taken) | IF | DEC | EXE | MEM | WB | |||
| PC + 4 | IF | DEC ✗ | ||||||
| PC + 8 | IF ✗ | |||||||
| target | IF | DEC | EXE | MEM | WB |
A taken branch or a jump costs 2 cycles. A not-taken branch costs 0.
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?
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?
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?
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
a, t, ra. Callee-saved: s, sp. A function that calls must save ra.