Show understanding of and be able to use different modes of addressing

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Assembly Language: Addressing Modes

4.2 Assembly Language – Addressing Modes

In assembly language the addressing mode determines how the operand of an instruction is obtained. Understanding the different modes is essential for writing efficient low‑level code and for interpreting machine code generated by a compiler.

Why Addressing Modes Matter

  • They affect the speed and size of the generated program.
  • Different CPUs provide different sets of modes; choosing the appropriate one can reduce the number of required instructions.
  • Correct use of modes is required for accessing data structures such as arrays, records, and stacks.

Common Addressing Modes

The table below summarises the most frequently used addressing modes in typical 8‑bit and 32‑bit architectures (e.g., 8085, 8086, ARM, MIPS).

ModeSyntax (example)DescriptionTypical Use
ImmediateMO \cdot R0, #5The operand is a constant value encoded directly in the instruction.Loading constants, setting flags.
Direct (Absolute)LDA 0x3C00The address of the operand is given explicitly in the instruction.Accessing fixed variables, I/O ports.
RegisterADD R1, R2Both source and destination are registers; no memory reference is needed.Arithmetic, logical operations.
Register IndirectLDR R0, [R1]The register contains the memory address of the operand.Pointer dereferencing, stack access.
IndexedLDA 0x2000(R2)Effective address = base address + index register.Array element access.
Based‑Indexed with DisplacementLDR R0, [R1 + R2*4 + 8]Effective address = base + (index × scale) + displacement.Structure field access, complex array indexing.
Relative (PC‑relative)BR labelTarget address = current program counter + signed offset.Branching, loops, function calls.
Stack‑relativePUSH R3 / POP R4Implicitly uses the stack pointer as the address register.Procedure call/return, local variable storage.

Calculating Effective Addresses

For modes that involve a base, index, scale and displacement, the effective address (EA) is computed as:

\$\text{EA} = \text{Base} + (\text{Index} \times \text{Scale}) + \text{Displacement}\$

Where:

  • Base – contents of a base register (often a pointer to a data structure).
  • Index – contents of an index register (often a loop counter).
  • Scale – a constant factor (1, 2, 4, or 8) that reflects the size of each element.
  • Displacement – a signed constant added to the address.

Example: Accessing an Array Element

Assume an integer array int a[10]; stored starting at address 0x1000. To load a[i] into register R0 on a 32‑bit architecture that uses a base‑indexed mode:

    ; R1 holds the index i

; R2 holds the base address of the array (0x1000)

LDR R0, [R2 + R1*4] ; scale = 4 because each int occupies 4 bytes

Here the effective address is calculated as \$\text{EA}=0x1000 + (i \times 4)\$ which points to the required element.

Example: Structure Field Access

Consider the C structure:

struct Point {

int x; // offset 0

int y; // offset 4

};

struct Point p;

If the address of p is in register R3, the field y can be loaded using a based‑indexed mode with displacement:

    LDR R0, [R3 + 4]   ; load p.y

Choosing the Right Mode

  1. Constant values? Use Immediate mode.
  2. Fixed memory locations? Use Direct mode.
  3. Variable addresses stored in registers? Use Register Indirect.
  4. Array or table lookup? Use Indexed or Based‑Indexed with appropriate scale.
  5. Branching within a function? Use PC‑relative (Relative) mode.
  6. Procedure call/return? Use Stack‑relative instructions.

Common Pitfalls

  • Forgetting the scale factor when indexing arrays of multi‑byte elements.
  • Mixing signed and unsigned offsets in relative branches, leading to incorrect jump targets.
  • Assuming a register contains data when it actually holds an address (or vice‑versa).
  • Neglecting alignment requirements; some CPUs fault on misaligned accesses.

Suggested diagram: Memory layout showing base address, index register, scale factor, and displacement leading to the effective address of an array element.

Summary

Addressing modes provide the bridge between the abstract operations of a program and the concrete locations of data in memory. Mastery of these modes enables you to:

  • Write concise and efficient assembly code.
  • Interpret compiler‑generated machine code.
  • Design data structures that map naturally onto the hardware’s addressing capabilities.

Practice by translating small C snippets into assembly, paying particular attention to how each variable is accessed. This will reinforce the concepts and prepare you for the A‑Level examination.