---
title: Problem set 5
heading: "Problem set 5: functions, the stack and fixed point"
eyebrow: C8 · Problem set 5
type: problems
---

::: hook
These problems use lessons 14 to 23 with the weight on the newest: stack frames and recursion, compiling C, pointers, multiplication, division and fixed point, and the functions lab. The stack starts at sp = 0x10000 (65,536), frames are multiples of 16 bytes, and instructions run one per cycle unless a problem says otherwise.
:::

::: pool
pick: 12

::: check
kind: parts
given: k = 0 | 2 | 3 | 5; L = 8 | 20 | 40

## A function that calls other functions uses {k} saved registers and {L} bytes of local variables.

::: part
answer: 4*(k + 1) + L
tolerance: 0

### How many bytes does it need to store?

::: misses
- [4*k + L] It calls others, so ra must be saved too.
- [any] ra, the saved registers and the locals.
:::
:::

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

### How big is its frame?

::: misses
- [any] Round up to a multiple of 16.
:::
:::
:::

::: check
kind: parts
given: S = 4096 | 8192 | 65536; F = 16 | 32 | 48; d = 20 | 50

## A recursive function uses {F}-byte frames on a {S}-byte stack.

::: part
answer: d*F
tolerance: 0

### How much stack does recursion {d} levels deep use?

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

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

### How deep can it go before overflowing?

::: misses
- [any] Whole frames that fit.
:::
:::
:::

::: check
kind: parts
given: N = 1000 | 100000; b = 3 | 6 | 15; c = 4 | 6; f = 16 | 48

## A leaf function with a {b}-instruction body is called {N} times; each call adds {c} instructions (argument moves, `jal`, `ret`). The core runs at {f} MHz.

::: part
answer: N*(b + c)
tolerance: 0

### How many instructions do the calls run?

::: misses
- [N*b] Count the call overhead too.
- [any] $N(b + c)$.
:::
:::

::: part
answer: N*(b + c)/f
unit: µs
tolerance: 0.5%

### How long does that take?

::: misses
- [any] Instructions ÷ clock.
:::
:::
:::

::: check
kind: parts
given: S = 65536 | 32768; F = 16 | 32 | 64

## A function is entered with sp = {S}, makes a {F}-byte frame, and saves ra at the top of it.

::: part
answer: S - F
tolerance: 0

### What is sp inside the function?

::: misses
- [S + F] The stack grows down.
- [any] {S} − {F}.
:::
:::

::: part
answer: S - 4
tolerance: 0

### At what address is ra saved?

::: misses
- [S - F] That's the bottom of the frame; ra goes at the top, offset {F} − 4.
- [any] sp + {F} − 4.
:::
:::
:::

::: check
kind: parts
given: n = 8 | 12 | 15; x = 0.3 | 0.71 | -0.45

## A Q-format number has {n} fraction bits.

::: part
answer: round(x*2^n)
tolerance: 0

### What integer stores {x}?

::: misses
- [any] Multiply by $2^n$ and round.
:::
:::

::: part
answer: 2^(-n)
tolerance: 0.1%

### What is its resolution, the value of one unit?

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

::: check
kind: parts
given: a = 0.3 | 0.55 | -0.8; b = 0.6 | -0.25 | 0.9

## {a} and {b} are multiplied in Q15 with rounding: the product of the integers, plus $2^{14}$, shifted right by 15.

::: part
answer: floor((round(a*32768)*round(b*32768) + 16384)/32768)
tolerance: 0

### What integer results?

::: misses
- [any] Multiply, add $2^{14}$, shift right 15 (rounding down).
:::
:::

::: part
answer: floor((round(a*32768)*round(b*32768) + 16384)/32768)/32768
tolerance: 0.01%

### What value does it represent?

::: misses
- [any] Divide by $2^{15}$.
:::
:::
:::

::: check
kind: parts
given: x = 1234 | 9999 | 40003 | 65535

## A compiler divides an unsigned 16-bit x = {x} by 10 as (x × 52429) >> 19.

::: part
answer: x*52429
tolerance: 0

### What is the product?

::: misses
- [any] Multiply first.
:::
:::

::: part
answer: floor(x/10)
tolerance: 0

### What does the shift leave?

::: misses
- [x/10] Integer division drops the fraction.
- [any] The quotient, rounded down.
:::
:::
:::

::: check
kind: parts
given: N = 10 | 12 | 16; V = 2500 | 3300 | 5000; c = 777 | 2048 | 3001

## An {N}-bit ADC with a {V} mV reference reads {c}.

::: part
answer: floor(c*V/2^N)
unit: mV
tolerance: 0

### What does (c × V) >> N give, in mV?

::: misses
- [any] Multiply by the reference, then divide by $2^N$ and round down.
:::
:::

::: part
answer: c*V/2^N - floor(c*V/2^N)
unit: mV
tolerance: 1%

### How much does dropping the fraction lose?

::: misses
- [any] The exact value less the truncated one.
:::
:::
:::

::: check
kind: parts
given: n = 4 | 100 | 1000 | 65536

## {n} Q15 values, each between −1 and 1, are summed in an integer accumulator.

::: part
answer: ceil(log(n)/log(2) - 0.000001)
tolerance: 0

### How many guard bits above the 16 must it have so the sum can't overflow?

::: misses
- [any] The sum can reach {n} times the largest value: $\lceil\log_2 n\rceil$ more bits.
:::
:::

::: part
answer: 16 + ceil(log(n)/log(2) - 0.000001)
tolerance: 0

### How wide must the accumulator be?

::: misses
- [any] 16 plus the guard bits.
:::
:::
:::

::: check
kind: parts
given: x = 240 | 4097 | 43690 | 65535

## Two loops count the 1 bits of x = {x}.

::: part
answer: x - floor(x/2) - floor(x/4) - floor(x/8) - floor(x/16) - floor(x/32) - floor(x/64) - floor(x/128) - floor(x/256) - floor(x/512) - floor(x/1024) - floor(x/2048) - floor(x/4096) - floor(x/8192) - floor(x/16384) - floor(x/32768)
tolerance: 0

### How many passes does the `x & (x − 1)` loop make?

::: misses
- [any] One per 1 bit.
:::
:::

::: part
answer: floor(log(x)/log(2) + 0.000001) + 1
tolerance: 0

### How many does a loop make that tests the low bit and shifts right until x is 0?

::: misses
- [any] One per bit up to the highest 1.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 3 | 5; L = 3 | 40 | 100

## `x = p->next->...->val` follows {k} `next` pointers, then reads val; each load depends on the one before and takes {L} cycles.

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

### How many loads does it make?

::: misses
- [k] The final read of val is a load too.
- [any] One per arrow.
:::
:::

::: part
answer: (k + 1)*L
tolerance: 0

### How many cycles do the loads take?

::: misses
- [any] They can't overlap: each needs the previous address.
:::
:::
:::

::: check
kind: parts
given: a = 7 | 101 | 1001; k = 1 | 2 | 3

## −{a} is divided by 2 to the power {k}.

::: part
answer: -floor(a/2^k)
tolerance: 0

### What does `div` give (rounding toward zero)?

::: misses
- [-ceil(a/2^k)] That's what `srai` gives.
- [any] Divide and drop the fraction toward zero.
:::
:::

::: part
answer: -ceil(a/2^k)
tolerance: 0

### What does `srai` by {k} give?

::: misses
- [any] Arithmetic shifts round toward −∞.
:::
:::
:::

::: check
kind: parts
given: a = 0.5 | 0.3 | -0.75; b = 0.25 | 0.6

## Q31 numbers for {a} and {b} are multiplied with `mulh`, which keeps the high 32 bits of the 64-bit product.

::: part
answer: floor(round(a*2^31)*round(b*2^31)/2^32)/2^30
tolerance: 0.001%

### Read as a Q30 number, what value does `mulh` return?

::: misses
- [any] The product has 62 fraction bits; dropping 32 leaves 30.
:::
:::

::: part
answer: 1
tolerance: 0

### By how many bits must it be shifted left to be Q31 again?

::: misses
- [any] From 30 fraction bits to 31.
:::
:::
:::

::: check
kind: parts
given: n = 10 | 15 | 20

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

::: part
answer: 2*round(((1 + sqrt(5))/2)^(n + 1)/sqrt(5)) - 1
tolerance: 0

### How many calls does it make?

::: misses
- [any] $2F(n + 1) - 1$.
:::
:::

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

### How many frames are live at the deepest point, with F(1) and F(0) returning without one?

::: misses
- [any] One per level from F({n}) down to F(2).
:::
:::
:::

::: check
kind: parts
given: n = 10 | 20 | 30; f = 16 | 100

## Main sums the squares of 1 to {n} by calling `square` each time. Each pass of its loop runs 6 instructions plus the 4 in `square`, after 4 to set up and 2 to finish. The core runs at {f} MHz.

::: part
answer: 4 + 10*n + 1 + 2
tolerance: 0

### How many instructions run, counting the final failed test?

::: misses
- [any] Setup, 10 per pass, the last test, then the finish.
:::
:::

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

### How long does that take?

::: misses
- [any] Instructions ÷ clock.
:::
:::
:::
:::
