Resources | Subject Notes | Computer Science
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.
There are several common addressing modes used in assembly language. These can be broadly categorized as follows:
Let's examine each addressing mode in more detail, along with illustrative examples.
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.
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`.
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.
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.
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.
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.