S4 · Lesson 4 · Instruction set design
Operand models, encoding budgets, addressing, code density and the RISC–CISC trade
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.
- How a 32-bit instruction's bits are spent, against the number of registers R. A three-register (R-type) instruction needs 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.
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 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.
Name it
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 bits;
- an immediate with two register fields gets bits;
- a branch offset of b bits counted in units of g bytes reaches 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 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 for each.
Worked 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.
Show the solution
- 32 registers, R-type. Three 5-bit register fields take 15 bits, leaving 17 for the opcode (7), funct3 (3) and funct7 (7).
- 32 registers, I-type. so immediates reach ±2048.
- 64 registers, R-type. Three 6-bit fields take 18 bits, leaving 14, so funct7 would shrink to 4 bits.
- 64 registers, I-type. 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).
Worked 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?
Show the solution
- Average size. per instruction, 70% of the uncompressed size.
- What the cache holds. of uncompressed code fits in the 32 KB cache, so it misses less.
- 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.
Worked 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?
Show the solution
- New instruction count. Each group of three becomes one: of the original.
- Time. Time goes as IC × CPI:
- 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
How many bits do three register fields take with 32 registers?
Check
A 32-bit instruction has two register fields for 16 registers and a 10-bit opcode. How many bits are left for the immediate?
Check
A branch offset is a 13-bit signed number counted in units of 1 bytes. How far backward can a branch reach?
Check
A fraction 0.5 of a program's 32-bit instructions are replaced by 16-bit forms. What percentage of the original size is the code?
Check
On a load-store ISA, a program runs at CPI 1.2, and a fraction 0.15 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 3, and its clock period is 1.1 times as long. What's the register-memory version's time as a fraction of the load-store version's?
Check
In a variable-length ISA, 0.5 of instructions are 2 bytes, 0.25 are 4 bytes and the rest 6 bytes. What's the average length?
Check
A 32-bit ISA has 32 registers and spends 10 bits on opcode fields. Its branches store 13-bit offsets counted in 4-byte units.
(a)
How many bits do three register fields take?
(b)
How wide an immediate fits beside two register fields?
(c)
How far can a branch reach?
Check
A fraction c of a program's 32-bit instructions are replaced by 16-bit compressed forms.
Step 1
What fraction of the original code size remains?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many times more instructions does the same instruction cache hold?
Explain why
Why do load-store ISAs with fixed-length instructions suit pipelined, high-performance processors better than register-memory ISAs with variable lengths?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.