TactivergeLite

Lite / Textbooks / Digital Logic and Computer Architecture / Chapter 7

7. Instructions and the datapath

Version 1 · Technically checked (automated checks only) · 5 source lessons · Full interactive version

Introduction

A processor is a state machine that reads its next job from memory. The instruction set architecture is the contract between software and hardware: which instructions exist, how they are encoded as numbers and what each one changes. This chapter encodes instructions, traces a short program cycle by cycle, and opens up the datapath and control unit that carry each instruction out.

Figure 7.1. An instruction is a number with fields (described in words)

An 8-bit instruction word split into a 4-bit opcode field and a 4-bit operand field, with the example 0001 1110: opcode 1 and operand 14. Below it, a loop of three boxes: fetch, then decode, then execute, then back to fetch.

Explanation

Built from 5 School lessons. Each part is the lesson's own explanation.

7.1 The contract between code and silicon

Code compiled years ago still runs on brand-new processors with completely different insides. What promise makes that possible?

What an ISA promises

An instruction set architecture defines: the registers visible to programs (Nibble has one, A, plus flags Z and C); the instructions and exactly what each does; their encoding as bits; how operands are found (addressing modes: a constant, a register, a memory address); and how memory and I/O look to software. Anything not in the contract is free for the hardware designer to change.

Architecture vs microarchitecture

Two chips implementing the same ISA can be utterly different inside: one takes 3 clock cycles per instruction, another overlaps many instructions in a pipeline. Both run the same program and get the same answers; only the speed differs. That separation is why software lasts longer than any one chip.

Design styles

Accumulator machines (like Nibble) keep one working register. Load–store / RISC machines (like RISC-V or the TV-16 teaching format) do arithmetic only between registers, with separate load and store instructions. CISC machines allow memory operands in arithmetic. Fewer, simpler instructions make hardware simpler; richer ones make programs shorter.

Common mix-ups

Open the Lite lesson: The contract between code and silicon

7.2 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?

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.

Common mix-ups

Open the Lite lesson: Instructions are just numbers

7.3 Tracing a program, counting cycles

Two programs give the same answer. One takes 12 clock cycles, the other 33. How do you know before you run them?

A trace table

List, for each instruction executed: the program counter, the instruction, and every register, flag and memory cell it changes. Only change what the instruction says it changes: STORE writes memory but leaves A and the flags alone. Most tracing mistakes are updating something an instruction doesn't touch.

Flags are how machines decide

Arithmetic sets flags as a side effect: Z (result was zero) and C (a carry fell off the top). A conditional jump like JNZ reads Z. In 'Count down', ADD 15 sets Z when A reaches 0; STORE in between doesn't disturb Z; JNZ then falls through to HALT.

Counting cycles

Cycles = Σ (instructions executed × cycles per instruction). A loop body of b instructions run n times costs b·n plus setup. In the stepper's model every instruction takes 3 cycles; real processors vary by instruction. The executed count, not the program length, decides speed: a 5-line loop can run thousands of instructions.

Common mix-ups

Open the Lite lesson: Tracing a program, counting cycles

7.4 Thinking in register transfers

Can you swap two registers in one clock cycle without a spare? In software you need a temporary variable. In hardware you don't. Why?

The notation

A ← A + B means: on the next clock edge, register A loads the value of A + B computed from the current outputs. A condition can guard it: if LOAD: R ← D. Several transfers listed for the same cycle all happen together. RTL hides gate-level detail while keeping exact timing.

Same edge, old values

Every register's input is computed from the values before the edge, and every register loads at the edge. So A ← B and B ← A swap; A ← B and B ← (A + B) steps a Fibonacci pair (3, 5 → 5, 8). Software executes lines in order, so the same two lines would give a different answer: a classic bug when translating between the two.

Datapath building blocks

Registers hold values (with a load-enable). Multiplexers choose which value goes where. The ALU computes (add, subtract, AND…). Buses carry values between them. A control unit sets the multiplexer selects, ALU operation and load-enables each cycle, deciding which transfers happen.

Common mix-ups

Open the Lite lesson: Thinking in register transfers

7.5 The control unit is a state machine

A processor's datapath can add, store and jump, but it does nothing on its own. What tells each part when to act?

Fetch, decode, execute as RTL

In the stepper's model each instruction takes three cycles. Fetch: IR ← ROM[PC]. Decode: PC ← PC + 1 while the control unit reads the opcode. Execute: the opcode's own transfer, such as A ← A + M[a] with flags updated, or if Z = 0: PC ← a. Then back to fetch.

Control signals

Each transfer happens because control lines are on: rom_read and ir_load in fetch; pc_inc in decode; for ADD, ram_read, alu_add, a_load and flags_load. The control unit is an FSM whose state is the step (fetch, decode, execute) and whose outputs, depending on that state and the opcode, are these signals.

Hard-wired or microcoded

A hard-wired control unit computes the signals with gates. A microcoded one stores them in a small memory addressed by (opcode, step): each row is one cycle's control word. Microcode is easier to change and fix; hard-wired is faster. Ben Eater's breadboard computer uses EEPROMs as microcode.

Common mix-ups

Open the Lite lesson: The control unit is a state machine

Worked examples

The contract between code and silicon: example 1

Write a Nibble program that sets M3 to M0 + M0.

Solution

LOAD 0 (A ← M0), ADD 0 (A ← A + M0), STORE 3 (M3 ← A), HALT. Four instructions, and the 4-bit result wraps if M0 ≥ 8.

Instructions are just numbers: example 1

Decode TV-16 word 0x2280.

Solution

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

Tracing a program, counting cycles: example 1

Trace 'Add two numbers' with M0 = 9, M1 = 8.

Solution

LOAD 0: A = 9, Z = 0. ADD 1: A = 17 mod 16 = 1, C = 1, Z = 0. STORE 2: M2 = 1. HALT. 4 instructions × 3 = 12 cycles, and C = 1 warns that the true answer, 17, didn't fit.

Thinking in register transfers: example 1

A = 2, B = 7. Each cycle: A ← A + B, B ← A. Find A and B after 2 cycles.

Solution

Cycle 1 uses old values: A = 9, B = 2. Cycle 2: A = 9 + 2 = 11, B = 9.

The control unit is a state machine: example 1

List the control signals for STORE 4 in each of its three cycles.

Solution

Fetch: rom_read, ir_load. Decode: pc_inc. Execute: ram_write (with address 4 from the IR's operand field). Flags are not loaded.

Interactive figures, in words

Figure 7.2 from Instructions are just numbers

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?

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.

Figure 7.3 from Tracing a program, counting cycles

Run 'Add two numbers' to the end and record the cycle count and the carry. Then step through 'Count down' one instruction (three clicks) at a time, writing a trace table: line, instruction, A, Z, M0. Check your predicted cycle count.

The stepper runs 'Add two numbers' in 4 instructions (12 cycles), leaving M2 = 1 with C = 1. 'Count down' from 3 executes LOAD, three rounds of ADD 15, STORE 0, JNZ 1, then HALT: 11 instructions, 33 cycles, ending with M0 = 0 and Z = 1.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. Which of these is NOT part of an ISA?
    Answer it in “The contract between code and silicon”
  2. An ISA has a 6-bit opcode field. At most how many different operations can it encode?
    Answer it in “The contract between code and silicon”
  3. Encode TV-16 'LDI R2, 5' (opcode 1, rd 2, rs 0, imm 5) and give the word in decimal.
    Answer it in “Instructions are just numbers”
  4. TV-16's immediate field is 6 bits, unsigned. What is the largest constant LDI can load in one instruction?
    Answer it in “Instructions are just numbers”
  5. A straight-line program executes 6 instructions on a machine that takes 3 cycles per instruction. How many cycles?
    Answer it in “Tracing a program, counting cycles”
  6. Why does A ← B, B ← A in one cycle swap correctly in hardware?
    Answer it in “Thinking in register transfers”
  7. In the stepper's model, what does the decode step change?
    Answer it in “The control unit is a state machine”
  8. A microcoded machine has 16 opcodes, 4 steps per instruction and a 12-bit control word. How many bits of microcode ROM?
    Answer it in “The control unit is a state machine”

Engineering applications

One program, many chips

Because the ISA is a contract, the same machine code runs on a small low-power core and on a large fast one. They differ in microarchitecture, not in what the instructions mean.

Firmware debugging

When a robot's controller misbehaves, engineers read the program counter and registers in a debugger. A trace table, like the ones in this chapter, is how they reason about what happened.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

Instruction set architecture (ISA)
The ISA is the contract between software and hardware: the instructions, registers, data types and addressing modes a processor promises to support. Field Guide
Fetch–decode–execute cycle
A processor runs a program by repeatedly fetching the instruction at the program counter, decoding what it means, executing it and moving the program counter on. Field Guide
Registers
Registers are a small number of very fast storage locations inside the processor that hold the values it is working on right now. Field Guide
ALU (arithmetic logic unit)
The ALU is the part of a processor that performs arithmetic (add, subtract) and logic (AND, OR, XOR, shifts) on values from registers, and sets status flags such as zero and carry. Field Guide
Address space
The address space is the range of addresses a processor can generate. With n address bits and byte addressing, it can reach 2^n bytes. Field Guide

Project: Design an instruction format

Design an 8-bit instruction format for a tiny processor with 4 registers that needs at least 6 operations. Decide how many bits go to the opcode and to the operands, justify the trade-off, and encode a three-instruction program in your format.

Deliverables

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

References

← 6. State machines and sequential logic8. Performance and pipelining →