Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 44 of 9361. Foundations of Digital Logic and Computer ArchitectureFree lesson

Utilizing Base-Displacement Addressing for Data Structures

You will apply base-displacement addressing to access elements within arrays and structures efficiently using register-based offsets.

In this lesson, you will learn how to efficiently access elements within data structures like arrays and structs by using a fixed base address combined with a flexible offset.

The Problem with Simple Addressing

So far, you’ve learned about direct addressing (hardcoding a memory address) and register addressing. While these are useful, they aren't enough for data structures. Imagine an array of 100 integers. If you wanted to access the 50th element, you wouldn't want to store 100 unique, hardcoded addresses in your program. Instead, we use Base-Displacement Addressing.

How Base-Displacement Works

In this mode, the CPU calculates the final memory address by taking a Base Address (stored in a register) and adding a Displacement (a constant value encoded directly into the instruction).

Think of the "Base" as the front door of an apartment complex and the "Displacement" as the specific apartment number. If the base register R1 points to the start of an array, the instruction LOAD R2, 4(R1) tells the processor: "Go to the address in R1, add 4 to it, and bring me the data found at that location."

Concrete Example

Let’s say we have a structure representing a "Person" in memory:

  • Age (at offset 0)
  • ID (at offset 4)

If register R5 holds the base address of this "Person" object, accessing the fields is incredibly fast:

LOAD R6, 0(R5)  ; Load Age into R6 (Base R5 + offset 0)
LOAD R7, 4(R5)  ; Load ID into R7 (Base R5 + offset 4)

By changing the value in R5, the same two lines of code can process any "Person" object in your system. We aren't changing the instructions themselves; we are simply shifting the "window" through which the CPU views memory. This is the cornerstone of how high-level language structures and local variables on the stack are implemented in hardware.

Key Takeaway: Base-displacement addressing allows us to navigate data structures dynamically by pairing a constant offset with a flexible base register, making our code reusable and memory-efficient.