---
title: Problem set 1
heading: "Problem set 1: the instruction cycle, instructions and memory"
eyebrow: C8 · Problem set 1
type: problems
---

::: hook
These problems use lessons 1 to 7: the fetch, decode and execute cycle and the time a program takes, RISC-V instructions, carries and overflow, how instructions are encoded in 32 bits, CISC and RISC, and how memory is addressed and laid out. Addresses are bytes, words are 4 bytes, and the board's memory map is the one from lesson 6.
:::

::: pool
pick: 12

::: check
kind: parts
given: N = 2 | 5 | 40; C = 1.4 | 2 | 3; f = 50 | 100 | 400

## A program runs {N} million instructions at an average of {C} cycles each 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] Multiply by the cycles per instruction too.
- [any] $t = N \cdot \text{CPI}/f$.
:::
:::

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

### How many million instructions does it run per second?

::: misses
- [any] $f/\text{CPI}$.
:::
:::
:::

::: check
kind: parts
given: b = 4096 | 8192 | 256; k = 3 | 17 | 100

## A program's first instruction is at {b:hex4}, and it runs straight through with no branches.

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

### At what address is instruction number {k} (counting the first as 0)?

::: misses
- [b + k] Each instruction is 4 bytes.
- [any] Not yet. base + 4k.
:::
:::

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

### How many bytes do instructions 0 to {k} take?

::: misses
- [4*k] Count instruction 0 too.
- [any] Not yet. (k + 1) words of 4 bytes.
:::
:::
:::

::: check
kind: parts
given: rd = 5 | 6 | 10 | 11; imm = 1 | 25 | 300 | 2000

## Consider `addi x{rd}, x0, {imm}`.

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

### What is its 32-bit encoding?

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

::: part
answer: imm
tolerance: 0

### What does x{rd} hold after it runs?

::: misses
- [any] x0 always reads as 0.
:::
:::
:::

::: check
kind: parts
given: rd = 3 | 7 | 12; rs1 = 1 | 9 | 20; rs2 = 2 | 14 | 30

## Encode the R-type instructions below (opcode 0x33, funct3 = 0).

::: part
answer: rs2*2^20 + rs1*2^15 + rd*2^7 + 51
base: 16
width: 8

### `add x{rd}, x{rs1}, x{rs2}`

::: misses
- [any] funct7 = 0: rs2 in 24:20, rs1 in 19:15, rd in 11:7.
:::
:::

::: part
answer: 32*2^25 + rs2*2^20 + rs1*2^15 + rd*2^7 + 51
base: 16
width: 8

### `sub x{rd}, x{rs1}, x{rs2}`

::: misses
- [rs2*2^20 + rs1*2^15 + rd*2^7 + 51] That's `add`. `sub` sets funct7 to 0x20.
- [any] funct7 = 0x20 in bits 31:25.
:::
:::
:::

::: check
kind: parts
given: w = 8463619 | 2111123 | 13002547 | 10486419

## An instruction word reads {w:hex8}.

::: part
answer: floor(w/32768) - 32*floor(w/1048576)
tolerance: 0

### What register number is in its rs1 field (bits 19:15)?

::: misses
- [any] Shift right by 15 and keep 5 bits.
:::
:::

::: part
answer: floor(w/4096) - 8*floor(w/32768)
tolerance: 0

### What is its funct3 (bits 14:12)?

::: misses
- [any] Shift right by 12 and keep 3 bits.
:::
:::
:::

::: check
kind: parts
given: hi = 4660 | 74565 | 1000; lo = 291 | 1024 | 2000

## A program builds a constant with `lui t0, {hi}` then `addi t0, t0, {lo}`.

::: part
answer: hi*4096 + lo
tolerance: 0

### What value does t0 hold?

::: misses
- [hi + lo] `lui` puts its immediate in the upper 20 bits: times 4096.
- [any] Not yet. hi × 4096 + lo.
:::
:::

::: part
answer: hi*4096
tolerance: 0

### What did t0 hold after the `lui` alone?

::: misses
- [hi] `lui` loads the upper 20 bits: its immediate times 4096.
- [any] Not yet. hi × 4096.
:::
:::
:::

::: check
kind: parts
given: b = 4096 | 8192; i = 2 | 9 | 30; s = 1 | 2 | 4

## An array of {s}-byte elements starts at {b:hex4}.

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

### What is the address of element {i}?

::: misses
- [any] base + size × index.
:::
:::

::: part
answer: s
unit: bytes
tolerance: 0

### How far apart are neighboring elements?

::: misses
- [any] One element's size.
:::
:::
:::

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

## The word {w:hex8} is stored at 0x1000 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 0x100{k}?

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

::: part
answer: floor(w/256) - 256*floor(w/65536)
base: 16
width: 2

### What does `lbu` from 0x1001 give?

::: misses
- [any] The second-lowest byte.
:::
:::
:::

::: check
kind: parts
given: v = 128..255 int

## The byte {v:hex2} is in memory.

::: part
answer: v - 256
tolerance: 0

### What does `lb` load?

::: misses
- [v] That's `lbu`. `lb` sign-extends.
- [any] Not yet. The top bit is set: subtract 256.
:::
:::

::: part
answer: v
tolerance: 0

### What does `lbu` load?

::: misses
- [any] Zero-extended: the byte's unsigned value.
:::
:::
:::

::: check
kind: parts
given: n = 12 | 16 | 20 | 24

## A region of memory is addressed by the low {n} bits of the address.

::: part
answer: 2^n
unit: bytes
tolerance: 0

### How many bytes does it cover?

::: misses
- [any] $2^n$ bytes.
:::
:::

::: part
answer: 2^n/4
tolerance: 0

### How many 32-bit words is that?

::: misses
- [any] Four bytes a word.
:::
:::
:::

::: check
kind: parts
given: a = 4096 | 4100 | 4102 | 4105 | 4108

## A load uses the address {a:hex4}.

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

### How many bytes past the start of its word is it? (0 means it's aligned for `lw`.)

::: misses
- [any] The address modulo 4.
:::
:::

::: part
answer: 4*floor(a/4)
base: 16
width: 4

### At what address does the word containing it start?

::: misses
- [any] Round down to a multiple of 4.
:::
:::
:::

::: check
kind: parts
given: s = 65536 | 61440; n = 4 | 10 | 16

## The stack pointer starts at {s:hex5} and a function makes a frame of {n} words.

::: part
answer: s - 4*n
base: 16
width: 5

### What does sp hold inside the function?

::: misses
- [s + 4*n] The stack grows toward lower addresses.
- [s - n] Each word is 4 bytes.
- [any] Not yet. sp − 4n.
:::
:::

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

### How many bytes is the frame?

::: misses
- [any] 4 bytes a word.
:::
:::
:::

::: check
kind: parts
given: x = 7 | 25 | 100; y = 3 | 9 | 40

## t0 = {x} and t1 = {y} when the instructions below run, in order.

::: part
answer: x - y
tolerance: 0

### `sub t2, t0, t1` puts what in t2?

::: misses
- [y - x] The first source is subtracted from: t2 = t0 − t1.
- [any] Not yet. rd = rs1 − rs2.
:::
:::

::: part
answer: (x - y)*4
tolerance: 0

### `slli t3, t2, 2` then puts what in t3?

::: misses
- [any] Shifting left by 2 multiplies by 4.
:::
:::
:::

::: check
kind: parts
given: l1 = 4000000000 | 3500000000; l2 = 800000000 | 1200000000; h1 = 5 | 17; h2 = 2 | 9

## Two 64-bit numbers are held as high and low words: ({h1}, {l1}) and ({h2}, {l2}).

::: part
answer: l1 + l2 - 2^32
tolerance: 0

### What does `add` leave in the low word of the sum?

::: misses
- [l1 + l2] The low sum wraps past $2^{32}$.
- [any] Subtract $2^{32}$.
:::
:::

::: part
answer: h1 + h2 + 1
tolerance: 0

### What is the high word of the sum?

::: misses
- [h1 + h2] Add the carry that `sltu` found.
- [any] High words plus the carry.
:::
:::
:::

::: check
kind: parts
given: w = 4267806435 | 6457443 | 2159022307

## A branch instruction is {w:hex8}. (B-type: bit 31 is imm[12], bits 30:25 imm[10:5], bits 11:8 imm[4:1], bit 7 imm[11].)

::: part
answer: (floor(w/2^31)*4096 + (floor(w/128) - 2*floor(w/256))*2048 + (floor(w/2^25) - 64*floor(w/2^31))*32 + (floor(w/256) - 16*floor(w/4096))*2) - 8192*floor(w/2^31)
tolerance: 0

### What byte offset does it encode?

::: misses
- [any] Reassemble imm[12:1], append a 0, and sign-extend 13 bits.
:::
:::

::: part
answer: floor(w/4096) - 8*floor(w/32768)
tolerance: 0

### What is its funct3?

::: misses
- [any] Bits 14:12.
:::
:::
:::

::: check
kind: parts
given: I = 1 | 2; c1 = 1.6 | 2; r = 1.25 | 1.4; c2 = 1 | 1.2; f = 2 | 3

## A CISC design runs {I} billion instructions at CPI {c1}; a RISC design needs {r} times as many at CPI {c2}. Both run at {f} GHz.

::: part
answer: I*c1/f
unit: s
tolerance: 0.5%

### How long does the CISC design take?

::: misses
- [I/f] Multiply by the CPI.
- [any] Not yet. $I \times CPI/f$.
:::
:::

::: part
answer: r*I*c2/f
unit: s
tolerance: 0.5%

### And the RISC design?

::: misses
- [I*c2/f] It runs {r} times as many instructions.
- [any] Not yet. $rI \times CPI/f$.
:::
:::

::: right
Right. The iron law decides, not the instruction count alone.
:::
:::
:::
