TactivergeLite

Lite / Engineering Complex Systems / 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 · Full interactive version

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.

Predict

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

  1. 2
  2. 10
  3. 12
  4. 163
Guess first, then reveal

Bits 15–12 are the top hex digit: A = 1010₂ = 10, which TV-16 calls ADDI. When a field lines up with a hex digit it reads straight off; fields that straddle digits need binary.

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?

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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

Intuitive

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.

Technical

Fixed-length encodings (RISC-V, TV-16) make decoding fast and parallel. Variable-length ones (x86) save memory but need more complex decoders. Keeping register fields in the same position across formats lets hardware read registers before the opcode is fully decoded.

Mathematical

field = ⌊word / 2^lo⌋ mod 2^(hi−lo+1). 0xA2C3 = 41667: opcode = ⌊41667/4096⌋ = 10, rd = ⌊41667/512⌋ mod 8 = 1, rs = ⌊41667/64⌋ mod 8 = 3, imm = 41667 mod 64 = 3.

Applied

When a microcontroller crashes, the debugger shows the faulting address and a hex word; decoding that word tells you which instruction faulted and with which registers.

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

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

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.

Reflect

Why is fixed-length encoding easier for hardware than variable-length?

Write your answer on paper or in your own notes. Lite never stores what you write.

← The contract between code and siliconTracing a program, counting cycles →