Addressing Modes

COS2621 - Computer Organisation · Machine Level Programming

Addressing Modes

Addressing modes are techniques used in assembly language programming to specify where operands (data values) are located. Understanding addressing modes is crucial because they determine how instructions access data in memory. In this topic, you will learn about different types of addressing modes, their usage, and how they affect the execution of assembly language programs.

What are Addressing Modes?

Addressing modes define how the operand of an instruction is obtained. They can be classified into different categories based on how they access data. Each mode has its own advantages and disadvantages, which can affect program efficiency and complexity.

Types of Addressing Modes

1. Immediate Addressing Mode

In immediate addressing mode, the operand is specified explicitly in the instruction itself. This means that the value of the operand is directly provided rather than stored in a memory location.

Remember: Immediate values are often prefixed with a special character, such as a hash (#), in assembly language.

Example:

MOV R1, #10

This instruction moves the immediate value 10 into register R1. Here, 10 is not located in memory; it is directly used as the operand.

2. Direct Addressing Mode

In direct addressing mode, the address of the operand is given explicitly in the instruction. The CPU accesses the memory location specified to retrieve the operand.

Example:
MOV R1, 1000

This instruction moves the value stored at memory address 1000 into register R1. The operand is located at the address specified.

Watch out: Ensure that the address specified is valid and contains the expected data type. Accessing an invalid address can lead to runtime errors.

3. Indirect Addressing Mode

Indirect addressing mode uses a register or memory location to hold the address of the operand. This allows for more flexible data access, as the actual address of the operand can change during program execution.

Example:
MOV R1, (R2)

This instruction moves the value from the memory location pointed to by register R2 into register R1. The actual address of the operand is stored in R2.

4. Register Addressing Mode

In register addressing mode, the operand is located in a register. The instruction specifies which register to use. This mode is typically the fastest because accessing registers is quicker than accessing memory.

Example:
MOV R1, R2

This instruction moves the value from register R2 into register R1. Both operands are located in the CPU's registers.

5. Indexed Addressing Mode

Indexed addressing mode uses a base address and an offset to calculate the effective address of the operand. This is useful for accessing elements in arrays or data structures.

Example:
MOV R1, 1000(R2)

This instruction moves the value from the memory location calculated by adding the contents of register R2 to the base address 1000 into register R1. If R2 contains 4, the effective address will be 1004.

Tip: Indexed addressing is particularly useful for looping through arrays, as you can easily change the index register to access different elements.

6. Base-Offset Addressing Mode

Base-offset addressing mode is similar to indexed addressing mode but typically uses a base register and a constant offset. This mode is often used for accessing data structures in memory.

Example:
MOV R1, (R2 + 5)

This instruction moves the value from the memory location calculated by adding 5 to the address stored in R2 into register R1.

Addressing Modes in Context

Addressing modes are essential for writing efficient assembly language programs. Choosing the right addressing mode can affect the speed and complexity of your program. As you work on assembly language projects, consider the following:

  • Immediate addressing is useful for constants.
  • Direct addressing is straightforward but limited to fixed addresses.
  • Indirect addressing allows for dynamic data access.
  • Register addressing is the fastest method for data manipulation.
  • Indexed and base-offset modes are ideal for working with arrays and structures.

Practical Example

Let us consider a simple program that adds two numbers stored in an array and stores the result in another location using different addressing modes.

START:  MOV R1, 10      ; Immediate Addressing Mode (load 10 into R1) 
        MOV R2, 20      ; Immediate Addressing Mode (load 20 into R2) 
        ADD R1, R2      ; Add R2 to R1 (R1 now contains 30) 
        MOV 1000, R1    ; Direct Addressing Mode (store result at memory address 1000) 
        MOV R3, (R4)    ; Indirect Addressing Mode (load value from address in R4 into R3) 
        MOV R5, R2(5)   ; Indexed Addressing Mode (load value from address in R2 + 5 into R5) 
        END

This program demonstrates the use of different addressing modes for loading, adding, and storing values. Each addressing mode serves a specific purpose, and understanding these modes enables you to write more effective assembly language programs.

Common Mistakes

Watch out: One common mistake is confusing direct and indirect addressing modes. Remember that direct addressing uses a fixed address, while indirect addressing uses a register or memory location to determine the address.

Watch out: When using indexed addressing, ensure that the base address and index register are correctly set to avoid accessing the wrong memory location.

Summary

  • Addressing modes specify how operands are accessed in assembly language.
  • Common addressing modes include immediate, direct, indirect, register, indexed, and base-offset.
  • Choosing the right addressing mode is crucial for program efficiency and correctness.
  • Practice using different addressing modes to become proficient in assembly language programming.

Check your understanding

  1. What is the difference between immediate and direct addressing modes?
  2. How does indexed addressing mode work in accessing array elements?
  3. Provide an example of a situation where indirect addressing mode would be beneficial.
  4. What are the advantages of using register addressing mode?