13.1 Addressing Modes
COS2621 - Computer Organisation · 13. Instruction Sets: Addressing Modes and Formats
Addressing Modes
Definition of Addressing Modes
Addressing modes are techniques used in computer architecture to specify the location of operands for instructions. They determine how the operands are accessed during instruction execution. Understanding addressing modes is crucial for effective programming and optimization in assembly language.
Types of Addressing Modes
There are several common addressing modes:
- Immediate Addressing: The operand is specified explicitly in the instruction. For example, in the instruction
mov ax, 10, the value 10 is an immediate operand. - Direct Addressing: The address of the operand is given directly in the instruction. For instance,
mov ax, [2000h]means to move the value at memory address 2000h into AX. - Indirect Addressing: The address of the operand is specified indirectly through a register. For example,
mov ax, [bx]uses the contents of the BX register as an address. - Indexed Addressing: Combines a base address with an index value. For example,
mov ax, [base + si]calculates the effective address by adding the base and index. - Base-Register Addressing: Uses a base register to hold the starting address, often combined with an offset. For example,
mov ax, [bp + 4]accesses memory relative to the base pointer.
Examples of Addressing Modes
Consider the following examples:
- Immediate Addressing Example:
mov ax, 10loads the immediate value 10 into AX. - Direct Addressing Example:
mov ax, [2000h]loads the value stored at memory location 2000h into AX. - Indirect Addressing Example:
mov ax, [si]loads the value from the address in the SI register into AX. - Indexed Addressing Example:
mov ax, [base + si]calculates the effective address using base and index.
Watch out: Do not confuse direct addressing with immediate addressing. In direct addressing, the address is specified, while in immediate addressing, the actual value is specified.
Common Instructions and Their Effects
Understanding the effect of certain instructions is essential. For example, the ROL instruction rotates the destination operand to the left, moving the most significant bit into the least significant position and the carry flag. In contrast, the SAR instruction shifts the destination operand to the right, retaining the most significant bit, which duplicates the sign bit.
Register Addressing
In a machine with seven general-purpose 32-bit registers, 3 bits are needed to address any one of these registers. This is calculated using the formula 2^n ≥ 7, where n is the number of bits.
Instruction Examples
Here are some instructions that illustrate different addressing modes:
JC label: This instruction jumps to the label if the Carry flag is set. It demonstrates conditional branching based on the status of a flag.LEA ax, [2000h]: This instruction loads the effective address of memory position 2000h into the AX register. It is crucial for calculating addresses in complex data structures.MUL bx: This instruction multiplies the value in AX by the value in BX. The Carry and Overflow flags are affected by this instruction.NEG ax: This instruction calculates the two's complement of the value in AX, effectively negating it.OR ax, bx: This instruction performs a logical OR on the values in AX and BX.POP ax: This instruction pops a word from the stack into the AX register.
Conclusion
Addressing modes are essential for understanding how instructions access data in a computer system. By mastering these modes, you can write more efficient assembly language programs.
Check your understanding
- What is the difference between immediate addressing and direct addressing?
- Provide an example of indexed addressing.
- Why is indirect addressing useful in programming?
- What instruction would you use to branch if the sign flag is set?
Test yourself
5 quick questions on this lesson. Pick an answer to see if you're right.
1. Suppose a machine has seven (7) general-purpose 32-bit registers. How many bits must be reserved to the machine code instruction in order to address any one of these seven registers?
2. In which addressing mode is the address of the operand specified indirectly through a register?
3. What flag does the STC instruction set?
4. Which flag is affected by the MUL instruction?
5. What does the XCHG instruction do?