Skip to content
Tactiverge

School/□ Systems Engineer/Week 25

Instructions are just numbers

The hex number 0x1405 can be a price, a colour or an instruction that puts 5 into register 2. How does the processor know which?

About 35 min · 3 goals · Checked by automated tests

1.Why should I care?

Debuggers, disassemblers, bootloaders and emulators all read raw instruction words. Encoding by hand once makes every later encounter with machine code, hex dumps and bit fields far less mysterious.

  • ◇ Split an instruction word into fields: opcode, registers and immediate.
  • ◇ Encode assembly into machine code and decode machine code back to assembly.
  • ◇ Reason about field widths: how many registers, operations and constant values fit.

2.Predict

In TV-16, the opcode is bits 15–12. The word is 0xA2C3. What is the opcode in decimal?

Guess first. Being wrong here is how the surprise works.

3.Discover

Encode LDI R2, 5 and ADD R1, R2, 0 and write down their hex words. Then type A2C3 into the decoder and explain each field. Finally switch to the Nibble format and encode LOAD 2: why is it 0x12?

Loading the interactive…

Can't use the interactive? Read a description instead

The encoder shows each bit of the word coloured by field. LDI R2, 5 encodes to 0x1405; ADD R1, R2, 0 to 0x2280; 0xA2C3 decodes to opcode 10 (ADDI), rd = 1, rs = 3, imm = 3. In the Nibble format LOAD 2 is opcode 1 and argument 2, so 0x12 = 18.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

Encode TV-16 'LDI R2, 5' (opcode 1, rd 2, rs 0, imm 5) and give the word in decimal.

Practice 2

What is the 8-bit Nibble machine code for STORE 7 (STORE has opcode 3), in decimal?

5.Understand

Fields

An instruction word is divided into fields. TV-16 (a teaching format made for these lessons) uses 16 bits: opcode bits 15–12 (16 operations), rd bits 11–9 and rs bits 8–6 (registers R0–R7), and imm bits 5–0 (a constant 0–63). Nibble packs an opcode nibble and a 4-bit argument into 8 bits: word = opcode × 16 + arg.

Encoding and decoding

To encode, shift each field to its position and combine: word = opcode·2¹² + rd·2⁹ + rs·2⁶ + imm. LDI R2, 5 = 1·4096 + 2·512 + 0 + 5 = 5125 = 0x1405. To decode, shift right and mask: rd = (word >> 9) AND 7. In hardware, decoding is just wiring: each field's bits run straight to the circuits that need them.

Bits are a budget

Every field width is a trade-off. More opcode bits allow more instructions; more register bits allow more registers; more immediate bits allow bigger constants. A 16-bit word can't have all three. That is why real ISAs have several formats and why loading a large constant can take two instructions.

Explain it another way

An instruction word is like a form with boxes: the first box says what to do, the next boxes say with what. The processor always reads the boxes in the same places.

Worked example 1

Decode TV-16 word 0x2280.

0010 0010 1000 0000: opcode 0010 = 2 (ADD), rd = 001 = R1, rs = 010 = R2, imm = 000000 = 0. So ADD R1, R2: R1 ← R1 + R2.

Common mix-ups

  • The processor can tell from the number itself whether it is an instruction.
    ✓ It can't. A word is executed if the program counter points at it; the same bits could be data anywhere else.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3 · a fresh version every time

TV-16 instructions are 16 bits: opcode = bits 15–12, rd = bits 11–9, rs = bits 8–6, imm = bits 5–0. The word is 0x5738. What is the rs field, in decimal?

Practice 4

TV-16's immediate field is 6 bits, unsigned. What is the largest constant LDI can load in one instruction?

Practice 5

A designer wants 16 registers instead of 8 in TV-16, keeping 16-bit words and two register fields. What must shrink?

Practice 6 · a fresh version every time

TV-16 instructions are 16 bits: opcode = bits 15–12, rd = bits 11–9, rs = bits 8–6, imm = bits 5–0. The word is 0xD6BB. What is the rs field, in decimal?

7.Apply it

Write a four-instruction TV-16 program that loads 5 and 7 into R1 and R2, adds them into R1 and halts. Encode each instruction in hex, then swap with a partner and decode theirs.

8.Reflect

Concepts: Instruction set architecture · Binary, hexadecimal and digital signals