Modes of Addressing (OCR A-Level Computer Science): Revision Notes
📚 Revision Notes
Modes of Addressing
Overview
Addressing modes in assembly language define how an instruction identifies the location of data in memory. Each mode offers a different way to access data, providing flexibility in programming and allowing efficient use of memory. The main addressing modes are immediate, direct, indirect, and indexed. Understanding these modes is essential in low-level programming, as it influences how instructions operate on data.
Key Addressing Modes
Immediate Addressing
- Definition: The operand (data) is specified directly in the instruction itself, not in memory. The CPU does not need to fetch this value from memory, as it's already part of the instruction.
lightbulbExample
Example:
MOV A, #5 ; Load the value 5 directly into register A
- Use Case: Useful for loading constant values quickly. For example, setting a counter to zero or assigning a fixed constant to a register.
- Advantages:
- Fast, as it doesn't require a memory lookup.
- Simplifies code for loading constant values.
- Disadvantages:
- Not flexible, as the value is fixed in the instruction and cannot be modified during runtime.
Direct Addressing
- Definition: The instruction specifies the memory address where the data is located. The CPU fetches the data from this memory address to perform the operation.
lightbulbExample
Example:
LDA 105 ; Load the value at memory address 105 into the accumulator
- Use Case: Common for accessing specific memory locations directly, often used for retrieving data stored in fixed memory addresses.
- Advantages:
- Simple to implement and understand.
- Efficient for accessing fixed locations in memory.
- Disadvantages:
- Limited by the size of the address space (usually a maximum number of locations the CPU can address).
- Not flexible if the memory layout changes, as each address is hard-coded.
Indirect Addressing
- Definition: The instruction provides an address where the actual memory address of the data is stored (a "pointer" to the data). The CPU accesses this address to retrieve the final memory location and then accesses the data at that location.
lightbulbExample
Example:
LDA (200) ; The address at memory location 200 contains the address of the data
- Use Case: Ideal for working with dynamic data structures like linked lists, where addresses are not fixed and can vary during execution.
- Advantages:
- Enables flexible memory access, as the effective address can change at runtime.
- Useful for data structures that need dynamic memory access, such as arrays or linked lists.
- Disadvantages:
- Slower than direct addressing due to the extra memory lookup.
- Slightly more complex to understand and trace.
Indexed Addressing
- Definition: The instruction specifies a base address, and an index register holds an offset. The effective address is calculated by adding the base address to the index value.
lightbulbExample
Example:
LDA 100, X ; Load the value from address (100 + X) into the accumulator, where X is the index
- Use Case: Commonly used in iterating over arrays or lists, where each element is stored at a consecutive memory location.
- Advantages:
- Simplifies array traversal, allowing easy access to sequential data.
- Efficient for loops and repetitive data access patterns.
- Disadvantages:
- Limited by the size of the index register, restricting the range of data that can be accessed.
- Slightly more complex than direct addressing.
infoNote
Example Programme Using Different Addressing Modes
Here's a sample assembly code that uses different addressing modes to demonstrate their function:
; Define constants and memory locations
START EQU 100 ; Start location for indexed access
DATA_PTR EQU 200 ; Memory location used for indirect addressing
COUNT EQU 3 ; Number of elements to process
; Immediate Addressing
MOV A, #5 ; Load constant 5 into register A
; Direct Addressing
LDA COUNT ; Load the value at memory address labelled COUNT into A
; Indirect Addressing
LDA (DATA_PTR) ; Load the value from the address found at DATA_PTR
; Indexed Addressing
LDX 0 ; Load index register X with 0
LOOP: LDA START, X ; Load value at address (START + X) into A
INC X ; Increment X to access the next element
CMP X, COUNT ; Compare X with COUNT to see if we're done
BNE LOOP ; If X is not equal to COUNT, repeat the loop
Explanation:
- Immediate: Directly loads a constant value into a register.
- Direct: Loads a specific memory location's value into a register.
- Indirect: Uses a pointer stored in memory to access the data indirectly.
- Indexed: Accesses an array by iterating through memory locations using an index register.
Tracing the Example
Assume the following initial values in memory:
- COUNT (Memory 300): 3
- DATA_PTR (Memory 200): Address 150 (containing some data)
- START (Memory 100 onwards): Array [10, 20, 30] | Instruction | Accumulator (A) | Index Register (X) | Effective Address | Explanation | |---|---|---|---|---| | MOV A, #5 | 5 | - | - | Loads constant 5 into A | | LDA COUNT | 3 | - | 300 | Loads value at address COUNT (3) | | LDA (DATA_PTR) | Value at 150 | - | 150 | Loads data from address in DATA_PTR | | LDX 0 | - | 0 | - | Initialises index register X to 0 | | LDA START, X | 10 | 0 | 100 | Loads START + X (100 + 0) | | INC X | - | 1 | - | Increments X for next array element | | LDA START, X | 20 | 1 | 101 | Loads START + X (100 + 1) | | CMP X, COUNT | - | - | - | Compares X with COUNT (not equal) | | BNE LOOP | - | - | - | Branches back to LOOP |
Note Summary
infoNote
Common Mistakes
- Using the Wrong Addressing Mode: Applying the wrong addressing mode can cause unexpected results, such as incorrect data being accessed or manipulated.
- Incorrect Index Calculation: In indexed addressing, forgetting to update the index register (or incrementing it incorrectly) can lead to errors or infinite loops.
- Indirect Addressing Missteps: Using indirect addressing without understanding the pointer values can lead to accessing unintended memory locations.
infoNote
Key Takeaways
- Immediate Addressing: Directly uses a constant within the instruction.
- Direct Addressing: Uses a specific memory address within the instruction.
- Indirect Addressing: Points to an address where the actual data address is stored, allowing dynamic data access.
- Indexed Addressing: Adds an offset to a base address, ideal for arrays and iterative tasks.
- Importance: Each mode has unique strengths and weaknesses, influencing how data is accessed and manipulated. Understanding these helps with efficient low-level programming.