---
title: Final exam
heading: "Final exam: computer organization"
eyebrow: C8 · Final exam
type: problems
---

::: hook
Twelve problems from the whole course: instructions and machine code, memory and the stack, bits and fixed point, interrupts and I/O, linking, performance, the single-cycle and pipelined processor, branch prediction, caches, virtual memory and multicore power, and one program to write. Give yourself three hours, work without the lessons open, and write out your reasoning before checking each answer. "Another set" deals a different exam.
:::

::: pool
pick: 12

::: check
kind: parts
given: rd = 7 | 12 | 28; rs1 = 5 | 10; rs2 = 6 | 11

## Encode the R-type instructions below (opcode 0x33).

::: part
answer: 32*2^25 + rs2*2^20 + rs1*2^15 + rd*2^7 + 51
base: 16
width: 8

### `sub x{rd}, x{rs1}, x{rs2}` (funct3 0, funct7 0x20)

::: misses
- [rs2*2^20 + rs1*2^15 + rd*2^7 + 51] That's `add`; `sub` has funct7 = 0x20.
- [any] Not yet. funct7 in 31:25, rs2, rs1, funct3, rd, opcode.
:::
:::

::: part
answer: 2^25 + rs2*2^20 + rs1*2^15 + rd*2^7 + 51
base: 16
width: 8

### `mul x{rd}, x{rs1}, x{rs2}` (funct3 0, funct7 1)

::: misses
- [any] The M extension uses funct7 = 1.
:::
:::
:::

::: check
kind: parts
given: w = 4287956243 | 4278255891 | 4294083219

## The word {w:hex8} is an `addi`.

::: part
answer: floor(w/128) - 32*floor(w/4096)
tolerance: 0

### Which register does it write?

::: misses
- [any] Bits 11:7.
:::
:::

::: part
answer: floor(w/2^20) - 4096
tolerance: 0

### What is its immediate?

::: misses
- [floor(w/2^20)] Sign-extend: bit 31 is set.
- [any] Not yet. Bits 31:20, minus 4096.
:::
:::
:::

::: check
kind: parts
given: w = 3405691582 | 2596069104 | 4275878552; k = 1 | 2

## The word {w:hex8} is stored at 0x3000, little-endian.

::: part
answer: floor(w/256^k) - 256*floor(w/256^(k + 1))
base: 16
width: 2

### What byte is at 0x300{k}?

::: misses
- [floor(w/256^(3 - k)) - 256*floor(w/256^(4 - k))] That's big-endian.
- [any] Not yet. Low byte first.
:::
:::

::: part
answer: floor(w/65536) - 65536*step(floor(w/65536) - 32767.5)
tolerance: 0

### What does `lh` from 0x3002 load?

::: misses
- [floor(w/65536)] `lh` sign-extends.
- [any] Not yet. The upper halfword, sign-extended.
:::
:::
:::

::: check
kind: parts
given: k = 1 | 2 | 4 | 5; d = 8 | 50 | 300

## A recursive function saves ra and {k} s registers in a frame rounded up to 16 bytes and recurses {d} levels deep.

::: part
answer: d*16*ceil(4*(k + 1)/16)
unit: bytes
tolerance: 0

### How much stack does it use at the deepest point?

::: misses
- [d*4*(k + 1)] Round each frame up to 16 bytes.
- [any] Not yet. Depth × frame.
:::
:::

::: part
answer: 16*ceil(4*(k + 1)/16)
unit: bytes
tolerance: 0

### How much would a loop version, with one such frame, use?

::: misses
- [any] One frame.
:::
:::
:::

::: check
kind: parts
given: x = 0.15 | 0.45 | -0.35; c = 1000..4095 int; V = 3300 | 5000

## Fixed point: {x} in Q15, and a 12-bit ADC reading of {c} with a {V} mV reference.

::: part
answer: round(x*32768)
tolerance: 0

### What integer stores {x}?

::: misses
- [any] × 32768, rounded.
:::
:::

::: part
answer: floor(c*V/4096)
unit: mV
tolerance: 0

### What does multiply-then-shift give for the reading, in millivolts?

::: misses
- [any] ⌊cV/4096⌋.
:::
:::
:::

::: check
kind: parts
given: R = 20000 | 200000 | 2000000; c = 40 | 90; f = 32 | 120; n = 256 | 1024

## A peripheral produces {R} values a second. Interrupts cost {c} cycles on a {f} MHz core.

::: part
answer: R*c/(f*1e6)*100
unit: %
tolerance: 0.5%

### What load would one interrupt per value cause? (Over 100% is impossible.)

::: misses
- [any] R·c/f.
:::
:::

::: part
answer: n/R*1e6
unit: µs
tolerance: 0.5%

### With double-buffered DMA blocks of {n}, how long may processing a block take?

::: misses
- [any] The time to fill the next block.
:::
:::
:::

::: check
kind: parts
given: T = 40 | 100; R = 5 | 12; D = 2 | 6; B = 10 | 30; S = 4 | 8

## A firmware build has {T} kB of `.text`, {R} kB of `.rodata`, {D} kB of `.data`, {B} kB of `.bss` and a {S} kB stack.

::: part
answer: T + R + D
unit: kB
tolerance: 0.5%

### How much flash?

::: misses
- [T + R] Include the initial values of `.data`.
- [any] .text + .rodata + .data.
:::
:::

::: part
answer: D + B + S
unit: kB
tolerance: 0.5%

### How much RAM?

::: misses
- [any] .data + .bss + stack.
:::
:::
:::

::: check
kind: parts
given: p = 0.5 | 0.8 | 0.95; s = 3 | 10 | 100

## A fraction {p} of a program's time is in code made {s} times faster.

::: part
answer: 1/((1 - p) + p/s)
tolerance: 0.5%

### What is the overall speedup?

::: misses
- [s] Only part of the program sped up.
- [any] Amdahl's law.
:::
:::

::: part
answer: 1/(1 - p)
tolerance: 0.5%

### What is the limit as s grows?

::: misses
- [any] 1/(1 − p).
:::
:::
:::

::: check
kind: parts
given: cq = 35 | 50; m = 180 | 250; r = 100 | 140; l = 120 | 200; x = 30; s = 50 | 80; o = 40

## A single-cycle processor has clock-to-Q {cq}, memories {m}, register read {r}, ALU {l}, mux {x} and register setup {s} (ps). A pipelined version's slowest stage is one memory access, plus {o} ps of register overhead.

::: part
answer: cq + 2*m + r + l + x + s
unit: ps
tolerance: 0.5%

### What is the single-cycle clock period?

::: misses
- [any] The load's path through both memories.
:::
:::

::: part
answer: (cq + 2*m + r + l + x + s)/(m + o)
tolerance: 0.5%

### How many times faster is the pipelined clock?

::: misses
- [any] Single-cycle period over (memory + overhead).
:::
:::
:::

::: check
kind: parts
given: L = 0.2 | 0.3; u = 0.3 | 0.6; B = 0.12 | 0.2; a = 0.9 | 0.97; p = 2 | 10

## {L} of instructions are loads, {u} of them used at once; {B} are branches, predicted with accuracy {a}, at a {p}-cycle penalty.

::: part
answer: 1 + L*u + B*(1 - a)*p
tolerance: 0.5%

### What is the CPI?

::: misses
- [1 + L*u + B*p] Only mispredicted branches pay.
- [any] Not yet. 1 + L·u + B(1 − a)p.
:::
:::

::: part
answer: 1/(1 + L*u + B*(1 - a)*p)
tolerance: 0.5%

### What is the IPC?

::: misses
- [any] 1/CPI.
:::
:::
:::

::: check
kind: parts
given: n = 4 | 8 | 30; m = 20 | 200

## A loop of {n} iterations runs {m} times; count all runs, the predictors starting at not taken (1-bit) and weakly not taken (2-bit).

::: part
answer: 2*m
tolerance: 0

### How many mispredictions does the 1-bit predictor make?

::: misses
- [2*m - 2] The first run misses its first iteration too, since the predictor starts at not taken.
- [any] Not yet. Two per run, the first run included.
:::
:::

::: part
answer: m + 1
tolerance: 0

### How many does the 2-bit counter make?

::: misses
- [m] The first run also misses its first iteration while the counter warms up.
- [any] Not yet. Two in the first run, one in each other.
:::
:::
:::

::: check
kind: parts
given: C = 32 | 128; L = 64; N = 4 | 8; t1 = 1 | 2; m1 = 0.03 | 0.05; t2 = 12; m2 = 0.25 | 0.4; P = 150

## A {C} KiB, {N}-way L1 with {L}-byte lines hits in {t1} cycles and misses {m1} of the time; L2 takes {t2} cycles with a local miss rate of {m2}; memory takes {P} cycles.

::: part
answer: 32 - log(C*1024/(L*N))/log(2) - 6
tolerance: 0.5%

### How many tag bits does the L1 use (32-bit addresses)?

::: misses
- [any] 32 − index − offset.
:::
:::

::: part
answer: t1 + m1*(t2 + m2*P)
unit: cycles
tolerance: 0.5%

### What is the AMAT?

::: misses
- [any] $t_1 + m_1(t_2 + m_2P)$.
:::
:::
:::

::: check
kind: parts
given: v = 4000000 | 60000000 | 900000000; P = 700 | 2000; h = 0.99 | 0.999; M = 100; n = 2 | 4

## Address {v:hex8} is on a page mapped to physical page {P:hex5} (4 KiB pages). A 1-cycle TLB hits {h} of the time, memory takes {M} cycles, and the page table has {n} levels.

::: part
answer: P*4096 + v - 4096*floor(v/4096)
base: 16
width: 8

### What is the physical address?

::: misses
- [any] PPN × 4096 + offset.
:::
:::

::: part
answer: h*(1 + M) + (1 - h)*(1 + n*M + M)
unit: cycles
tolerance: 0.5%

### What is the average access time?

::: misses
- [any] Hits 1 + M; misses 1 + nM + M.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 4 | 8; s = 0.6 | 0.8; v = 0.8 | 0.9; p = 0.9 | 0.99

## {k} cores at {s} of a single core's clock and {v} of its voltage run a program that is {p} parallel.

::: part
answer: k*v^2*s
tolerance: 0.5%

### What is their dynamic power relative to the one core?

::: misses
- [any] k·v²·s.
:::
:::

::: part
answer: s/((1 - p) + p/k)
tolerance: 0.5%

### What is the speedup over the one core?

::: misses
- [1/((1 - p) + p/k)] Each core is also slower, at {s} of the clock.
- [any] Not yet. Amdahl's speedup at the slower clock.
:::
:::
:::

::: check
kind: asm
given: a = 1..200 int; b = 1..200 int; c = 1..200 int; d = 1..200 int; e = 1..200 int
expect: a0 = (((a + b + abs(a - b))/2 + c + abs((a + b + abs(a - b))/2 - c))/2 + (d + e + abs(d - e))/2 + abs(((a + b + abs(a - b))/2 + c + abs((a + b + abs(a - b))/2 - c))/2 - (d + e + abs(d - e))/2))/2 - (((a + b - abs(a - b))/2 + c - abs((a + b - abs(a - b))/2 - c))/2 + (d + e - abs(d - e))/2 - abs(((a + b - abs(a - b))/2 + c - abs((a + b - abs(a - b))/2 - c))/2 - (d + e - abs(d - e))/2))/2

## Leave the largest of the five words in `vals` minus the smallest (the range) in a0.

```asm starter
.data
vals: .word a, b, c, d, e
.text
    la t0, vals
    ebreak
```

```asm solution
.data
vals: .word a, b, c, d, e
.text
    la t0, vals
    lw t1, 0(t0)        # largest so far
    mv t2, t1           # smallest so far
    li t3, 4
loop:
    beqz t3, done
    addi t0, t0, 4
    addi t3, t3, -1
    lw t4, 0(t0)
    ble t4, t1, notbig
    mv t1, t4
notbig:
    bge t4, t2, loop
    mv t2, t4
    j loop
done:
    sub a0, t1, t2
    ebreak
```

::: right
Right. One pass keeps both the largest and the smallest.
:::
:::

::: check
kind: parts
given: l = 20 | 30; b = 10 | 20; T = 250 | 320; U = 750 | 800

## A program is {l}% loads and {b}% branches. A multi-cycle processor (loads 5 cycles, branches 3, the rest 4) runs at {T} ps; a single-cycle one at {U} ps.

::: part
answer: (4 + l/100 - b/100)*T
unit: ps
tolerance: 0.5%

### What is the multi-cycle time per instruction?

::: misses
- [any] CPI × period, with CPI = 4 + loads − branches.
:::
:::

::: part
answer: U/(4 + l/100 - b/100)
unit: ps
tolerance: 0.5%

### What clock period would the multi-cycle design need to match the single-cycle one?

::: misses
- [any] $U/\text{CPI}$.
:::
:::
:::
:::
