---
title: Problem set 3
heading: "Problem set 3: branches, loops, data and functions"
eyebrow: C8 · Problem set 3
type: problems
---

::: hook
These problems use lessons 9 to 15: branches and loops, arrays and strings in memory, the stack and the calling convention, and recursion. Instructions are 4 bytes, the stack starts at sp = 0x10000 and grows down, and frames are kept a multiple of 16 bytes.
:::

::: pool
pick: 12

::: check
kind: parts
given: s = 2 | 3 | 4; k = 4 | 5 | 7; n = 10 | 100 | 1000; f = 10 | 50

## A loop has {s} setup instructions, then a body of {k} instructions (its test included) that runs {n} times, and one final test that leaves the loop. Each instruction takes one cycle at {f} MHz.

::: part
answer: s + k*n + 1
tolerance: 0

### How many instructions run in all?

::: misses
- [s + k*n] Count the final test that leaves the loop.
- [k*n] Count the setup and the final test.
- [any] Not yet. setup + passes × body + 1.
:::
:::

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

### How long does it take?

::: misses
- [any] Instructions divided by the clock rate.
:::
:::
:::

::: check
kind: parts
given: a = 4200 | 4400 | 4800; d = 3 | 8 | 20

## A branch at {a:hex4} jumps back {d} instructions, to the top of its loop.

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

### What offset is stored in the branch?

::: misses
- [4*d] The target is behind the branch: the offset is negative.
- [-d] Offsets count bytes, 4 to an instruction.
- [any] Not yet. target − branch address.
:::
:::

::: part
answer: a - 4*d
base: 16
width: 4

### What is the target address?

::: misses
- [any] The branch's own address plus the offset.
:::
:::
:::

::: check
kind: parts
given: a = 4096 | 4352 | 4660; F = 5120 | 6144

## `jal ra, f` sits at {a:hex4}, and the function f starts at {F:hex4}.

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

### What does ra hold after the jump?

::: misses
- [a] ra gets the address of the next instruction, so the function returns past the call.
- [any] Not yet. PC + 4.
:::
:::

::: part
answer: F - a
unit: bytes
tolerance: 0

### What offset does the `jal` hold?

::: misses
- [any] Target minus the jump's own address.
:::
:::
:::

::: check
kind: parts
given: x = 1 | 5 | 100; y = 1 | 7 | 30

## a0 = −{x} and a1 = {y}.

::: part
answer: 2^32 - x
tolerance: 0

### What is a0 read as an unsigned number?

::: misses
- [any] $2^{32} - x$.
:::
:::

::: part
answer: 1
tolerance: 0

### What does `slt a2, a0, a1` put in a2?

::: misses
- [0] Signed, −{x} is less than {y}.
- [any] slt gives 1 when the first is less, as signed numbers.
:::
:::

::: part
answer: 0
tolerance: 0

### What does `sltu a2, a0, a1` put in a2?

::: misses
- [1] Unsigned, a0 is about four billion.
- [any] sltu compares as unsigned numbers.
:::
:::
:::

::: check
kind: parts
given: b = 4096 | 8192 | 12288; n = 5 | 16 | 100

## An array of {n} words starts at {b:hex4}, and a loop walks a pointer through it.

::: part
answer: b + 4*n
base: 16
width: 4

### Where does the end pointer, just past the last element, point?

::: misses
- [b + 4*(n - 1)] That's the last element itself; the end pointer is one past it.
- [b + n] Words are 4 bytes.
- [any] Not yet. base + 4n.
:::
:::

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

### Where is the last element?

::: misses
- [any] base + 4(n − 1).
:::
:::
:::

::: check
kind: parts
given: b = 4096 | 4352; L = 5 | 12 | 40

## A C string of {L} characters starts at {b:hex4}.

::: part
answer: b + L
base: 16
width: 4

### At what address is its terminating zero?

::: misses
- [b + L - 1] That's the last character; the zero comes after it.
- [any] Not yet. base + length.
:::
:::

::: part
answer: L + 1
unit: bytes
tolerance: 0

### How many bytes does it occupy?

::: misses
- [any] The characters and the zero.
:::
:::
:::

::: check
kind: parts
given: h = 32768 | 40000 | 50000 | 65535

## The halfword {h:hex4} is in memory.

::: part
answer: h - 65536
tolerance: 0

### What does `lh` load?

::: misses
- [h] That's `lhu`. `lh` sign-extends from bit 15.
- [any] Not yet. The top bit is set: subtract 65536.
:::
:::

::: part
answer: h
tolerance: 0

### What does `lhu` load?

::: misses
- [any] Zero-extended.
:::
:::
:::

::: check
kind: parts
given: k = 1 | 2 | 4 | 5 | 6

## A function calls other functions and uses {k} saved registers (s0 upward) for values that must survive those calls.

::: part
answer: k + 1
tolerance: 0

### How many registers must it save on the stack?

::: misses
- [k] It calls other functions, so ra too.
- [any] Not yet. The s registers it uses, and ra.
:::
:::

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

### How large is its frame, rounded up to a multiple of 16 bytes?

::: misses
- [4*(k + 1)] Round up to a multiple of 16.
- [any] Not yet. 4 bytes each, rounded up to 16.
:::
:::
:::

::: check
kind: parts
given: n = 5 | 20 | 100; f = 16 | 32

## A recursive function makes one {f}-byte frame per level and goes {n} levels deep before the base case, which makes no frame. sp starts at 0x10000.

::: part
answer: n*f
unit: bytes
tolerance: 0

### How much stack is in use at the deepest point?

::: misses
- [any] Levels times frame size.
:::
:::

::: part
answer: 65536 - n*f
base: 16
width: 5

### What is sp at the deepest point?

::: misses
- [65536 + n*f] The stack grows down.
- [any] Not yet. 0x10000 minus the frames.
:::
:::
:::

::: check
kind: parts
given: n = 6 | 8 | 10 | 12; let F = round(((1 + sqrt(5))/2)^(n + 1)/sqrt(5))

## The naive recursive Fibonacci computes F({n}).

::: part
answer: 2*F - 1
tolerance: 0

### How many calls does it make?

::: misses
- [n] That's the depth; the calls branch.
- [any] Not yet. $2F(n + 1) - 1$.
:::
:::

::: part
answer: n - 1
tolerance: 0

### How many additions does a loop starting from F(0) and F(1) need instead?

::: misses
- [n] F(2) takes the first addition.
- [any] One for each of F(2) to F(n).
:::
:::
:::

::: check
kind: parts
given: n = 3 | 10 | 50

## A count-down loop runs `addi t1, t1, -1` then `bnez t1, loop` each pass.

::: part
answer: n
tolerance: 0

### If t1 starts at {n}, how many passes does it make?

::: misses
- [n - 1] The pass that takes t1 to 0 counts too.
- [any] Not yet. One pass per decrement until t1 is 0.
:::
:::

::: part
answer: 2^32
tolerance: 0.001%

### If t1 starts at 0 by mistake, how many passes does it make?

::: misses
- [0] The body runs before the test: the first decrement makes t1 equal to −1, which isn't 0.
- [1] The first decrement makes t1 equal to −1, which isn't 0, so it keeps going.
- [any] Not yet. t1 has to wrap all the way round, through every 32-bit value.
:::
:::
:::

::: check
kind: parts
given: k = 9 | 10 | 12 | 14

## A function takes {k} word-sized arguments. The calling convention passes the first eight in a0 to a7 and the rest on the stack.

::: part
answer: k - 8
tolerance: 0

### How many arguments go on the stack?

::: misses
- [any] Those beyond the eighth.
:::
:::

::: part
answer: 4*(k - 8)
unit: bytes
tolerance: 0

### How many bytes of stack do they take?

::: misses
- [any] Four bytes each.
:::
:::
:::

::: check
kind: parts
given: n = 8 | 12 | 20; F = 16 | 32; S = 2048 | 4096

## A recursive `fact(n)` uses {F}-byte frames, and fact(1) returns without making one. The stack holds {S} bytes.

::: part
answer: (n - 1)*F
tolerance: 0

### How much stack does fact({n}) use at its deepest?

::: misses
- [n*F] fact(1) makes no frame.
- [any] {n} − 1 frames.
:::
:::

::: part
answer: floor(S/F) + 1
tolerance: 0

### What is the largest n whose recursion fits?

::: misses
- [floor(S/F)] fact(1) needs no frame, so one more level fits.
- [any] Frames that fit, plus one.
:::
:::
:::

::: check
kind: parts
given: k = 1 | 3 | 6; c = 0 | 1; L = 2 | 6

## A function uses {k} saved registers and {L} bytes of locals; it calls other functions if c = 1 (here c = {c}).

::: part
answer: k + c
tolerance: 0

### How many registers does it save in its frame?

::: misses
- [any] Its saved registers, plus ra if it calls.
:::
:::

::: part
answer: 16*ceil((4*(k + c) + L)/16)
tolerance: 0

### How big is its frame?

::: misses
- [4*(k + c) + L] Round up to a multiple of 16.
- [any] Bytes needed, rounded up to 16.
:::
:::
:::

::: check
kind: parts
given: n = 10 | 25 | 100

## A loop reverses a string of {n} bytes in place by swapping from both ends; each swap pass runs 8 instructions, after 4 to set up.

::: part
answer: floor(n/2)
tolerance: 0

### How many swaps does it make?

::: misses
- [n] Each swap handles two bytes.
- [any] Half the length, rounded down.
:::
:::

::: part
answer: 4 + 8*floor(n/2)
tolerance: 0

### How many instructions does it run?

::: misses
- [any] Setup plus 8 per swap.
:::
:::
:::
:::
