What is an addressing mode?
- Addressing modes are ways in which an instruction in assembly language or machine code can access data stored in memory.
- There are four main types of addressing modes:
- Immediate
- Direct
- Indirect
- Indexed
Immediate addressing
- Operand is part of the instruction itself
MOV AX, 1234h // Moves the immediate hex value 1234h to the AX register
Direct addressing
- The memory address of the operand is directly specified
MOV AX, [1234h] // Take the value stored in memory location 1234h and move to the AX register
Indirect addressing
- A register contains the memory address of the operand
- If
BXcontains the value2000h:
MOV AX, [BX] // Moves the value from memory location 2000h to the AX register
- This does not mean “Move the value
2000hinto AX” - Instead, it means, “Look in the memory address
2000h(the value currently stored in the BX register) and move whatever value you find into the AX register.” - When brackets
[ ]are around a register in assembly language (like[BX]), it’s an instruction to treat the value inside that register as a memory address and to use the data at that memory address for the operation
Indexed addressing
- Combines a base address with an index to compute the effective address
- If
BXcontains0050handSIhas a base address1000h:
MOV AX, [BX + SI] // Move the value at memory location 1050h to AX
- Fetches data from the effective address (because
1000h + 0050his1050h) and moves it into the AX register
Worked Example
Consider a basic computer system with the following assembly language instructions and a memory layout starting at address
1000.1000: MOV AX, 8 1002: ADD AX, [BX] 1004: MOV [0008], AX 1006: MOV CX, [BX+DI] 1008: HLTAssume the registers have the following values before execution:
AX = 0000 BX = 0003 DI = 0002 CX = 0010Memory contains:
0000: 0 0001: 0 0002: 0 0003: 5 0004: 0 0005: 7 0006: 7 0007: 9 0008: 0a) For the instruction at
1002, identify the addressing mode used and explain what it does in the context of this instruction.[2]
b) After the instruction at
1004has executed, what will the value at memory address0008be? Justify your answer.[2]
c) What value will be moved into the
CXregister after the instruction at1006executes? Explain the addressing mode used.[2]
Answer:
Answer that gets full marks: a) The instruction at
1002uses Indirect Addressing. The instructionADD AX, [BX]adds the value at the memory address contained in theBXregister to theAXregister. In this case, sinceBXis3, it will add the value at memory address3toAX.b) The value at memory address
0008will be13. Before the instruction,AXcontains the value8. After adding5(from memory address3due to the instruction at1002),AXwill have the value13. The instruction at1004then moves this value to memory address0008.c) The value moved into the
CXregister will be7. The instruction at1006uses Indexed Addressing. It accesses memory by combining the address inBXwith the offset inDI. GivenBXis3andDIis2, the effective address is3 + 2 = 5, so it fetches the value7from address0005intoCX.Acceptable responses:
a) Any answer identifying Indirect Addressing and explaining its use in the context of fetching a value from memory for the instruction should be awarded marks.
b) Any answer stating that the value at address
0008will be13due to adding8and5should be awarded marks.c) Any response indicating the use of Indexed Addressing and explaining the value fetch from address
5should be awarded marks.