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

Resources | Subject Notes | Computer Science

Assembly Language - Addressing Modes

4.2 Assembly Language - Addressing Modes

Addressing modes specify how the effective address of an operand is determined in an assembly language instruction. They are crucial for accessing data stored in memory. Different addressing modes offer varying levels of flexibility and efficiency.

Types of Addressing Modes

There are several common addressing modes used in assembly language. These can be broadly categorized as follows:

  • Immediate Addressing: The operand is the actual value itself, placed directly within the instruction.
  • Register Addressing: The operand is the contents of a specific register.
  • Direct Addressing: The operand is located at a specific memory address specified in the instruction.
  • Indirect Addressing: The operand is located at a memory address whose value is stored in a register.
  • Register Indirect with Displacement Addressing: The operand's memory address is calculated by adding a displacement (an offset) to the value in a register.
  • Indexed Addressing: Similar to register indirect with displacement, but the displacement is added to a base register value to calculate the memory address.

Detailed Explanation of Addressing Modes with Examples

Let's examine each addressing mode in more detail, along with illustrative examples.

1. Immediate Addressing

In immediate addressing, the operand is a constant value embedded directly in the instruction. This is useful for constants that are not expected to change during program execution.

Example (x86 assembly):

        MOV EAX, 10  ; Move the immediate value 10 into register EAX
    

Here, the value 10 is the immediate operand.

2. Register Addressing

Register addressing uses a register as the operand. The value stored in the specified register is directly accessed.

Example (x86 assembly):

        MOV EAX, EBX  ; Move the contents of register EBX into register EAX
    

The operand is the register `EBX`.

3. Direct Addressing

Direct addressing specifies the memory address of the operand directly within the instruction. This mode is suitable for accessing data stored at fixed memory locations.

Example (x86 assembly):

        MOV EAX, [1000H] ; Move the value at memory address 1000H into register EAX
    

The address `1000H` is the direct operand, enclosed in square brackets to indicate memory access.

4. Indirect Addressing

Indirect addressing uses a register to store the memory address of the operand. The instruction fetches the value from the memory location whose address is held in the register.

Example (x86 assembly):

        MOV EAX, [EBX]  ; Move the value at the memory address stored in register EBX into register EAX
    

The register `EBX` contains the memory address, and the value at that address is accessed.

5. Register Indirect with Displacement Addressing

This mode calculates the memory address by adding a displacement (an offset) to the value in a register. The displacement can be a constant or another register value.

Example (x86 assembly):

        MOV EAX, [EBX + 4] ; Move the value at the memory address (EBX + 4) into register EAX
    

The displacement `4` is added to the value in register `EBX` to determine the effective memory address.

6. Indexed Addressing

Indexed addressing is similar to register indirect with displacement, but the displacement is added to a base register value. This is commonly used to access elements within an array.

Example (x86 assembly):

        MOV EAX, [EBX + ECX * 4] ; Move the value at the memory address (EBX + ECX * 4) into register EAX
    

Here, `EBX` is the base register, `ECX` is the index register, and `4` is the scale factor. The effective address is calculated as `EBX + (ECX * 4)`. This is often used to access elements in an array where `EBX` holds the base address and `ECX` holds the index.

Addressing Mode Description Example (x86)
Immediate Operand is a constant value. MOV EAX, 10
Register Operand is the contents of a register. MOV EAX, EBX
Direct Operand is at a fixed memory address. MOV EAX, [1000H]
Indirect Operand's address is in a register. MOV EAX, [EBX]
Register Indirect with Displacement Address is calculated by adding a displacement to a register value. MOV EAX, [EBX + 4]
Indexed Address is calculated by adding a displacement to a base register value (often with a scale factor). MOV EAX, [EBX + ECX * 4]

Understanding and effectively utilizing different addressing modes is fundamental to writing efficient and well-structured assembly language programs.