# Decode

*Splitting the instruction and choosing what to do*

Decode is the phase that splits the fetched instruction into its opcode (which operation) and operand (its data), then uses the opcode to select which control lines the execute phase must raise.

Group: Processor
URL: https://digiwleea.wleeaf.dev/learn/decode-cycle/

[Fetch](https://digiwleea.wleeaf.dev/learn/fetch-cycle/) delivered an instruction byte into the [instruction register](https://digiwleea.wleeaf.dev/learn/instruction-register/). **Decode** is where the machine works out what that byte *means*. It has two jobs: carve the byte into its fields, and translate the operation field into the specific set of control lines the [execute phase](https://digiwleea.wleeaf.dev/learn/execute-cycle/) will need.

## Splitting the byte into fields

In our [machine code](https://digiwleea.wleeaf.dev/learn/machine-code/) an instruction byte is two nibbles: the high 4 bits are the **opcode** (the operation) and the low 4 bits are the **operand** (here a memory address). Splitting them needs no logic at all, it is just wiring: the top four bits go to the decode logic, the bottom four go to the address bus. The [instruction register](https://digiwleea.wleeaf.dev/learn/instruction-register/) already exposes them separately.

```
0x25  =  opcode 0x2 (ADD)  ,  operand 0x5 (address 5)
```

## Opcode to control lines

The second job is the real decoding. The opcode is a small number (`0x1`, `0x2`, `0x3`, `0xF` in our set), and the [control unit](https://digiwleea.wleeaf.dev/learn/control/) must turn it into a plan: which register drives the bus, which register loads, what the [ALU](https://digiwleea.wleeaf.dev/learn/alu/) computes. This is combinational: a [decoder](https://digiwleea.wleeaf.dev/learn/decoder/) turns the opcode into one-hot "this is a LOAD" / "this is an ADD" lines, and those select the right control pattern. Decode does not *do* anything to the data yet; it just decides what execute will do.

_Circuit diagram: Decode splitting an instruction (IR8): load a byte on I0-I7, pulse LD, and read the opcode on OP0-OP3 (high nibble) and operand on AR0-AR3 (low nibble). Open it in the lab, load 0010 0101 (0x25, "ADD 5"), and read opcode 0010 and operand 0101._

> **WARN:** **Common mistakes.** Decode is **combinational**: it computes a selection, it does not store or move data, and it takes no clock edge of its own in a simple machine (the split and the control-line logic just settle). Do not confuse the opcode (top nibble, the operation) with the operand (bottom nibble, the address). And decode only *chooses* the control lines; raising them and moving data is the [execute phase](https://digiwleea.wleeaf.dev/learn/execute-cycle/).

**Q (Try it):** Split the byte `0x25` into opcode and 4-bit operand, and name the operation (opcode `0x2` is ADD).

**A:** `0x25` in binary is `0010 0101`. The high nibble `0010` is the opcode `0x2`, which is **ADD**. The low nibble `0101` is the operand `0x5`, the memory address 5. So `0x25` decodes to "ADD the value at address 5 to the accumulator."

### FAQ

**Q:** What happens during the decode phase?

**A:** Decode splits the fetched instruction into its opcode (the operation) and operand (its data), then uses the opcode to select which control lines the [execute phase](https://digiwleea.wleeaf.dev/learn/execute-cycle/) must raise. It decides what to do without yet doing it.

**Q:** How does a CPU decode an opcode?

**A:** The opcode is a small number, and a [decoder](https://digiwleea.wleeaf.dev/learn/decoder/) turns it into one-hot signals (one line per instruction). Those lines, combined with the step counter, select the control pattern for that instruction. The whole thing is combinational logic, essentially a lookup from opcode to control lines.

**Q:** What is the difference between decode and execute?

**A:** Decode *chooses* what to do: it splits the instruction and selects the control lines. [Execute](https://digiwleea.wleeaf.dev/learn/execute-cycle/) *does* it: it raises those control lines so the datapath actually moves data and runs the ALU. Decode is a decision; execute is the action.

> **KEY:** With the opcode decoded into a control plan, the [execute phase](https://digiwleea.wleeaf.dev/learn/execute-cycle/) raises those lines and the datapath finally moves data and computes.
