Addressing Modes
Addressing modes in assembly language refer to the various methods used to specify the operand(s) for an instruction. These addressing modes dictate how the processor accesses data or operands in memory or registers. Different assembly languages support different addressing modes.
On a 64-bit architecture, address require 64-bits.
As we noted in the previous chapter, the only way to access memory is with the brackets ([]
). Omitting the brackets will not access memory and instead obtain the address of the item. For example:
mov rax, qword [var1] ; vlaue of var1 in rax
mov rax, var1 ; address of var1 in rax
1 Immediate Addressing Mode:
In immediate addressing mode, the operand itself is provided as part of the instruction.
Example:
mov eax, 10 ; move the immediate value 10 into the eax register
2 Register Addressing Mode:
In register addressing mode, the operand is stored in a register.
Example:
add eax, ebx ; add the contents of ebx register to eax register
3 Direct Addressing Mode:
In it, the operand is directly specified by its memory address.
mov eax, [address] ; move the value stored at memory 'address' into eax register
4 Indirect Addressing Mode:
In it, the operand's memory address is specified indirectly, typically through a register or a memory location containing the address.
mov eax, [ebx] ; move the value stored at the memory address stored in ebx register into eax register.
5 Indexed Addressing Mode:
In indexed addressing mode, the memory address is computed by adding an offset to a base address specified in a register.
mov eax, [ebx + ecx*4] ; move the value stored at the memory address computed by adding ebx and four times ecx into eax register
6 Base-Plus-Index Addressing Mode:
In it, the memory address is computed by adding an offset to a base address stored in one register and an index stored in another register.
mov eax, [ebx + ecx] ; move the value stored at the memory address computed by adding the contents of ebx and ecx registers into eax register
7 Relative Addressing Mode:
In it, the operands' memory address is specified relative to the current instruction's memory address.
jmp label ; Jump to the memory address specified by 'label'