Textbooks/Digital Logic and Computer Architecture/Chapter 7
7Instructions and the datapath
Version 1 · Technically checked (automated checks only) · 5 source lessons · 8 practice items
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.
Explanation
This chapter is built from 5 School lessons. Each part below is the lesson's own explanation; open the lesson for its predict-and-discover activities.
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
- A faster chip must have a better instruction set.
✓ Speed comes mostly from the microarchitecture; the same ISA can be implemented slowly or very fast. - Missing instructions make a machine less capable.
✓ Anything computable can be built from a small set; missing instructions cost speed or code size, not capability.
Open the full 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
- 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.
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
- A shorter program always runs faster.
✓ Speed depends on instructions executed, not lines written: a short loop can run for a long time. - Every instruction updates the flags.
✓ Only those the ISA says do; in Nibble STORE, JNZ and HALT leave them unchanged.
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
- Transfers written in the same cycle happen one after another.
✓ They all read old values and load together on the clock edge.
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
- The program counter is incremented after the instruction finishes.
✓ In many designs, including this model, it is incremented early (during decode) and a jump overwrites it later.
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
Interactive figure (online). Description: 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.
Interactive figure (online). Description: 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.
Laboratory
Everything runs in your browser. No kit, nothing mains-powered. Simulations are simplified educational models.
Silicon Studio stage
Silicon Studio: Instruction decoding
Decode five instruction words by hand, then check them in the studio.
Open Silicon Studio: Instruction decoding →Silicon Studio stage
Silicon Studio: Fetch, decode, execute
Step a program one cycle at a time and record the program counter and accumulator after each step.
Open Silicon Studio: Fetch, decode, execute →BUILD level
BUILD: Virtual computer, Level 4
Write a short countdown program and predict how many cycles it takes before you run it.
Open BUILD: Virtual computer, Level 4 →Nexus project
Nexus: Computer-controlled machine
Identify which parts of the machine are datapath and which are control.
Open Nexus: Computer-controlled machine →
Guided practice
Answers are checked on the server; wrong answers get a hint and another try. Signed in, solved items also count towards the source lesson.
- Which of these is NOT part of an ISA?
- The list of instructions
- The number of programmer-visible registers
- How many clock cycles the cache takes on a miss
- How instructions are encoded in bits
- An ISA has a 6-bit opcode field. At most how many different operations can it encode? (operations)
- Encode TV-16 'LDI R2, 5' (opcode 1, rd 2, rs 0, imm 5) and give the word in decimal.
- TV-16's immediate field is 6 bits, unsigned. What is the largest constant LDI can load in one instruction?
- A straight-line program executes 6 instructions on a machine that takes 3 cycles per instruction. How many cycles? (cycles)
- Why does A ← B, B ← A in one cycle swap correctly in hardware?
- The hardware secretly uses a temporary register
- Both registers sample their inputs at the same edge, from the old values
- A updates first, then B
- It doesn't; it gives A = B
- In the stepper's model, what does the decode step change?
- A
- The flags
- PC ← PC + 1
- Memory
- A microcoded machine has 16 opcodes, 4 steps per instruction and a 12-bit control word. How many bits of microcode ROM? (bits)
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 verified videos. Nothing loads from YouTube until you press play.
- Instructions & Programs: Crash Course Computer Science #8 (CrashCourse)
- 8-bit CPU control logic: Part 1 (Ben Eater)
Chapter summary
- An ISA defines the instructions, registers, data types and encoding that software may rely on; how they are implemented is the microarchitecture.
- An instruction word is divided into fields; k bits of opcode allow 2^k different operations.
- Register-transfer notation such as R1 ← R1 + R2 describes what happens on one clock edge.
- The control unit is a state machine that turns the opcode into control signals for the datapath.
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.
- 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.
- Registers
- Registers are a small number of very fast storage locations inside the processor that hold the values it is working on right now.
- 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.
- 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.
Definitions from the 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
- A field diagram with bit counts
- A table of opcodes
- The encoded program in binary and hexadecimal
- One thing your format cannot do, stated honestly
Keep your work in your Engineering Journal; it stays private unless you choose to share it.
References
- David A. Patterson and John L. Hennessy (2017). Computer Organization and Design RISC-V Edition: The Hardware Software Interface. Morgan Kaufmann.
- Noam Nisan and Shimon Schocken (2021). The Elements of Computing Systems: Building a Modern Computer from First Principles, 2nd edition. MIT Press. Link ↗
- MIT OpenCourseWare (2017). 6.004 Computation Structures (Spring 2017). Massachusetts Institute of Technology. Link ↗
Publication record
- Version
- 1
- Status
- Technically checked (automated checks only)
- Technical review
- Automated checks only, last run 2026-11-29. No human, educator or mentor review is claimed.src/content/textbooks/textbooks.test.ts: schema, every lesson, diagram, practice item, video, Field Guide entry, lab and reference resolves; figure numbers recomputed; no practice answers in client payloads.
- Written by
- Compiled and written by the Tactiverge team with AI assistance, from Tactiverge School lessons that were themselves written with AI assistance.
- Licence
- All rights reserved by the founders unless a licence is approved.
- Corrections
- None recorded yet.Spotted a mistake? Use “Report a problem” on any chapter, or see the corrections page.