• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

When writing code that might be loaded at different memory addresses (position-independent code), we can’t use fixed addresses for jumps and branches. We need addresses relative to the current instruction.

Core Idea

Relative Addressing computes the target address by adding an offset to the Program Counter (PC). The address is relative to the current instruction, making code position-independent.

How It Works

  1. Instruction specifies an offset value (positive or negative)
  2. CPU reads current PC (address of current instruction)
  3. Target address = PC + offset
  4. Used primarily for branch and jump instructions
  5. Example: JMP 10 — jumps to address (PC + 10)
relative Instr Instruction (JMP +10) Offset: 10 Add Target Address 1000h + 10 = 100Ah Instr->Add PC Program Counter current: 1000h PC->Add Target Memory Addr 100Ah: next instruction Add->Target

Key Properties

  • Position-independent: code works at any load address
  • Offset is typically small (short jumps) or large (long jumps)
  • Used for conditional/unconditional branches, function calls
  • Most common addressing mode for control flow instructions

Connections

Edge Cases & Gotchas

  • Offset range is limited (can’t jump too far with short relative)
  • PC value used is typically after instruction fetch (PC = next instruction)
  • Negative offsets for backward jumps (loops)