---
title: Problem set 12
heading: "Problem set 12: computer organization across the course"
eyebrow: C8 · Problem set 12
type: problems
---

::: hook
Multi-step problems that draw on the whole course: timing and pipelines, addresses and the stack, device registers and interrupts, caches and virtual memory, machine code, fixed-point arithmetic, and one program to write. Each one has your own numbers.
:::

::: pool
pick: 12

::: check
kind: parts
given: N = 2 | 5 | 8; f = 50 | 100; c = 1.2 | 1.25 | 1.5; let g = 4*f

## A program runs {N} million instructions. A single-cycle processor runs it at {f} MHz. A 5-stage pipelined version of the same processor runs at {g} MHz with an average CPI of {c}.

::: part
answer: 1000*N/f
unit: ms
tolerance: 0.5%

### How long does the single-cycle processor take?

::: misses
- [N/f] {N} million instructions at {f} million a second: the answer in seconds is {N}/{f}.
- [any] $t = N \cdot \text{CPI}/f$ with CPI = 1.
:::
:::

::: part
answer: 1000*N*c/g
unit: ms
tolerance: 0.5%

### How long does the pipelined processor take?

::: misses
- [1000*N/g] Include the CPI of {c}.
- [any] $t = N \cdot \text{CPI}/f$ at {g} MHz.
:::
:::

::: part
answer: g/(f*c)
tolerance: 0.5%

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

::: misses
- [g/f] The stalls take back part of the faster clock: divide by the CPI too.
- [any] Divide the first time by the second.
:::
:::

::: right
Right. The clock is 4 times faster, but stalls make the gain smaller than 4.
:::
:::

::: check
kind: parts
given: p = 1024..2000 int; n = 10..120 int; f = 16 | 48 | 100; let B = 4*p

## An array of {n} `int`s (4 bytes each) starts at 0x{B:hex4}, and lesson 17's `sum` adds it up on a processor with CPI 1 at {f} MHz.

::: part
answer: B + 4*(n - 1)
base: 16
width: 4

### At what address does the last element start?

::: misses
- [B + 4*n] The last element is number {n} − 1, counting from 0.
- [any] Base + 4 × (n − 1), in hex.
:::
:::

::: part
answer: 7*n + 5
tolerance: 0

### How many instructions does `sum` run (7 per pass, plus 5)?

::: misses
- [any] 7 × {n} + 5.
:::
:::

::: part
answer: (7*n + 5)/f
unit: µs
tolerance: 0.5%

### How long does it take?

::: misses
- [any] Instructions / clock rate: at {f} MHz, each takes 1/{f} µs.
:::
:::

::: right
Right. Counting instructions and dividing by the clock rate is how firmware engineers estimate timing before measuring it.
:::
:::

::: check
kind: parts
given: q = 2048..4095 int; let S = 16*q

## sp holds 0x{S:hex4} when `f` is called. `f` makes a 16-byte frame and saves ra at 12(sp), then calls `g`, which does the same and calls `h`, a leaf that makes no frame.

::: part
answer: S - 32
base: 16
width: 4

### What does sp hold while `h` runs?

::: misses
- [S - 48] `h` is a leaf and makes no frame.
- [S + 32] The stack grows toward lower addresses.
- [any] Two frames of 16 bytes.
:::
:::

::: part
answer: S - 4
base: 16
width: 4

### At what address did `f` save its ra?

::: misses
- [S + 12] `f` first moves sp down 16, then saves at 12 above the new sp.
- [S - 16] The frame starts there; ra is 12 bytes above that.
- [any] (0x{S:hex4} − 16) + 12.
:::
:::

::: right
Right. Each frame sits just below its caller's, and returning pops them in reverse order.
:::
:::

::: check
kind: parts
given: k = 0..3 int; a = 16..255 int; let v = a - (floor(a/2^k) - 2*floor(a/2^(k + 1)))*2^k; let w = v + 2^k

## An 8-bit LED register holds 0x{v:hex2}. Firmware sets bit {k}, then flips all eight bits with an XOR.

::: part
answer: w
base: 16
width: 2

### What does it hold after setting bit {k}?

::: misses
- [2^k] Setting a bit keeps the others, so OR the mask with the old value instead of writing the mask alone.
- [any] OR in $2^{\given{k}}$.
:::
:::

::: part
answer: 255 - w
base: 16
width: 2

### What does it hold after the XOR with 0xFF?

::: misses
- [255 - v] Flip the value after setting bit {k}.
- [any] XOR with 0xFF flips every bit: 0xFF minus the value.
:::
:::

::: right
Right. Read-modify-write with OR, then XOR, each keeping track of all eight bits.
:::
:::

::: check
kind: parts
given: f = 8 | 16 | 32; T = 1 | 2 | 10; h = 30..80 int; F = 48 | 96 | 150

## A timer counting at {f} MHz interrupts a processor every {T} ms. The handler runs {h} instructions on a {F} MHz core with CPI 1.

::: part
answer: 1000*f*T
tolerance: 0

### How much does the handler add to TIMECMP each time?

::: misses
- [f*T] {f} MHz × {T} ms is {f} × 10⁶ × {T} × 10⁻³.
- [any] Counts = rate × time.
:::
:::

::: part
answer: h/F
unit: µs
tolerance: 0.5%

### How long does the handler take?

::: misses
- [any] {h} instructions at {F} million a second.
:::
:::

::: part
answer: 100*h/(F*T*1000)
unit: %
tolerance: 0.5%

### What percentage of the processor's time does it use?

::: misses
- [100*h/(F*T)] The period is {T} ms, which is {T} × 1000 µs.
- [any] Handler time / period.
:::
:::

::: right
Right. A short handler on a fast core costs a small fraction of a percent.
:::
:::

::: check
kind: parts
given: C = 4 | 8 | 32; B = 32 | 64; h = 1 | 2; r = 2 | 4; P = 60 | 100

## A direct-mapped cache holds {C} KiB in {B}-byte lines. It hits in {h} ns, misses cost {P} ns more, and {r}% of accesses miss.

::: part
answer: 1024*C/B
tolerance: 0

### How many lines does it have?

::: misses
- [C*1000/B] 1 KiB is 1024 bytes.
- [any] Size / line size.
:::
:::

::: part
answer: round(log(1024*C/B)/log(2))
tolerance: 0

### How many index bits?

::: misses
- [any] $\log_2$ of the number of lines.
:::
:::

::: part
answer: h + r/100*P
unit: ns
tolerance: 0.5%

### What's the average access time?

::: misses
- [r/100*P] Plus the hit time.
- [any] $t_\text{hit} + m \cdot t_\text{penalty}$.
:::
:::

::: right
Right. Size, line size and miss rate together set how much time memory costs.
:::
:::

::: check
kind: asm
given: a = -90..90 int; b = -90..90 int; c = -90..90 int; d = -90..90 int; e = -90..-1 int
expect: a0 = 5 - step(a) - step(b) - step(c) - step(d) - step(e)

## Count how many of the five words in `vals` are negative, and leave the count in a0.

```asm starter
.data
vals: .word a, b, c, d, e
.text
    # count the negative words in vals; leave the count in a0
    ebreak
```

```asm solution
.data
vals: .word a, b, c, d, e
.text
    la t0, vals
    li t1, 5            # words left
    li a0, 0
loop:
    beqz t1, done
    lw t2, 0(t0)
    bgez t2, next       # not negative: skip the count
    addi a0, a0, 1
next:
    addi t0, t0, 4
    addi t1, t1, -1
    j loop
done:
    ebreak
```

::: right
Right. A pointer that steps by 4, a counter, and a branch that skips the increment for the non-negative words.
:::
:::
::: check
kind: parts
given: L = 0.2 | 0.3; u = 0.3 | 0.5; B = 0.15 | 0.2; m = 0.05 | 0.1; r = 0.3 | 0.4; dm = 0.02 | 0.04; P = 40 | 100; f = 500 | 1000

## A five-stage pipeline at {f} MHz: {L} of instructions are loads, {u} of them used at once; {B} are branches, {m} of them mispredicted at 2 cycles; {r} of instructions access data, missing {dm} of the time at {P} cycles.

::: part
answer: 1 + L*u + B*m*2 + r*dm*P
tolerance: 0.5%

### What is the CPI?

::: misses
- [1 + L*u + B*m*2] Add the data-cache misses: {r} × {dm} × {P} cycles per instruction.
- [any] Not yet. 1 + load-use + mispredictions + misses.
:::
:::

::: part
answer: 1e9*(1 + L*u + B*m*2 + r*dm*P)/(f*1e6)
unit: s
tolerance: 0.5%

### How long do a billion instructions take?

::: misses
- [any] N × CPI / f.
:::
:::
:::

::: check
kind: parts
given: rs2 = 5 | 10 | 18; imm = 8 | 44 | 100 | 1000; let hi = floor(imm/32); let lo = imm - 32*hi

## Encode `sw x{rs2}, {imm}(sp)` (S-type: opcode 0x23, funct3 2, rs1 = x2).

::: part
answer: lo
tolerance: 0

### What goes in bits 11:7?

::: misses
- [rs2] In an S-type instruction bits 11:7 hold the low five immediate bits, since there's no rd.
- [any] Not yet. imm mod 32.
:::
:::

::: part
answer: hi*2^25 + rs2*2^20 + 2*2^15 + 2*2^12 + lo*2^7 + 35
base: 16
width: 8

### What is the whole word?

::: misses
- [any] imm[11:5] in 31:25, rs2, rs1 = 2, funct3 = 2, imm[4:0] in 11:7, opcode 0x23.
:::
:::
:::

::: check
kind: parts
given: v = 5000000 | 70000000; p = 300 | 1027; h = 0.98 | 0.995; M = 100

## Virtual address {v:hex8} is on a page that maps to physical page {p:hex5} (4 KiB pages). The TLB hits {h} of the time in 1 cycle; memory takes {M} cycles and the page table has 2 levels.

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

### What physical address does it become?

::: misses
- [any] PPN × 4096 + the virtual address's low 12 bits.
:::
:::

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

### What is the average time per access?

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

::: check
kind: parts
given: B = 115200 | 921600; d = 4 | 8 | 16; c = 60 | 100; f = 16 | 72

## A UART receives continuously at {B} baud (10 bit times a byte) into a FIFO that interrupts the {f} MHz processor once every {d} bytes, each interrupt costing {c} cycles plus 5 cycles per byte moved.

::: part
answer: B/10/d
tolerance: 0.5%

### How many interrupts a second?

::: misses
- [B/10] The FIFO collects {d} bytes per interrupt.
- [any] Not yet. Bytes per second over {d}.
:::
:::

::: part
answer: B/10*(c/d + 5)/(f*1e6)*100
unit: %
tolerance: 0.5%

### What percentage of the processor does receiving use?

::: misses
- [any] (interrupts × c + bytes × 5) / f.
:::
:::
:::

::: check
kind: parts
given: a = 3277 | 8192 | 16384; X = 10000 | 20000; let y1 = floor(a*X/32768)

## A smoothing filter runs `y = y + ((a * (x - y)) >> 15)` in Q15, with a = {a}, y starting at 0 and every input x = {X}.

::: part
answer: y1
tolerance: 0

### What is y after the first sample?

::: misses
- [a*X] Shift the product right by 15.
- [any] Not yet. ⌊aX/2^15⌋.
:::
:::

::: part
answer: y1 + floor(a*(X - y1)/32768)
tolerance: 0

### What is y after the second?

::: misses
- [2*y1] The second step works on the remaining difference, x − y.
- [any] Not yet. y₁ + ⌊a(X − y₁)/2^15⌋.
:::
:::
:::

::: check
kind: parts
given: l = 20 | 25; u = 40; b = 15; p = 2; r = 1.3; m = 2 | 4; P = 50 | 100

## A pipelined core: {l}% loads, {u}% of them used at once (1-cycle stall); {b}% branches, mispredicted 10% of the time ({p} cycles); {r} memory accesses per instruction, {m}% missing at {P} cycles.

::: part
answer: 1 + l/100*u/100 + b/100*0.1*p + r*m/100*P
tolerance: 0.5%

### What is the CPI?

::: misses
- [any] Add each stall source to the base of 1.
:::
:::

::: part
answer: (r*m/100*P)/(1 + l/100*u/100 + b/100*0.1*p + r*m/100*P)*100
unit: %
tolerance: 0.5%

### What share of the cycles are memory stalls?

::: misses
- [any] Memory stall cycles ÷ CPI.
:::
:::
:::

::: check
kind: parts
given: R = 100 | 500; N = 64 | 256; c = 60 | 80; f = 48 | 160

## A {R} ksample/s ADC feeds a {f} MHz core. An interrupt costs {c} cycles.

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

### What load does an interrupt per sample put on the core?

::: misses
- [any] Rate × cycles ÷ clock.
:::
:::

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

### And DMA with an interrupt per {N} samples?

::: misses
- [any] Divide by the block size.
:::
:::
:::

::: check
kind: parts
given: w = 12944691 | 1086686515; a = 50 | 300; b = 20 | 90

## The instruction {w:hex8} runs with a1 = {a} and a2 = {b}.

::: part
answer: floor(w/2^30) - 2*floor(w/2^31)
tolerance: 0

### What is bit 30, which chooses between `add` and `sub`?

::: misses
- [any] Shift right by 30, keep one bit.
:::
:::

::: part
answer: a + b - 2*b*(floor(w/2^30) - 2*floor(w/2^31))
tolerance: 0

### What does a0 receive?

::: misses
- [any] a1 + a2 for `add`, a1 − a2 for `sub`.
:::
:::
:::
:::
