Instruction sets and addressing modes

A processor's instruction set architecture (ISA) is the contract between hardware and software: the instructions, registers and memory model a program may use. x86-64 (most PCs), ARM (phones, Apple computers) and RISC-V (open, growing fast) are the big ones today. 8086 assembly, the start of x86, has its own course on StepVioz.

Instruction formats

An instruction is a binary number split into fields. A made-up 16-bit format:

Bits15–1211–87–43–0
Fieldopcodedestination registersource register 1source register 2

The opcode says what to do; the other fields say what to do it to. Shifts and masks pull the fields out:

OPCODES = {1: "ADD", 2: "SUB", 3: "AND", 4: "OR"}

for word in [0x1123, 0x3A57, 0x2FF0]:
    opcode = (word >> 12) & 0xF
    rd = (word >> 8) & 0xF
    rs = (word >> 4) & 0xF
    rt = word & 0xF
    print(hex(word), "=", format(word, "016b"), "->", OPCODES[opcode], "r" + str(rd) + ", r" + str(rs) + ", r" + str(rt))
Open in compiler

Real ISAs have several formats: MIPS and RISC-V use 32-bit instructions with R-type (three registers), I-type (two registers and a constant) and J-type (a jump address).

Addressing modes

Where does an operand come from?

ModeExampleThe operand is
ImmediateADDI r1, r1, 5the number 5, inside the instruction
RegisterADD r1, r2, r3in register r2
DirectLOAD 1000memory[1000]
Register indirectLW r1, 0(r2)memory[r2]
Indexed / displacementLW r1, 8(r2)memory[r2 + 8]: an array element or a struct field
PC-relativeBEQ r1, r2, -3used by branches: target = PC + offset
registers = {"r2": 100}
memory = {100: 7, 108: 42, 5: 99}

print("immediate 5      ->", 5)
print("direct [5]       ->", memory[5])
print("indirect (r2)    ->", memory[registers["r2"]])
print("displacement 8(r2) ->", memory[registers["r2"] + 8])
Open in compiler

RISC and CISC

FeatureRISCCISC
instructionsfew and simple, one sizemany, some complex, different sizes
memory accessonly loads and storesmost instructions may use memory
registersmany (32)fewer (8 to 16)
examplesARM, RISC-V, MIPSx86

RISC instructions are easy to pipeline (later in this course). Modern x86 processors translate their complex instructions into simple RISC-like steps inside the chip, so the two designs have met in the middle.

Common mistakes

  • Mixing up immediate and direct: 5 as a value versus the value at address 5.
  • Forgetting to mask after shifting: (word >> 8) still holds the opcode above the field; & 0xF keeps only 4 bits.
  • Thinking RISC means "slower": simpler instructions let each one run faster and overlap more.

Quick check

1. In the format above, what is the opcode of 0x3A57?
2. LW r1, 8(r2): which addressing mode reads the operand?
3. In a RISC design, which instructions may access memory?
4. ADDI r1, r1, 5. Where does the 5 come from?

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.