Addressing modes in computer organization refer to the different ways in which memory addresses are calculated and used to access data or operands during program execution. Higher-level language constructs, such as arrays, pointers, constants, and variables, need to be represented in machine-level instructions to be executed by the processor. Let’s look at each of these constructs and the addressing modes that can be used to represent them:
Arrays:
Arrays are contiguous blocks of data elements of the same type. In machine-level code, arrays can be accessed using two primary addressing modes: indexed addressing and base+offset addressing.
a) Indexed Addressing:
In indexed addressing, an array element’s memory address is calculated by adding an index (an offset) to a base address. The index represents the position of the desired element within the array.
Example: Consider an integer array arr
in C/C++:
int arr[5] = {10, 20, 30, 40, 50};
The machine-level instruction to access arr[2] using indexed addressing might look like:
MOV R1, #2 ; Load the index 2 into register R1
MOV R2, #1000 ; Load the base address of the array into register R2 (e.g., 1000)
ADD R2, R2, R1 ; Calculate the address: base + offset
LD R3, (R2) ; Load the value of arr[2] into register R3
b) Base+offset Addressing:
In base+offset addressing, the memory address is calculated by adding an offset to a base register’s value, where the base register holds the starting address of the array.
Example: Continuing with the same arr
in C/C++:
int arr[5] = {10, 20, 30, 40, 50};
The machine-level instruction to access arr[2] using base+offset addressing might look like:
MOV R2, #1000 ; Load the base address of the array into register R2 (e.g., 1000)
ADD R2, R2, #8 ; Calculate the address: base + (element size * offset), assuming integer size is 4 bytes
LD R3, (R2) ; Load the value of arr[2] into register R3
Pointers:
Pointers are variables that store memory addresses. They can point to other variables, arrays, or dynamically allocated memory.
a) Register Indirect Addressing:
In register indirect addressing, a pointer variable’s value (the memory address) is stored in a register, and that register is used to access the data pointed to by the pointer.
Example: Consider the following C/C++ code with a pointer variable ptr
:
int *ptr;
int num = 100;
ptr = #
The machine-level instruction to access the value pointed to by ptr using register indirect addressing might look like:
MOV R1, #ptr ; Load the memory address of pointer variable `ptr` into register R1
LD R2, (R1) ; Load the value pointed to by `ptr` into register R2
Constants:
Constants are fixed values that are hardcoded in the source code. They can be numeric constants, character constants, or string literals.
Immediate Addressing:
In immediate addressing, the constant value itself is directly included in the instruction, rather than referring to a memory location.
Example:
Consider the following C/C++ code with a constant x:
int x = 42;
The machine-level instruction to load the constant 42 into a register using immediate addressing might look like:
MOV R1, #42 ; Load the value 42 directly into register R1
Variables:
Variables are named memory locations that store data values during program execution.
Direct Addressing:
In direct addressing, the memory address of the variable is directly specified in the instruction.
Example:
Consider the following C/C++ code with a variable count:
int count = 0;
The machine-level instruction to load the value of count into a register using direct addressing might look like:
LD R1, (1000) ; Load the value of `count` from memory address 1000 into register R1