---
title: Midterm exam
heading: "Midterm exam: instructions, programs and the machine around them"
eyebrow: C8 · Midterm exam
type: problems
---

::: hook
Twelve problems from the first half of the course: the instruction cycle, RISC-V instructions and their encoding, memory and data layout, branches, functions and the stack, C and pointers, integer and fixed-point arithmetic, interrupts and I/O, linking and performance. Two of them ask you to write assembly. Give yourself two 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: N = 4 | 25; C = 1.25 | 1.8; f = 40 | 120

## A program runs {N} million instructions at an average CPI of {C} on a {f} MHz clock.

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

### How long does it take?

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

::: part
answer: f/C
unit: MIPS
tolerance: 0.5%

### What is its MIPS rate?

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

::: check
kind: parts
given: rd = 6 | 9 | 13; imm = 12 | 500 | 1999

## Consider `addi x{rd}, x0, {imm}` (opcode 0x13).

::: part
answer: imm*2^20 + rd*2^7 + 19
base: 16
width: 8

### What is its encoding?

::: misses
- [any] imm in 31:20, rd in 11:7, opcode in 6:0.
:::
:::

::: part
answer: imm*2^20
base: 16
width: 8

### What is the word with only its immediate field filled in, everything else 0?

::: misses
- [any] imm × $2^{20}$.
:::
:::
:::

::: check
kind: parts
given: w = 2596069104 | 3735928559 | 2882400001; k = 0 | 3

## The word {w:hex8} is stored at 0x2000 on this little-endian machine.

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

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

::: misses
- [floor(w/256^(3 - k)) - 256*floor(w/256^(4 - k))] That's big-endian order.
- [any] Not yet. The lowest byte is at the lowest address.
:::
:::

::: part
answer: floor(w/256^3) - 256*step(floor(w/256^3) - 127.5)
tolerance: 0

### What does `lb` from 0x2003 load, as a signed number?

::: misses
- [floor(w/256^3)] `lb` sign-extends: a byte of 0x80 or more is negative.
- [any] Not yet. The top byte, sign-extended.
:::
:::
:::

::: check
kind: parts
given: a = 4400 | 4800; d = 4 | 11; F = 6000 | 7000

## A loop's branch at {a:hex4} jumps back {d} instructions; elsewhere, `jal ra, f` at {a:hex4} + 8 calls f at {F:hex4}.

::: part
answer: -4*d
unit: bytes
tolerance: 0

### What offset does the branch hold?

::: misses
- [-d] Offsets count bytes.
- [4*d] Backward means negative.
- [any] Not yet. target − branch.
:::
:::

::: part
answer: a + 12
base: 16
width: 4

### What does the `jal` put in ra?

::: misses
- [a + 8] ra gets the address after the `jal`.
- [any] Not yet. The jal's address + 4.
:::
:::
:::

::: check
kind: parts
given: b = 4096 | 8192; i = 3 | 6 | 15

## `struct sample { int t; short v; short flags; }` is 8 bytes: t at offset 0, v at 4, flags at 6. An array of them starts at {b:hex4}.

::: part
answer: b + 8*i + 4
base: 16
width: 4

### At what address is `s[{i}].v`?

::: misses
- [b + 8*i] Add the field's offset.
- [b + 2*i + 4] Each element is 8 bytes.
- [any] Not yet. base + 8i + 4.
:::
:::

::: part
answer: 8*i
unit: bytes
tolerance: 0

### How far is `s[{i}]` from `s[0]`?

::: misses
- [any] 8 bytes per element.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 4 | 6; n = 10 | 30 | 200

## A recursive function saves ra and {k} s registers in a frame rounded up to 16 bytes, and recurses {n} levels deep. sp starts at 0x10000.

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

### How big is each frame?

::: misses
- [4*(k + 1)] Round up to 16.
- [4*k] Count ra too.
- [any] Not yet. 4 bytes each for ra and the s registers, rounded up to 16.
:::
:::

::: part
answer: 65536 - n*16*ceil(4*(k + 1)/16)
base: 16
width: 5

### What is sp at the deepest point?

::: misses
- [any] 0x10000 minus n frames.
:::
:::
:::

::: check
kind: parts
given: r = 0..255 int; k = 0 | 4 | 7; let bit = floor(r/2^k) - 2*floor(r/2^(k + 1))

## A control register holds {r:hex2}.

::: part
answer: r + 2^k*(1 - 2*bit)
base: 16
width: 2

### What does it hold after `r ^= (1 << {k})`?

::: misses
- [any] XOR flips bit {k}.
:::
:::

::: part
answer: r - 2^k*bit
base: 16
width: 2

### What would `r &= ~(1 << {k})` give instead?

::: misses
- [any] AND with the inverted mask clears bit {k}.
:::
:::
:::

::: check
kind: parts
given: x = 0.3 | 0.55 | -0.6; y = 0.25 | 0.4 | 0.9

## {x} and {y} are stored in Q15 and multiplied.

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

### What integer stores {x}?

::: misses
- [any] Multiply by $2^{15}$ and round.
:::
:::

::: part
answer: floor(round(x*32768)*round(y*32768)/32768)
tolerance: 0

### The integer product is shifted right arithmetically by 15. What results?

::: misses
- [round(x*32768)*round(y*32768)] That's the product with 30 fraction bits; shift it.
- [any] Not yet. $\lfloor ab/2^{15} \rfloor$.
:::
:::
:::

::: check
kind: parts
given: a = 9 | 25 | 47; b = 2 | 4

## Signed integer division on RISC-V.

::: part
answer: -floor(a/b)
tolerance: 0

### What does `div` give for −{a} ÷ {b}?

::: misses
- [-floor(a/b) - 1] That rounds down, as a shift would; `div` rounds toward zero.
- [any] Not yet. Toward zero.
:::
:::

::: part
answer: -(a - b*floor(a/b))
tolerance: 0

### What does `rem` give for −{a} and {b}?

::: misses
- [a - b*floor(a/b)] The remainder has the dividend's sign, negative here.
- [any] Not yet. dividend − divisor × quotient.
:::
:::
:::

::: check
kind: parts
given: R = 8000 | 48000 | 400000; c = 50 | 120; f = 24 | 96; n = 64 | 512

## Samples arrive at {R} per second on a {f} MHz processor whose interrupts cost {c} cycles.

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

### What load would an interrupt per sample put on it? (Over 100% means impossible.)

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

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

### With DMA in double-buffered blocks of {n}, how long does the processor have per block?

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

::: check
kind: parts
given: T = 22 | 48; R = 3 | 9; D = 2 | 5; B = 6 | 20; S = 2 | 4

## Firmware has {T} kB of code, {R} kB of constants, {D} kB of initialized globals, {B} kB of zeroed globals and a {S} kB stack.

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

### How much flash?

::: misses
- [T + R] The initialized globals' values are stored in flash.
- [any] Not yet. .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.55 | 0.8 | 0.95; s = 4 | 16

## A fraction {p} of a program's time is in a routine 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 most any speedup of that routine could give?

::: misses
- [any] Let s grow without limit.
:::
:::
:::

::: check
kind: parts
given: a = 0.5 | 0.6; l = 0.2 | 0.3; cl = 2 | 4; cb = 1.5 | 3; let b = 1 - a - l

## A program is {a} ALU instructions (1 cycle), {l} loads ({cl} cycles) and the rest branches ({cb} cycles on average).

::: part
answer: a + l*cl + b*cb
tolerance: 0.5%

### What is its CPI?

::: misses
- [any] $\sum F_i \cdot \text{CPI}_i$.
:::
:::

::: part
answer: b*cb/(a + l*cl + b*cb)*100
unit: %
tolerance: 0.5%

### What percentage of its cycles go to branches?

::: misses
- [b*100] That's the share of instructions, not of cycles.
- [any] Not yet. Branch cycles over all cycles.
:::
:::
:::

::: check
kind: asm
given: s1 = -1 | 1; s2 = -1 | 1; s3 = -1 | 1; s4 = -1 | 1; m1 = 1..60 int; m2 = 1..60 int; m3 = 1..60 int; m4 = 1..60 int; let v1 = s1*m1; let v2 = s2*m2; let v3 = s3*m3; let v4 = s4*m4
expect: a0 = m1 + m2 + m3 + m4

## Add up the absolute values of the four signed words in `vals` and leave the total in a0.

```asm starter
.data
vals: .word v1, v2, v3, v4
.text
    la t0, vals
    ebreak
```

```asm solution
.data
vals: .word v1, v2, v3, v4
.text
    la t0, vals
    li t1, 4
    li a0, 0
loop:
    beqz t1, done
    lw t2, 0(t0)
    bgez t2, add_it
    neg t2, t2
add_it:
    add a0, a0, t2
    addi t0, t0, 4
    addi t1, t1, -1
    j loop
done:
    ebreak
```

::: right
Right. A branch around the negation for values that are already positive.
:::
:::

::: check
kind: asm
given: c = 1..1023 int
setup: a0 = c
expect: a1 = floor(c*2500/1024)

## a0 holds a 10-bit ADC code; the reference is 2500 mV. Put the reading in millivolts in a1, rounded down.

```asm starter
    # the code is in a0
    ebreak
```

```asm solution
    li t0, 2500
    mul a1, a0, t0
    srli a1, a1, 10
    ebreak
```

::: right
Right. Multiply by the reference and shift right by the converter's 10 bits.
:::
:::

::: check
kind: parts
given: c = 1 | 3 | 5; t = 4 | 8

## On RV32, a struct holds {c} chars, then a {t}-byte field aligned to {t}, then one more char.

::: part
answer: t*ceil(c/t)
tolerance: 0

### At what offset does the {t}-byte field start?

::: misses
- [c] It must start at a multiple of {t}.
- [any] Round {c} up to a multiple of {t}.
:::
:::

::: part
answer: t*ceil((t*ceil(c/t) + t + 1)/t)
tolerance: 0

### How big is the struct?

::: misses
- [any] The last char, then round up to a multiple of {t}.
:::
:::
:::
:::
