---
title: Instruction set design
heading: Operand models, encoding budgets, addressing, code density and the RISC–CISC trade
eyebrow: S4 · Lesson 4 · Instruction set design
xp: 50
---

::: hook
An instruction set is a contract that outlives every chip built to it: x86 code from 1985 still runs, so its design choices are still being paid for. Designing one means dividing a fixed number of bits among opcodes, registers and immediates, choosing how instructions reach memory, and balancing code density against how easily hardware can decode and pipeline the instructions. Modern ISAs such as RISC-V and ARMv8 settled on load-store designs with 32 registers and fixed 32-bit instructions, plus an optional compressed 16-bit form. This lesson works out why, with the arithmetic of encoding space and the iron law.
:::

::: play
- How a 32-bit instruction's bits are spent, against the number of registers R. A three-register (R-type) instruction needs $3\lceil\log_2R\rceil$ bits for registers; the rest (solid) is left for the opcode and function fields.
- An instruction with two registers and an immediate (I-type) keeps the same opcode field of k bits, and the immediate gets what's left (dashed).
- More registers mean fewer loads and stores, but each doubling costs three bits in R-type and two in the immediate. At 32 registers RISC-V has 17 bits of opcode space in R-type and 12-bit immediates.
:::
:::

```sim plot
view: graph
x: R = 4..256 log
y: b = 0..32 bits
slider: k = 5..12 step 1 = 10
plot: 32 - 3*ceil(log(R - 0.5)/log(2)) | samples: 400 | label: bits for opcode and function (R-type)
plot: 32 - 2*ceil(log(R - 0.5)/log(2)) - k | samples: 400 | dashed | color: ch2 | label: immediate bits (I-type)
```

::: predict
## A 32-bit ISA moves from 32 to 64 registers, keeping three-register instructions and a 10-bit opcode field in I-type instructions. What happens to the I-type immediate?

- [ ] It stays 12 bits
- [x] It shrinks from 12 to 10 bits
- [ ] It shrinks from 12 to 9 bits
- [ ] It grows

::: reveal
It loses 2 bits, one per register field: 32 − 2 × 6 − 10 = 10 bits, a range of ±512 instead of ±2048. More constants and offsets no longer fit, so some code needs extra instructions to build them. Whether the saved loads and stores pay for that is exactly the kind of question architects settle by measuring compiled code, which is how 32 registers became the common choice.
:::
:::

::: nameit
## Where operands live

| Class | Example | Operands |
|---|---|---|
| stack | JVM bytecode | top of a stack, implicit |
| accumulator | early micros | one register, implicit |
| register-memory | x86 | registers or memory in most instructions |
| load-store | RISC-V, ARM | only loads and stores touch memory |

Load-store ISAs need more instructions (higher IC) but every instruction has the same simple shape, which pipelines well (low CPI, short period). Modern x86 cores translate their instructions into load-store micro-operations internally.

## The encoding budget

With W-bit instructions, R registers and k opcode bits:

- three register fields take $3\lceil\log_2R\rceil$ bits;
- an immediate with two register fields gets $W - 2\lceil\log_2R\rceil - k$ bits;
- a branch offset of b bits counted in units of g bytes reaches $\pm 2^{b-1}g$ bytes (RISC-V: 12 stored bits, with an implicit 0 making 13, in bytes: ±4 KiB).

## Fixed and variable length

Fixed-length instructions decode in parallel and simply (the next instruction's start is known). Variable-length ones (x86: 1 to 15 bytes) pack code densely. RISC-V's **C extension** adds 16-bit forms of the most common instructions: with a fraction c compressible, code shrinks to $1 - c/2$ of its size, which effectively enlarges the instruction cache.

## The RISC–CISC trade in iron-law terms

A complex instruction that replaces k simple ones cuts IC, but if it raises CPI or lengthens the clock period, the program can end up slower: compare $\text{IC} \times \text{CPI} \times T$ for each.
:::

::: example
## Spend a 32-bit encoding with 32 and with 64 registers, keeping RISC-V's 7-bit opcode, 3-bit funct3 and a 10-bit opcode field for I-type.

::: solution
1. **32 registers, R-type.** Three 5-bit register fields take 15 bits, leaving 17 for the opcode (7), funct3 (3) and funct7 (7).
2. **32 registers, I-type.**
   $$32 - 10 - 10 = 12\ \text{bits}$$
   so immediates reach ±2048.
3. **64 registers, R-type.** Three 6-bit fields take 18 bits, leaving 14, so funct7 would shrink to 4 bits.
4. **64 registers, I-type.**
   $$32 - 12 - 10 = 10\ \text{bits}$$
   so immediates reach only ±512.

**Answer:** with 32 registers, R-type keeps a 7-bit funct7 and I-type gets 12-bit immediates (±2048); with 64 registers, funct7 shrinks to 4 bits and immediates to 10 bits (±512).
:::
:::

::: example
## 60% of a program's instructions have 16-bit compressed forms. How big is its code, and how much code does a 32 KB instruction cache now hold?

::: solution
1. **Average size.**
   $$\begin{aligned} \text{size} &= 0.4 \times 4 + 0.6 \times 2 \\ &= 2.8\ \text{bytes} \end{aligned}$$
   per instruction, 70% of the uncompressed size.
2. **What the cache holds.**
   $$32\ \text{KB} / 0.7 = 45.7\ \text{KB}$$
   of uncompressed code fits in the 32 KB cache, so it misses less.
3. **The cost.** The decoder pays a little complexity to find instruction boundaries, which is why compressed forms are only 16 or 32 bits, never arbitrary lengths.

**Answer:** the code shrinks to 2.8 bytes per instruction (70% of its old size), and the 32 KB cache holds what 45.7 KB of uncompressed code would have needed.
:::
:::

::: example
## In a program, 20% of the instructions come in groups of three that one new complex instruction could replace. Using it raises the CPI from 1.2 to 1.5 at the same clock. Is the program faster?

::: solution
1. **New instruction count.** Each group of three becomes one:
   $$\begin{aligned} \text{IC} &= 0.8 + 0.2/3 \\ &= 0.867 \end{aligned}$$
   of the original.
2. **Time.** Time goes as IC × CPI:
   $$\begin{aligned} \frac{t_\text{new}}{t_\text{old}} &= 0.867 \times \frac{1.5}{1.2} \\ &= 1.08 \end{aligned}$$
3. **Verdict.** A complex instruction pays only if it costs less than the CPI and clock it displaces.

**Answer:** no, the program is 8% slower.
:::
:::

::: check
given: R = 8 | 32 | 128
answer: 3*ceil(log(R - 0.5)/log(2))
tolerance: 0

## How many bits do three register fields take with {R} registers?

::: right
Right. $3\lceil\log_2R\rceil$ = {answer}.
:::

::: misses
- [ceil(log(R - 0.5)/log(2))] Each of the three fields needs its own bits.
- [any] Not yet. Bits per register name, times three.
:::
:::

::: check
given: W = 32 | 48; R = 16 | 32 | 64; k = 7 | 10
answer: W - 2*ceil(log(R - 0.5)/log(2)) - k
tolerance: 0

## A {W}-bit instruction has two register fields for {R} registers and a {k}-bit opcode. How many bits are left for the immediate?

::: right
Right. {answer} bits.
:::

::: misses
- [W - 3*ceil(log(R - 0.5)/log(2)) - k] I-type has two register fields.
- [any] Not yet. Subtract the register and opcode fields.
:::
:::

::: check
given: b = 12 | 13 | 21; g = 1 | 2 | 4
answer: 2^(b - 1)*g
unit: B
tolerance: 0

## A branch offset is a {b}-bit signed number counted in units of {g} bytes. How far backward can a branch reach?

::: right
Right. $2^{b-1}g$ = {answer} bytes; forward it reaches one unit less.
:::

::: misses
- [2^b*g] It's signed: half the range is backward.
- [any] Not yet. The most negative value, times the unit.
:::
:::

::: check
given: c = 0.3 | 0.5 | 0.7
answer: 100*(1 - c/2)
unit: %
tolerance: 0.5%

## A fraction {c} of a program's 32-bit instructions are replaced by 16-bit forms. What percentage of the original size is the code?

::: right
Right. $1 - c/2$ = {answer}%.
:::

::: misses
- [100*(1 - c)] A compressed instruction still takes 2 bytes.
- [any] Not yet. Average bytes per instruction over 4.
:::
:::

::: check
given: f = 0.15 | 0.25; C = 1.2 | 1.5; c = 2 | 3; t = 1 | 1.1
answer: ((1 - 2*f)*C + f*c)*t/C
tolerance: 0.5%

## On a load-store ISA, a program runs at CPI {C}, and a fraction {f} of its instructions are loads whose value is used only by the next instruction. A register-memory ISA fuses each such pair into one instruction with CPI {c}, and its clock period is {t} times as long. What's the register-memory version's time as a fraction of the load-store version's?

::: right
Right. Each pair of instructions becomes one: $\frac{((1 - 2f)C + fc)\,t}{C}$ = {answer}.
:::

::: misses
- [(1 - f)*t] Fewer instructions, but the fused ones take more cycles.
- [any] Not yet. Count cycles per original instruction on each machine, then scale by the clock period.
:::
:::

::: check
given: a = 0.5 | 0.6; b = 0.25 | 0.3; x = 1 | 2; y = 3 | 4
answer: a*x + b*y + (1 - a - b)*6
unit: B
tolerance: 0.5%

## In a variable-length ISA, {a} of instructions are {x} bytes, {b} are {y} bytes and the rest 6 bytes. What's the average length?

::: right
Right. {answer} bytes.
:::

::: misses
- [(x + y + 6)/3] Weight each length by its frequency.
- [any] Not yet. Weighted average.
:::
:::

::: check
kind: parts
given: R = 16 | 32 | 64; k = 10 | 17; b = 12 | 13; g = 2 | 4

## A 32-bit ISA has {R} registers and spends {k} bits on opcode fields. Its branches store {b}-bit offsets counted in {g}-byte units.

::: part
answer: 3*ceil(log(R)/log(2) - 1e-9)
unit: bits
tolerance: 0

### How many bits do three register fields take?

::: misses
- [any] $3\lceil\log_2R\rceil$.
:::
:::

::: part
answer: 32 - 2*ceil(log(R)/log(2) - 1e-9) - k
unit: bits
tolerance: 0

### How wide an immediate fits beside two register fields?

::: misses
- [any] $W - 2\lceil\log_2R\rceil - k$.
:::
:::

::: part
answer: 2^(b - 1)*g
unit: bytes
tolerance: 0

### How far can a branch reach?

::: misses
- [2^b*g] The offset is signed: $\pm2^{b-1}$ units.
- [any] $2^{b-1}g$.
:::
:::
:::

::: check
kind: derivation
vars: c = 0..0.9

## A fraction c of a program's 32-bit instructions are replaced by 16-bit compressed forms.

::: step
answer: 1 - c/2

### What fraction of the original code size remains?
:::

::: step
answer: 1/(1 - c/2)

### How many times more instructions does the same instruction cache hold?
:::

::: right
Right. Compressing 60% of instructions shrinks code by 30% and makes the cache act 43% larger.
:::
:::

::: explain
## Why do load-store ISAs with fixed-length instructions suit pipelined, high-performance processors better than register-memory ISAs with variable lengths?

::: model
A pipeline works best when every instruction can move through the same stages in the same way. In a load-store ISA, each instruction does at most one memory access, in a fixed stage, and its fields sit in fixed places, so decoding is simple and several instructions can be decoded in parallel because each one's start is known in advance. A register-memory instruction may read memory, compute and write memory, so its stages vary, and variable lengths mean the decoder can't find the next instruction until it has partly decoded this one. That makes fetching and decoding several instructions per cycle hard. The price of load-store is a higher instruction count, which compilers and register files with enough registers keep small.
:::
:::

::: practice

::: check
given: W = 32 | 40; R = 32 | 256; k = 6 | 8
answer: W - 2*ceil(log(R - 0.5)/log(2)) - k
tolerance: 0

## Immediate bits: {W}-bit instruction, {R} registers, {k}-bit opcode?

::: misses
- [any] $W - 2\lceil\log_2R\rceil - k$.
:::
:::

::: check
given: b = 10 | 20; g = 2 | 4
answer: 2^(b - 1)*g
unit: B
tolerance: 0

## Backward reach of a {b}-bit offset in {g}-byte units?

::: misses
- [any] $2^{b-1}g$.
:::
:::

::: check
given: c = 0.3 | 0.4 | 0.5 | 0.6 | 0.65 | 0.7 | 0.8
answer: 100*(1 - c/2)
unit: %
tolerance: 0.5%

## Code size with {c} compressed?

::: misses
- [any] $1 - c/2$.
:::
:::
:::
