---
title: CISC and RISC
heading: Two philosophies of instruction sets, and why the difference matters less inside the chip than on paper
eyebrow: C8 · Lesson 5 · CISC and RISC
xp: 40
---

::: hook
RISC-V instructions are all 32 bits, touch memory only through loads and stores, and each does one simple thing. The x86 instructions in most laptops and servers range from 1 to 15 bytes, can add a memory operand directly to a register, and include single instructions that copy whole strings. The first is called RISC (reduced instruction set computer) and the second CISC (complex instruction set computer). The argument between them shaped the 1980s; today both kinds of chip translate instructions into simple internal operations, and the real trade-offs, code size, decoding cost and how many instructions a program needs, can be put in numbers with the iron law of lesson 31.
:::

::: play
- The same statement, `a = b + c` with all three in memory, in RISC-V. It takes two loads, an add and a store: four 4-byte instructions, 16 bytes.
- An x86 compiler would write `mov eax, [b]`, `add eax, [c]`, `mov [a], eax`: three instructions, because `add` can read memory itself, in about 15 bytes.
- Step through and watch the memory change only at the store.
:::

```sim cpu
.data
a: .word 0
b: .word 1200
c: .word 345
.text
    la   t0, b
    lw   t1, 0(t0)        # load b
    la   t0, c
    lw   t2, 0(t0)        # load c
    add  t3, t1, t2       # b + c in a register
    la   t0, a
    sw   t3, 0(t0)        # store a
    ebreak
show: t1, t2, t3
memory: 0x1000..0x100C
speed: 2
```

::: predict
## A program runs 1.0 billion x86 instructions at an average of 1.5 cycles each. Compiled for RISC-V, it needs 1.3 billion instructions at 1.1 cycles each. Both chips run at 3 GHz. Which finishes first?

- [ ] x86, since it runs fewer instructions
- [x] RISC-V, by about 5%
- [ ] They tie
- [ ] RISC-V, by about 30%

::: reveal
Time is instructions × cycles per instruction ÷ clock rate. x86: $10^9 \times 1.5/(3 \times 10^9)$ = 0.500 s. RISC-V: $1.3 \times 10^9 \times 1.1/(3 \times 10^9)$ = 0.477 s, 4.7% faster. Fewer, more complex instructions only win if they don't cost proportionally more cycles; the product is what counts.
:::
:::

::: nameit
## Two philosophies

| | CISC (x86) | RISC (RISC-V, ARM) |
|---|---|---|
| Instruction length | 1 to 15 bytes | fixed 4 bytes (2 with compression) |
| Memory operands | most instructions can read or write memory | only loads and stores |
| Registers | 8 in the original, 16 in x86-64 | 32 |
| Addressing | many modes, such as base + index × scale + offset | base + offset |
| Decoding | sequential, complex | simple, parallel |

## What happened

In the 1980s, RISC designs ran faster because their simple, fixed instructions pipelined well (lesson 40). Then x86 processors began translating each instruction into simple internal **micro-operations** and running those on a RISC-like core, keeping compatibility while gaining most of the speed. What remains of the difference:

- **Decoding cost:** finding where variable-length instructions begin takes energy and limits how many can be decoded per cycle; x86 chips cache decoded micro-operations to avoid repeating it.
- **Code density:** variable-length code is often smaller, which saves instruction cache. RISC-V's compressed extension (RV32C) gives common instructions 16-bit forms and recovers most of the gap.
- **Instruction count:** memory operands and complex addressing let CISC do more per instruction, but each takes more cycles.

## The iron law

$$\text{time} = \text{instructions} \times \text{CPI} \times \text{cycle time}$$

An instruction set changes the first factor, the microarchitecture the second, and the circuit and process the third. Comparing two designs on any one factor alone is how marketing misleads.

## Average instruction size

If a fraction $p$ of instructions has a short form of $s$ bytes and the rest are $L$ bytes, the average is $ps + (1 - p)L$. With RV32C, about half to two-thirds of the instructions in typical code compress to 2 bytes.
:::

::: example
## A RISC-V program has 60% of its instructions in compressible forms. How much smaller is it with RV32C, and how does that compare with x86 code averaging 3.7 bytes per instruction for 20% fewer instructions?

::: solution
1. **Average size with RV32C.**
   $$\begin{aligned} \text{size} &= 0.6 \times 2 + 0.4 \times 4 \\ &= 2.8\ \text{bytes} \end{aligned}$$
   which is 30% smaller than 4 bytes.
2. **Per 100 RISC-V instructions.** That's 280 bytes compressed and 400 uncompressed.
3. **The x86 version.** It needs 80 instructions at 3.7 bytes:
   $$80 \times 3.7 = 296\ \text{bytes}$$
4. **Compare.** Compressed RISC-V is slightly smaller than x86 here, and uncompressed RISC-V is a third larger.
5. **What sets the density.** Code density comes down to the encoding more than to CISC versus RISC as such.

**Answer:** RV32C averages 2.8 bytes an instruction, 30% smaller; per 100 RISC-V instructions that's 280 bytes, against 296 for x86 and 400 uncompressed.
:::
:::

::: example
## Count the RISC-V instructions and bytes for `a[i] = a[i] + 5`, with the array's address in a0 and i in a1 (words), and compare an x86 `add dword [rdi + rsi*4], 5` of 4 bytes.

::: solution
1. **Write the RISC-V sequence.** RISC-V needs the address computed first, then a load, an add and a store.

```asm
    slli t0, a1, 2
    add  t0, a0, t0
    lw   t1, 0(t0)
    addi t1, t1, 5
    sw   t1, 0(t0)
```

2. **Count it.** Five instructions, 20 bytes (or 12 to 16 compressed, depending on which registers are used).
3. **The x86 version.** x86 does it in one 4-byte instruction, which the processor splits internally into a load, an add and a store, three micro-operations.
4. **Compare.** The work is the same; x86 packs it more densely, and RISC-V exposes each step to the compiler, which can reuse the address or the loaded value.

**Answer:** RISC-V takes five instructions, 20 bytes (12 to 16 compressed); x86 takes one 4-byte instruction that becomes three micro-operations.
:::
:::

::: example
## A chip decodes x86 instructions at 4 per cycle, but a loop body runs from its micro-operation cache at 6 per cycle. The loop has 600 instructions, 1.1 micro-operations each, and executes 10⁶ times at 3 GHz. If decode is the bottleneck, how much time does the cache save?

::: solution
1. **Cycles per iteration from the decoders.**
   $$\frac{600}{4} = 150\ \text{cycles}$$
2. **From the micro-op cache.** The 600 instructions are 660 micro-operations:
   $$\frac{660}{6} = 110\ \text{cycles}$$
3. **Time for a million iterations at 3 GHz.**
   $$\begin{aligned} t_\text{dec} &= \frac{150 \times 10^6}{3 \times 10^9}\ \text{s} \\ &= 50\ \text{ms} \\ t_\text{cache} &= \frac{110 \times 10^6}{3 \times 10^9}\ \text{s} \\ &= 36.7\ \text{ms} \end{aligned}$$
4. **The saving.** 13.3 ms, 27%.
5. **Why the cache exists.** Decoding x86 is expensive; a fixed-length RISC instruction set can decode many instructions per cycle directly.

**Answer:** the micro-op cache saves 13.3 ms, 27%: 36.7 ms against 50 ms.
:::
:::

::: check
given: I = 0.8 | 1 | 1.5; c = 1.2 | 1.5 | 1.8; f = 2.5 | 3 | 4
answer: I*c/f
unit: s
tolerance: 0.5%

## A program executes {I} billion instructions at an average CPI of {c} on a {f} GHz processor. How long does it take?

::: right
Right. Instructions × CPI ÷ clock rate = {answer}.
:::

::: misses
- [I*f/c] Time grows with CPI and shrinks with clock rate: $I \times CPI/f$.
- [I/f] Include the cycles per instruction.
- [any] Not yet. $t = I \times CPI/f$.
:::
:::

::: check
kind: parts
given: I1 = 1 | 1.2; c1 = 1.4 | 1.8; r = 1.2 | 1.3 | 1.4; c2 = 0.9 | 1.1 | 1.2

## Design A runs {I1} billion instructions at CPI {c1}. Design B needs {r} times as many instructions at CPI {c2}. Both run at the same clock.

::: part
answer: r*c2/c1
tolerance: 0.5%

### What is B's run time as a fraction of A's?

::: misses
- [r] The CPI matters too.
- [c2/c1] B runs more instructions as well.
- [any] Not yet. $(rI \cdot c_2)/(I \cdot c_1)$.
:::
:::

::: part
answer: c1/r
tolerance: 0.5%

### What CPI would B need to tie A?

::: misses
- [c1*r] B runs more instructions, so it needs a lower CPI.
- [any] Not yet. $r \cdot CPI_B = CPI_A$.
:::
:::

::: right
Right. An instruction set that needs more instructions must make each one cheaper to break even.
:::
:::

::: check
given: p = 0.4 | 0.6 | 0.7
answer: p*2 + (1 - p)*4
unit: B
tolerance: 0.5%

## In a RISC-V program, a fraction {p} of instructions compress to 2 bytes and the rest stay 4 bytes. What's the average instruction size?

::: right
Right. $2p + 4(1 - p)$ = {answer} bytes.
:::

::: misses
- [p*4 + (1 - p)*2] The compressed fraction gets the 2-byte size.
- [3] Weight each size by its fraction.
- [any] Not yet. $2p + 4(1 - p)$.
:::
:::

::: check
given: n = 400 | 600 | 900; u = 1.1 | 1.3; d = 4 | 5; k = 6 | 8
answer: n/d - n*u/k
tolerance: 0.5%

## A loop of {n} x86 instructions, {u} micro-operations each, is limited by decoding at {d} instructions per cycle. A micro-operation cache delivers {k} per cycle. How many cycles per iteration does the cache save?

::: right
Right. $n/d - nu/k$ = {answer}.
:::

::: misses
- [n/d - n/k] The cache delivers micro-operations, $u$ per instruction.
- [any] Not yet. Decoders: $n/d$ cycles. Cache: $nu/k$.
:::
:::

::: check
kind: asm
given: b = 1..1000 int; c = 1..1000 int
expect: mem[0x1000] = b + c

## Memory holds `a` at 0x1000, `b` at 0x1004 and `c` at 0x1008 (the starter stores them). Compute `a = b + c` with loads and a store, as a RISC machine must.

```asm starter
    li t0, 0x1000
    li t1, b
    sw t1, 4(t0)
    li t1, c
    sw t1, 8(t0)
    # your code: a = b + c
    ebreak
```

```asm solution
    li t0, 0x1000
    li t1, b
    sw t1, 4(t0)
    li t1, c
    sw t1, 8(t0)
    lw t2, 4(t0)
    lw t3, 8(t0)
    add t2, t2, t3
    sw t2, 0(t0)
    ebreak
```

::: right
Right. Load, load, add, store: on a load-store machine, arithmetic happens only between registers.
:::
:::

::: check
kind: choice

## Why do modern x86 processors translate instructions into micro-operations before executing them?

- [x] So the core can pipeline and schedule simple, uniform operations, as a RISC design does, while staying compatible with the old instruction set
- [ ] Because micro-operations use less memory
- [ ] To encrypt the instruction stream
- [ ] Because x86 instructions can't be executed directly by any hardware

::: right
Right. Complex, variable-length instructions are hard to pipeline. Splitting them into simple operations lets the execution core look like a RISC machine, keeping the instruction set for compatibility.
:::

::: misses
- [2] Micro-operations take more space, which is why they're cached separately.
- [3] Translation isn't about secrecy; it's about pipelining.
- [4] Early x86 chips did execute them directly, slowly. Translation is about speed.
:::
:::

::: check
kind: python
given: n = 1000 | 2000 | 2500 | 4000 | 5000 | 10000 | 20000

## Model code size. A program has `n` = {n} instructions; their kinds and proportions are: arithmetic 45% (compressible 70% of the time), loads and stores 30% (compressible 60%), branches and jumps 15% (compressible 50%), other 10% (never). Compute `size_rv32` (bytes, all 4-byte), `size_rvc` (bytes, compressed where possible, using the expected counts) and `saving` (the fractional reduction).

```python starter
size_rv32 = 4 * n
size_rvc = 2 * n
saving = 0.5
```

```python solution
mix = [(0.45, 0.70), (0.30, 0.60), (0.15, 0.50), (0.10, 0.0)]
size_rv32 = 4 * n
comp = sum(f * c for f, c in mix) * n
size_rvc = 2 * comp + 4 * (n - comp)
saving = 1 - size_rvc / size_rv32
```

```python tests
mix = [(0.45, 0.70), (0.30, 0.60), (0.15, 0.50), (0.10, 0.0)]
c = sum(f * k for f, k in mix) * n
assert size_rv32 == 4 * n, "Uncompressed, every instruction is 4 bytes."
assert abs(size_rvc - (2 * c + 4 * (n - c))) < 1e-9, f"Compressed size should be {2 * c + 4 * (n - c):.1f} bytes."
assert abs(saving - (1 - size_rvc / size_rv32)) < 1e-12 and abs(saving - 0.285) < 1e-9, "The saving is 28.5% for this mix."
```

::: right
Right. A program 28.5% smaller fits better in the instruction cache, which is the main reason RV32C exists.
:::
:::

::: explain
## RISC designs were once much faster than CISC ones; today high-end x86 and ARM processors perform similarly. Explain what changed, and what differences between the instruction sets still matter.

::: model
Early RISC chips won because fixed-length, load-store instructions were easy to pipeline: each took about one cycle in a simple pipeline, while CISC instructions took many cycles in microcode. When transistor budgets grew, x86 designers began translating each instruction into simple micro-operations and running those on an out-of-order core much like a RISC one, so the execution engines converged. What remains is at the edges: decoding variable-length x86 instructions costs energy and limits decode width, which is why x86 chips cache decoded micro-operations; code density affects instruction-cache use, where x86 and compressed RISC-V are similar; and simpler encodings make RISC designs cheaper to build and verify, which matters most for small, low-power cores.
:::
:::

::: practice

::: check
given: I = 2 | 5; c = 1.2 | 1.4; f = 1 | 2.5
answer: I*c/f
unit: s
tolerance: 0.5%

## How long do {I} billion instructions at CPI {c} take at {f} GHz?

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

::: check
given: p = 0.3 | 0.55 | 0.8
answer: 1 - (p*2 + (1 - p)*4)/4
tolerance: 0.5%

## If a fraction {p} of 4-byte instructions compress to 2 bytes, by what fraction does the program shrink?

::: misses
- [p] Each compressed instruction saves half its size: the saving is $p/2$.
- [any] $1 - (2p + 4(1 - p))/4 = p/2$.
:::
:::
:::
