TactivergeLite

Lite / Textbooks / Computer Architecture / Chapter 1

1. Instruction-set design

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

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.

Figure 1.1. Every bit is spent somewhere (described in words)

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

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

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

Open the Lite 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

Open the Lite 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

Open the Lite 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

Open the Lite 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, in words

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

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. Two processors implement the same ISA. One is three times faster. What can you say?
    Answer it in “The contract between code and silicon”
  3. 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”
  4. A designer wants 16 registers instead of 8 in TV-16, keeping 16-bit words and two register fields. What must shrink?
    Answer it in “Instructions are just numbers”
  5. Why state a loop invariant before writing the program?
    Answer it in “Project: program the processor”
  6. reg = 0x3C. After reg |= 0x81, what is reg in decimal?
    Answer it in “Registers and bit masks: where software touches silicon”
  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?
    Answer it in “Registers and bit masks: where software touches silicon”
  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?
    Answer it in “Registers and bit masks: where software touches silicon”

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)

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
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
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
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. Field Guide
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. 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

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

References

← Contents2. The performance equation →