---
title: Problem set 2
heading: "Problem set 2: loops, arrays, strings and structs"
eyebrow: C8 · Problem set 2
type: problems
---

::: hook
These problems use lessons 9 to 12: branches and loops, arrays and strings, how structs and unions are laid out, and the heap and linked structures. Words are 4 bytes, memory is little-endian, and instructions run one per cycle unless a problem says otherwise.
:::

::: pool
pick: 12

::: check
kind: parts
given: n = 10 | 50 | 200; b = 3 | 5 | 8

## A loop runs {n} times with a {b}-instruction body.

::: part
answer: n*(b + 2) + 1
tolerance: 0

### With the test at the top (a branch out, the body, a jump back), how many instructions run, counting the final failed test?

::: misses
- [n*(b + 2)] The test runs once more, to fail and leave.
- [any] Each pass is test, body and jump; then one last test.
:::
:::

::: part
answer: n*(b + 1)
tolerance: 0

### With the test at the bottom (the body, then a branch back while not done)?

::: misses
- [any] Body plus one branch per pass.
:::
:::
:::

::: check
kind: parts
given: A = 256 | 1024 | 4096; d = 3 | 7 | 12

## A branch at address {A} goes back {d} instructions to the top of a loop.

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

### What byte offset does it encode?

::: misses
- [-d] Offsets are in bytes: 4 per instruction.
- [4*d] It goes backward: the offset is negative.
- [any] $-4d$.
:::
:::

::: part
answer: A - 4*d
tolerance: 0

### What is the target address?

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

::: check
kind: parts
given: B = 8192 | 12288; R = 5 | 8 | 12; C = 6 | 16 | 25; i = 2 | 3; j = 1 | 4

## `short m[{R}][{C}]` starts at {B}, stored row by row.

::: part
answer: B + 2*(i*C + j)
tolerance: 0

### Where is m[{i}][{j}]?

::: misses
- [B + 4*(i*C + j)] A short is 2 bytes.
- [B + 2*(j*R + i)] C stores rows one after another.
- [any] base + 2(iC + j).
:::
:::

::: part
answer: 2*C
tolerance: 0

### How many bytes apart are m[i][j] and m[i + 1][j]?

::: misses
- [any] One row of {C} shorts.
:::
:::
:::

::: check
kind: parts
given: k = 3 | 8 | 20

## `strcmp` compares two strings byte by byte. They first differ at position {k} (counting from 0).

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

### How many passes does its loop make?

::: misses
- [k] The pass at the differing position counts too.
- [any] Positions 0 to {k}.
:::
:::

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

### How many byte loads does it make?

::: misses
- [any] Two per pass, one from each string.
:::
:::
:::

::: check
kind: parts
given: t = 2 | 4 | 8

## On RV32, `struct { char a; T b; char c; }` where T is a {t}-byte type aligned to {t}.

::: part
answer: 3*t
tolerance: 0

### How big is the struct?

::: misses
- [t + 2] b must be aligned, and the total rounds up to a multiple of {t}.
- [any] a at 0, b at {t}, c at 2·{t}, then round up.
:::
:::

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

### And with b first?

::: misses
- [any] b, then the two chars, rounded up to a multiple of {t}.
:::
:::
:::

::: check
kind: parts
given: B = 4096 | 8192; i = 2 | 6 | 15

## `struct rec { int id; short x; short y; char tag[8]; }` (20 bytes) is stored in an array from {B}.

::: part
answer: B + 20*i + 6
tolerance: 0

### What address does `recs[{i}].y` read?

::: misses
- [B + 20*i] Add y's offset: after the int and one short.
- [B + 20*i + 4] That's x; y is one short further.
- [any] base + 20i + 6.
:::
:::

::: part
answer: B + 20*i + 8 + 3
tolerance: 0

### And `recs[{i}].tag[3]`?

::: misses
- [any] tag starts at offset 8.
:::
:::
:::

::: check
kind: parts
given: w = 305419896 | 3735928559 | 2864434397; k = 1 | 2 | 3

## The word {w:hex8} is stored at 0x1000.

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

### What does `lbu` read from 0x100{k}?

::: misses
- [floor(w/256^(3 - k)) - 256*floor(w/256^(4 - k))] That's big-endian order.
- [any] Little-endian: byte {k} is bits $8k$ up.
:::
:::

::: part
answer: w - 256^2*floor(w/256^2)
tolerance: 0
base: 16
width: 4

### What does `lhu` read from 0x1000?

::: misses
- [any] The low two bytes.
:::
:::
:::

::: check
kind: parts
given: b0 = 1 | 18 | 192; b1 = 2 | 52; b2 = 3 | 86; b3 = 4 | 120

## A big-endian 32-bit field arrives as the bytes {b0}, {b1}, {b2}, {b3}.

::: part
answer: b0*2^24 + b1*2^16 + b2*2^8 + b3
tolerance: 0

### What value does it hold?

::: misses
- [b3*2^24 + b2*2^16 + b1*2^8 + b0] That's how a little-endian `lw` reads it.
- [any] The first byte is the most significant.
:::
:::

::: part
answer: b3*2^24 + b2*2^16 + b1*2^8 + b0
tolerance: 0

### What does `lw` read on RV32?

::: misses
- [any] Little-endian: the first byte becomes the low byte.
:::
:::
:::

::: check
kind: parts
given: k = 0 | 9 | 49; n = 50 | 100

## A linear search through {n} words runs 5 instructions a pass, plus 2 to set up. The value sits at index {k}.

::: part
answer: 5*(k + 1) + 2
tolerance: 0

### How many instructions does it run to find it?

::: misses
- [5*k + 2] The pass that finds it counts too.
- [any] {k} + 1 passes.
:::
:::

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

### And for a value that isn't there?

::: misses
- [any] Every pass runs.
:::
:::
:::

::: check
kind: parts
given: A = 4096 | 8200; k = 3 | 10 | 25

## `int *p` and `short *q` both hold {A}.

::: part
answer: A + 4*k
tolerance: 0

### What does `p + {k}` hold?

::: misses
- [A + k] C scales pointer arithmetic by the element size.
- [any] {A} + 4 · {k}.
:::
:::

::: part
answer: A + 2*k
tolerance: 0

### And `q + {k}`?

::: misses
- [any] Shorts are 2 bytes.
:::
:::
:::

::: check
kind: parts
given: w = 3735928559 | 305419896 | 4042322160; l = 4 | 8 | 12; m = 3 | 5

## A status word {w:hex8} holds a {m}-bit field starting at bit {l}.

::: part
answer: floor(w/2^l) - 2^m*floor(w/2^(l + m))
tolerance: 0

### What is the field's value?

::: misses
- [any] Shift right by {l}, then keep {m} bits.
:::
:::

::: part
answer: (2^m - 1)*2^l
tolerance: 0
base: 16
width: 8

### What mask selects it in place?

::: misses
- [2^m - 1] Shift the mask up to bit {l}.
- [any] {m} ones, shifted left by {l}.
:::
:::
:::

::: check
kind: parts
given: T = 8192 | 12288; x = 2 | 5; k = 6 | 10

## A `switch` on x with cases 0 to {k} − 1 compiles to a jump table of word addresses at {T}.

::: part
answer: T + 4*x
tolerance: 0

### Which address holds the target for x = {x}?

::: misses
- [T + x] Each entry is a 4-byte address.
- [any] base + 4x.
:::
:::

::: part
answer: 4*k
tolerance: 0

### How many bytes is the table?

::: misses
- [any] One word per case.
:::
:::
:::

::: check
kind: parts
given: n = 10 | 64; m = 8 | 32; b = 3 | 4

## A nested loop: the outer runs {n} times with 3 instructions of its own; the inner runs {m} times per outer pass, each pass {b} instructions plus 2 of loop overhead.

::: part
answer: n*(3 + m*(b + 2))
tolerance: 0

### How many instructions in all?

::: misses
- [n*m*b] Count both loops' overhead.
- [any] $n(3 + m(b + 2))$.
:::
:::

::: part
answer: n*m*2/(n*(3 + m*(b + 2)))*100
unit: %
tolerance: 0.5%

### What share of them is the inner loop's overhead?

::: misses
- [any] $2nm$ out of the total.
:::
:::
:::

::: check
kind: parts
given: n = 3 | 5; v = 900000000 | 1000000000

## A loop adds {n} copies of {v} into a 32-bit signed register.

::: part
answer: n*v - 2^32
tolerance: 0

### What does the register hold?

::: misses
- [n*v] The sum doesn't fit below $2^{31}$; it wraps once.
- [any] Subtract $2^{32}$.
:::
:::

::: part
answer: n*v
tolerance: 0

### What true sum should it have held?

::: misses
- [any] The plain product.
:::
:::
:::

::: check
kind: parts
given: n = 16 | 100 | 1000

## A word copy loop runs `lw`, `sw`, two pointer `addi`s, a count `addi` and a `bnez`, 6 instructions per word, after 3 to set up. It copies {n} words.

::: part
answer: 6*n + 3
tolerance: 0

### How many instructions does it run?

::: misses
- [any] Six per word, plus setup.
:::
:::

::: part
answer: 6*4*n + 3
tolerance: 0

### How many would a byte-by-byte copy of the same data take, with the same loop shape?

::: misses
- [any] Four times as many passes.
:::
:::
:::

::: check
kind: parts
given: d = 4 | 8 | 12; h = 8 | 16; n = 100 | 1000

## A list of {n} nodes holds {d} bytes of data and a 4-byte link per node, from an allocator with an {h}-byte header that rounds blocks to multiples of 8.

::: part
answer: n*8*ceil((d + 4 + h)/8)
unit: B
tolerance: 0.5%

### How many bytes of heap does the list use?

::: misses
- [n*(d + 4)] Include the header and the rounding.
- [any] Not yet. Round $d + 4 + h$ up to 8, times the node count.
:::
:::

::: part
answer: 8*ceil((d + 4 + h)/8)/d
tolerance: 0.5%

### How many times the size of a plain array of the same data is that?

::: misses
- [(d + 4)/d] Count the header and rounding too.
- [any] Not yet. Bytes per node over data bytes per node.
:::
:::

::: right
Right. Small linked nodes cost several times the memory of an array.
:::
:::
:::
