Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 67 ยท Instruction Set Architecture

Addressing Modes

Learn how instructions specify operands: immediate, direct, indirect, register, indexed, base plus offset and PC-relative modes, with a Python simulation.

An instruction like "add" needs data to work on, and it has to say where that data is. The rule that turns the bits in an instruction's operand field into an actual value is called an addressing mode. The value might sit inside the instruction itself, in a register, or at some location in memory, and there are many ways to describe that location.

Addressing modes decide how naturally a processor handles the everyday structures of programs: variables, arrays, pointers, records and the call stack. In this lesson you will go through the standard modes one by one, simulate them in Python, and see which ones modern ISAs keep and why.

Effective address

For modes that involve memory, the central idea is the effective address (EA): the final memory address the processor uses, after any calculation. Each mode is simply a different recipe for computing the EA from the instruction's operand field and the machine's registers.

flowchart LR
  F["Operand field<br/>in the instruction"] --> EA{"Mode: how to<br/>compute the EA"}
  REG["Registers<br/>and PC"] --> EA
  EA --> MEM["Memory access<br/>at the EA"]
  MEM --> V["Operand value"]

The mode combines the instruction's field and the register state into an effective address.

The classic modes

Imagine an operand field x (and sometimes a second field y). The modes below differ in what they do with it.

Mode Notation Effective address Operand is
Immediate #42 none the constant 42 itself
Register R3 none contents of register R3
Direct (absolute) [10] 10 memory at address 10
Indirect [[11]] memory[11] memory at the address stored in cell 11
Register indirect [R1] contents of R1 memory at the address in R1
Indexed [20 + R2] 20 + contents of R2 memory at that sum
Base + offset (displacement) [R1 + 4] contents of R1 + 4 memory at that sum
PC-relative label PC + offset memory (or a jump target) near the instruction

A few observations:

  • Immediate is the fastest because no memory access is needed, but the constant is limited by the number of bits in the field (6 bits signed in TC16).
  • Register operands are also free of memory traffic, and are by far the most common in RISC code.
  • Direct needs the whole address inside the instruction, which is large in a 32- or 64-bit address space. It suits fixed global variables.
  • Indirect needs two memory accesses: first read the pointer, then read the data it points to. It implements pointers directly but is slow.
  • Register indirect gives the same pointer behavior with one memory access, because the pointer lives in a register.
  • Indexed and base + offset are the same calculation with the roles of constant and register swapped. By convention, indexed has the array's start address as the constant and a register as the index, while base + offset (displacement) has a register holding the base address and a small constant as offset. Many books use the names interchangeably.

Let us put them to work.

Python Evaluating the operand modes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the rows for direct 10 and indirect 11. The direct mode reads cell 10 and gets 99. The indirect mode reads cell 11, finds the address 20, and then has to read cell 20 to get 7: two memory reads. The last two rows show that [20 + R2] and [R1 + 4] land on the same cell, 24, by different routes.

The three memory cells involved in the indirect case look like this:

flowchart LR
  I["Instruction operand: 11"] --> C11["memory 11<br/>holds 20"]
  C11 -->|"points to"| C20["memory 20<br/>holds 7"]

Indirect addressing follows a pointer one level: the operand field names a cell that holds the address of the data.

Why arrays and pointers need more than direct addressing

Suppose a program must sum five numbers stored at addresses 8 to 12. With direct addressing alone every access names a fixed address, so the code needs five separate load instructions, one per element, and cannot be a loop. The address would have to change while the program runs, and early computers did exactly that by modifying their own instructions, a technique that is error-prone and clashes with modern protection.

Indexed and register-indirect modes solve the problem cleanly: the instruction stays the same, and a register changes. The loop body holds one load instruction, and each pass increments a register.

Python Arrays, pointers and records

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both loop styles give 66 (4 + 8 + 15 + 16 + 23). These patterns map directly onto C code:

  • a[i] is indexed (or base plus scaled index): array start plus i times the element size.
  • *p is register indirect: the pointer p sits in a register.
  • p->age is base + offset: the address in p plus the fixed offset of the field age in the record.
  • A local variable in a function is base + offset from the stack pointer or frame pointer, which is why function-call code is full of [sp + 8].

Some ISAs add convenient variations. x86 offers [base + index * scale + displacement], where the scale (1, 2, 4 or 8) matches the element size, so a[i] for 4-byte integers is one operand. ARM allows auto-increment modes that update the pointer register as part of the load, ideal for walking arrays and for push/pop on a stack.

PC-relative addressing

In PC-relative addressing the operand field is an offset added to the program counter. It is the standard mode for branches: a jump of "back 3 instructions" does not mention any absolute address.

Two benefits follow:

  • Short encodings. Most branches target a place only a few instructions away, so a small offset (6 bits in a TC16 branch) is enough, where an absolute address would need 12 bits or more.
  • Position independence. Code that uses only relative branches works wherever it is loaded in memory. Shared libraries and modern operating systems with address randomization depend on this.

In TC16 the target is PC + 1 + offset, because by the time the branch executes the PC has already advanced past it. Different ISAs make different choices about which PC value is used; always check the manual.

Python Absolute versus PC-relative branches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When the program is loaded at 100, both versions work. When the same machine code is loaded at address 300, the absolute branch still jumps to 102, which is the wrong place (it is now outside the program), while the relative branch lands on 302, exactly where it should. The cost of relative addressing is a tiny addition in the hardware, which is cheap.

Which modes do real ISAs keep?

Simplicity in the instruction set affects the datapath and the pipeline, so RISC designs support few modes, while older CISC designs supported dozens.

ISA Memory addressing Other
TC16 base + offset (lw, sw) register, immediate (addi), PC-relative (beq, bne), direct (j)
RISC-V base + 12-bit offset only PC-relative branches and jumps; indexing needs an add first
AArch64 base + offset, base + register (scaled), auto-increment PC-relative branches and literals
x86-64 [base + index * scale + disp], RIP-relative memory operands in most instructions
VAX (historic) about 20 modes, including multi-level indirect one reason it was hard to pipeline

The pattern is clear: the memory addressing modes that matter in practice are base + offset, possibly with a scaled index, plus PC-relative for control flow. Indirect modes are rare because a pointer in a register does the same thing with one memory access instead of two, and fewer, simpler modes make pipelining much easier, as you will see in Instruction Pipelining.

In TC16, lw r4, arr(r2) is the indexed pattern: the label arr becomes the 6-bit offset and r2 is the base register. The next lesson, Assembly Language Basics, uses this to loop over an array.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the effective address?

    Each addressing mode is a recipe for computing the address that the memory access really uses: the effective address.
  2. 2. How many memory reads does the memory-indirect mode need to fetch the operand value?

    The first read fetches the pointer stored in memory, the second reads the data at the address it contains.
  3. 3. Which mode is the best fit for stepping through the elements of an array in a loop?

    With a register that changes on every pass, the same load instruction touches a different element each time.
  4. 4. Why does PC-relative addressing make code position independent?

    A branch of three instructions backward remains three instructions backward after the whole program moves.
  5. 5. In C, which addressing mode does p->age (a field of a struct that p points to) correspond to?

    The register holding p provides the base address and the fixed position of age inside the record is the offset.

Summary

  • An addressing mode is the rule that turns an operand field into an operand value; for memory operands it computes an effective address.
  • Immediate and register operands need no memory access. Direct, indirect, register indirect, indexed and base + offset differ in how the address is built.
  • Indexed and register-indirect modes let one instruction handle arrays and pointers in loops. Base + offset handles record fields and stack variables.
  • PC-relative addressing gives short branch encodings and position-independent code.
  • RISC ISAs keep a few simple modes (base + offset, PC-relative); CISC ISAs such as x86 add scaled indexing and memory operands.

Continue with Assembly Language Basics.