Sub topic 1
What an instruction set architecture defines
Prerequisites
This chapter assumes you can read binary and hexadecimal numbers, know basic logic gates, and understand that a register holds a word and memory is addressed by number. You should also be comfortable reading Python.
The instruction set architecture
An instruction set architecture (ISA) is the contract between software and hardware. It defines the operations a processor performs, the registers and memory it exposes, the binary encoding of each instruction, and how an instruction names its operands. Two chips with different internal designs can run the same program if they implement the same ISA.
The stored-program model and the instruction cycle
In the stored-program model, instructions and data share one memory. The processor repeats the instruction cycle: fetch the instruction at the program counter (PC), decode it, execute it, and update the PC.
Instruction formats: zero, one, two and three addresses
An instruction format fixes how many operand addresses it names.
| Format | Example | Addresses named |
|---|---|---|
| Three-address | ADD D, A, B | 3 |
| Two-address | ADD A, B | 2 (A is also the result) |
| One-address | ADD B | 1 (accumulator is implicit) |
| Zero-address | ADD | 0 (operands come from a stack) |
Fewer addresses make instructions shorter but force more data movement.
Expanding opcodes and counting encodable instructions
A fixed-length opcode wastes bits when an instruction needs few operands. An expanding opcode gives frequent instructions short codes and reuses unused opcode prefixes for less frequent ones. For 16-bit instructions with a 4-bit opcode and two 6-bit address fields, 14 two-address codes (0000 to 1101) leave 2 prefixes free. Each prefix gives 2^6 = 64 one-address instructions, so 128 are possible. Using 100 of them for one-address instructions leaves 28 prefixes for zero-address use, giving 28 × 64 = 1792 zero-address instructions.
RISC versus CISC design choices
A CISC (complex instruction set computer) offers many instructions, variable lengths, and memory operands. A RISC (reduced instruction set computer) offers a small set of simple, fixed-length instructions, and most operations work on registers. RISC keeps decoding and pipelining simple. CISC can express a task in fewer instructions.
Addressing modes and why they exist
An addressing mode tells the processor how to find an operand. Modes let a short address field reach large memory and support arrays and pointers.
def fetch_operand(mode, x, regs, memory):
"""Return (effective_address, operand) for a simple addressing mode."""
if mode == "immediate":
return None, x
if mode == "register":
return None, regs[x]
if mode == "direct":
ea = x
elif mode == "indirect":
ea = memory[x]
elif mode == "register_indirect":
ea = regs[x]
elif mode == "indexed":
ea = x + regs["XR"]
else:
raise ValueError(mode)
return ea, memory[ea]
memory = {100: 500, 500: 7, 600: 42}
regs = {"R1": 100, "XR": 4}
print(fetch_operand("direct", 600, regs, memory)) # (600, 42)
print(fetch_operand("indirect", 100, regs, memory)) # (500, 7)
print(fetch_operand("register_indirect", "R1", regs, memory))# (100, 500)
print(fetch_operand("indexed", 596, regs, memory)) # (600, 42)Exercise
A 16-bit format has a 4-bit opcode, two 6-bit address fields, and 14 two-address codes in use. How many one-address instructions can the two free prefixes encode?
Show answer
2 × 2^6 = 128 one-address instructions.