Skip to content
Tactiverge

Textbooks/Computer Architecture/Chapter 1Lite (plain text) version

1Instruction-set design

Version 1 · Technically checked (automated checks only) · 4 source lessons · 8 practice items

Text
All chapters and search
  1. 1. Instruction-set design
  2. 2. The performance equation
  3. 3. Pipelining and hazards
  4. 4. Caches and the memory hierarchy
  5. 5. Input, output and the hardware–software boundary
  6. 6. Parallelism and acceleration
  7. 7. Energy and reduced precision
  8. 8. Measuring performance honestly

Introduction

An instruction set is a contract: software promises to use only these instructions, and hardware promises what each one does. Designing it is a game of trade-offs inside a fixed word. More registers mean fewer memory trips but smaller immediates; more opcodes mean richer instructions but fewer bits for operands. This chapter encodes instructions by hand, writes and times a small program, and meets the memory-mapped registers where software reaches the outside world.

8 registersopcode4 bitsrd3 bitsrs3 bitsimm6 bits16 opcodes · 8 registers · immediate 0 to 6316 registersopcode4 bitsrd4 bitsrs4 bitsimm4 bits16 opcodes · 16 registers · immediate 0 to 15Bits given to one field come from another.
Figure 1.1. Every bit is spent somewhere. Two 16-bit instruction formats with a 4-bit opcode (16 opcodes). With 8 registers, rd and rs take 3 bits each and the immediate gets 6 bits, values 0 to 63. With 16 registers they take 4 bits each and the immediate shrinks to 4 bits, values 0 to 15.

Explanation

This chapter is built from 4 School lessons. Each part below is the lesson's own explanation; open the lesson for its predict-and-discover activities.

1.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 →

1.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.

Open the full lesson: Instructions are just numbers →

1.3 Project: program the processor

The Nibble machine can't multiply. By the end of this lesson it will, using only adding, storing and one conditional jump.

Plan with a loop invariant

Before coding, state what stays true each time round the loop, for example: 'M2 + x × (remaining y) = x × original y'. Each pass adds x to M2 and takes 1 off y; when y reaches 0 the invariant gives M2 = x × y. Invariants turn 'I think it works' into an argument.

Test the edges

Test small ordinary cases (3 × 4), the identities (1 × 7), and the zeros (0 × 6, 6 × 0). In 4 bits, results above 15 wrap, and a loop that counts y down with ADD 15 behaves surprisingly when y starts at 0: trace it and explain what happens. The build's own notes describe the behaviour you should be able to justify.

Measure, don't guess

Record instructions executed for several y and fit a formula: instructions = b·y + s, where b is the loop body length and s the setup and HALT. Multiply by cycles per instruction (3 in the stepper's model) for cycles. This table is your 'before' measurement for the speed-up.

Common mix-ups

  • If it works for 3 × 4 it works.
    ✓ Zeros, ones and overflow are where loops fail; test them deliberately.

Open the full lesson: Project: program the processor →

1.4 Registers and bit masks: where software touches silicon

One line of C turns on a motor driver. Which single bit, at which address, did it change, and what else could it break?

Memory-mapped I/O

Microcontrollers give each peripheral (GPIO, timers, ADC, UART) a block of addresses. Writing to an address doesn't store data in RAM; it sets flip-flops whose outputs drive the hardware: a pin's direction, a timer's prescaler, an interrupt enable. Reading may return live hardware state, such as whether a conversion has finished.

The four mask operations

Set bit n: reg |= (1 << n). Clear: reg &= ~(1 << n). Toggle: reg ^= (1 << n). Test: (reg & (1 << n)) != 0. Multi-bit fields (a 3-bit prescaler at bits 4–6) are cleared with a field mask, then the new value is ORed in, shifted into place. Datasheets give each bit's meaning; the code should name them, not use magic numbers.

Read–modify–write hazards

reg |= mask is really three steps: read, OR, write. If an interrupt changes the same register between the read and the write, its change is lost. Fixes: disable interrupts briefly around the update, or use hardware 'set' and 'clear' registers that change only the bits you write as 1. Some status bits clear when read, so even reading has side effects.

Integration means checking both sides

A firmware unit test can't see a wrongly configured clock. Integration tests run real code on real (or accurately simulated) hardware and check observable behaviour: the pin toggles at the expected frequency, the ADC reads a known voltage correctly. Write down what each register should contain and check it in a debugger.

Common mix-ups

  • Writing to a register is like writing to a variable.
    ✓ It changes hardware immediately and can have side effects; reading can too.
  • reg |= mask is a single, safe operation.
    ✓ It is read–modify–write and can lose updates made by interrupts in between.

Open the full lesson: Registers and bit masks: where software touches silicon →

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.

Project: program the processor: example 1

A different looping program's measured step counts are 14 for y = 2 and 26 for y = 4. Find b and s.

Solution

Each extra y costs (26 − 14)/2 = 6 instructions, so b = 6; s = 14 − 2 × 6 = 2. N(y) = 6y + 2, and at 3 cycles per instruction, cycles = 18y + 6.

Registers and bit masks: where software touches silicon: example 1

reg = 0x5A (0101_1010). Clear bit 3, then toggle bit 0. Result?

Solution

Clear bit 3: 0101_1010 & 1111_0111 = 0101_0010 (0x52). Toggle bit 0: 0101_0011 = 0x53.

Interactive figures

Figure 1.2 from Instructions are just numbersEncode 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?
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.

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.

Laboratory

Everything runs in your browser. No kit, nothing mains-powered. Simulations are simplified educational models.

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.

Practice 1

Which of these is NOT part of an ISA?

Practice 2

Two processors implement the same ISA. One is three times faster. What can you say?

Practice 3

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

Practice 4

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

Practice 5

Why state a loop invariant before writing the program?

Practice 6

reg = 0x3C. After reg |= 0x81, what is reg in decimal?

Practice 7

A 3-bit prescaler field sits at bits 4–6 of an 8-bit register that currently holds 0x0F. You write the value 5 into the field. What does the register hold now, in decimal?

Practice 8

Main code does PORT |= LED_BIT while an interrupt handler sets MOTOR_BIT in the same register. Occasionally the motor bit is lost. Why?

  1. 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
  2. Two processors implement the same ISA. One is three times faster. What can you say?
    • They must have identical circuits
    • The same program gives the same results on both; only timing differs
    • Programs must be rewritten for the faster one
    • The faster one must have more instructions
  3. TV-16's immediate field is 6 bits, unsigned. What is the largest constant LDI can load in one instruction?
  4. A designer wants 16 registers instead of 8 in TV-16, keeping 16-bit words and two register fields. What must shrink?
    • Nothing
    • The opcode or immediate fields lose 2 bits in total
    • The word must become 15 bits
    • The registers must become smaller
  5. Why state a loop invariant before writing the program?
    • It makes the program run faster
    • It gives a reason the result is right when the loop ends, not just a hope
    • The assembler requires it
    • It replaces testing
  6. reg = 0x3C. After reg |= 0x81, what is reg in decimal?
  7. A 3-bit prescaler field sits at bits 4–6 of an 8-bit register that currently holds 0x0F. You write the value 5 into the field. What does the register hold now, in decimal?
  8. Main code does PORT |= LED_BIT while an interrupt handler sets MOTOR_BIT in the same register. Occasionally the motor bit is lost. Why?
    • The compiler is broken
    • The interrupt ran between main's read and write, so main wrote back the old value
    • Bits can't be shared
    • The LED draws too much current

Engineering applications

  • Compressed instructions

    Some instruction sets add a 16-bit form of the most common 32-bit instructions. Code shrinks by about a quarter, which matters when the program must fit in a small microcontroller's flash.

  • Peripheral registers

    A timer's prescaler, mode and enable bits share one register. Firmware changes one field with a read, mask and write, and must stop an interrupt from changing the same register halfway through.

Watch

Optional verified videos. Nothing loads from YouTube until you press play.

Shows instructions as bit patterns with an opcode and an address field, then runs a small program step by step.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Shows how a CPU reads machine code byte by byte, with a real chip and an Arduino as a probe.

49 min 13 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

Compares a small C program with the machine code it becomes.

10 min 1 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

  • Instructions & Programs: Crash Course Computer Science #8 (CrashCourse)
  • How do CPUs read machine code? — 6502 part 2 (Ben Eater)
  • Comparing C to machine language (Ben Eater)

Chapter summary

  • The ISA defines instructions, registers, data types and memory model; the microarchitecture is how one chip delivers them.
  • A fixed instruction width forces trade-offs between opcode, register and immediate fields.
  • A program's cost is instruction count, and a loop invariant is how you know it is correct.
  • Memory-mapped registers are set and cleared with masks; read-modify-write needs protection from interrupts.

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.
Registers
Registers are a small number of very fast storage locations inside the processor that hold the values it is working on right now.
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.
Memory-mapped I/O
With memory-mapped I/O, peripheral control and data registers appear at fixed memory addresses, so the processor talks to hardware with ordinary load and store instructions.
Hexadecimal
Hexadecimal is base 16, using digits 0–9 and A–F. Each hex digit stands for exactly four bits, so two hex digits describe one byte.

Definitions from the Field Guide.

Project: Design a 16-bit instruction format

Design an instruction format for a small controller with at least 12 operations. Choose the field widths, justify the register count, and encode a five-instruction program by hand. State one program your format makes awkward.

Deliverables

  • The format diagram with field widths
  • The encoded program in binary and hexadecimal
  • A trade-off note: what you gave up and why

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.
  • John L. Hennessy and David A. Patterson (2017). Computer Architecture: A Quantitative Approach, 6th edition. Morgan Kaufmann.

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.