Skip to main content
Be Bitwise

Encoding Instructions

Registers & Instruction Sets · Foundations track

3 min read

In this module

Instructions Are Just Numbers

At the hardware level, every instruction is a binary number. The CPU never sees the words ADD or LOAD. It sees patterns of zeros and ones. Turning a human-readable instruction into its binary form is called encoding, and the resulting binary is machine code.

Opcode and Operands

A machine code instruction breaks down into fields:

  • Opcode identifies the operation (add, subtract, load, jump, etc.). This is the first thing the control unit looks at during the decode stage.
  • Operands specify what data to operate on. They might identify registers, memory addresses, or immediate values (constants embedded directly in the instruction).

A simple instruction format might use 4 bits for the opcode and 4 bits each for two register operands, fitting neatly into a 12- or 16-bit word.

Key Concept

Every instruction is encoded as a binary number with an opcode (what to do) and operands (what to do it to). This is the machine code that the CPU actually reads and executes.

Fixed-Width vs. Variable-Width

Fixed-width encoding gives every instruction the same number of bits. 64-bit ARM (AArch64) uses 32-bit instructions uniformly. Older 32-bit ARM code often uses the Thumb encoding instead, which mixes 16-bit and 32-bit instructions. This keeps the hardware simple. The CPU always knows exactly how many bits to fetch.

Variable-width encoding lets different instructions have different lengths. x86 instructions can range from 1 to 15 bytes. The format is more compact for simple instructions, but it complicates decoding: the CPU has to figure out where each instruction ends before it can locate the next one.

A Concrete Example

Design a tiny 16-bit instruction format:

text
| Opcode (4 bits) | Dest (4 bits) | Src1 (4 bits) | Src2 (4 bits) |

Say opcode 0001 is ADD, and our registers are numbered 0 through 15. The instruction ADD R1, R2 (add R2 to R1, store result in R1) encodes as:

text
0001 0001 0001 0010
ADD R1 R1 R2

In hexadecimal, that's 0x1112. This 16-bit number is what the CPU actually fetches from memory. The control unit pulls out the opcode, identifies the registers, and tells the ALU to add.

The visualiser below holds that same word, 0x1112. Read the 16 bits in groups of four from the left: opcode, Dest, Src1, Src2. Flip the rightmost bit and the hex becomes 0x1113, which in our format is ADD R1, R3. Each hex digit is one field, which is one reason people design formats on 4-bit boundaries.

215
214
213
212
211
210
29
28
27
26
25
24
23
22
21
20
Decimal4370
Hex0x1112
Binary0001000100010010

This Is What the CPU Sees

When people say "the computer only understands ones and zeros," this is exactly what they mean. Every high-level line of code you write eventually becomes sequences of encoded instructions like the one above. The CPU fetches these binary patterns, decodes them, and executes them, one instruction cycle at a time. Nothing underneath. This is the bottom.