Memory Addressing Modes

As we get deeper into computer systems, we need to understand how data is transferred and how it gets into and out of the computer's memory. This is where memory addressing modes come into focus.

Purpose and Importance of Memory Addressing

At the heart of every computation is data, and data resides in memory. Memory addressing modes are the methods used in machine code instructions to specify the location of this data. Understanding memory addressing modes provides insight into optimizing code, debugging, and understanding the subtleties of system behavior (as we have seen with some Python examples in previous terms).

The instruction set architecture (ISA) of a computer defines the set of addressing modes available. In this section, we'll explore the intricacies of addressing modes using x86 assembly language.

Principle of Memory Addressing Modes

There are various modes such as immediate addressing, direct addressing, indirect addressing, register addressing, base-plus-index and relative addressing. Each of these modes is designed to efficiently support specific programming constructs, such as arrays, records and procedure calls.

Before continuing, read https://en.wikipedia.org/wiki/Addressing_mode for a full list and detailed explanation of memory addressing modes.

Check Your Understanding: Name at least three memory addressing modes.

Click Here for the Answer Three memory addressing modes include immediate addressing, direct addressing, and indirect addressing.

Various Modes and Their Uses

Let's touch on a few modes for context.

  1. Immediate Addressing: The operand used during the operation is a part of the instruction itself. Useful when a constant value needs to be encoded into the instruction.
  2. Direct Addressing: The instruction directly states the memory address of the operand. This mode is beneficial while accessing global variables.
  3. Indirect Addressing: Here, the instruction specifies a memory address that contains another memory address, which is the location of the operand. Indirect addressing is crucial for implementing pointers or references.

It's equally important to contextualize these modes into real-world programming paradigms. For instance, when writing a for loop, the iterator variable often uses immediate addressing since the start, end, and step variables are usually predefined values.

Note: Each ISA has different addressing modes. The C programming language provides several of these, primarily via its pointer constructs.

Check out this video for more explanation about various Memory Modes:

Check Your Understanding: In which addressing mode is the operand part of the instruction itself?

Click Here for the Answer Immediate Addressing.

Examples of x86 Addressing Modes

x86 assembly provides a rich set of addressing modes for accessing data. We'll dig into two of these - Immediate and Register Indirect.

  1. Immediate Mode: This mode uses a constant value within the instruction. For instance, mov eax, 1. Here 1 is an immediate value.

  2. Register Indirect Mode: A register contains the memory address of the data. For example, mov eax, [ebx] implies that the data is at the memory address stored in ebx.

Importantly, x86 supports complex addressing modes including base, index and displacement addressing methods combined, such as mov eax, [ebx+ecx*4+4].

In this mode, ebx is the base register, ecx is the index register, 4 is the scale factor, and the final 4 is the displacement. This mode is handy when dealing with arrays and structures.

Check Your Understanding: What is the addressing mode used when a register contains the memory address of the data?

Click Here for the Answer Register Indirect Mode.