OCR H446, section 1.2.4 (d)
An assembly instruction has an operand. The addressing mode says how the operand is to be used to find the data. You need to know four modes.
The examples use this memory, and an index register X that holds 2:
| Address | 20 | 21 | 22 | 23 | 30 |
|---|---|---|---|---|---|
| Contents | 30 | 11 | 9 | 5 | 7 |
Immediate addressing
The operand is the data itself.
LDA #20 ; load the number 20 into the accumulator
The # marks an immediate value. The accumulator gets 20.
- Fast: no memory access is needed to fetch the data.
- The value is fixed in the program, and limited by the size of the operand.
- Good for constants and starting values.
Direct addressing
The operand is the address of the data. The processor goes to that address to get the value.
LDA 20 ; load the value stored at address 20
The accumulator gets the contents of address 20, which is 30.
- Needs one memory access to fetch the data.
- The data can change as the program runs, and can be anywhere in memory.
- This is the only mode in the basic Little Man Computer:
LDA firstis direct.
Indirect addressing
The operand is the address of a location that holds the address of the data. It is a pointer.
LDA (20) ; address 20 holds 30, so go to address 30
The processor reads address 20 and finds 30, then goes to address 30 to find the data: the accumulator gets 7.
- Needs two memory accesses, so it is slower.
- The address can be changed while the program runs, so one instruction can refer to different data at different times.
- Useful for pointers, linked data structures and passing the address of data to a subroutine.
Indexed addressing
The address is worked out by adding the contents of an index register to the operand.
LDA 20,X ; address = 20 + the contents of X = 20 + 2 = 22
The accumulator gets the contents of address 22, which is 9.
- Ideal for arrays: the operand is the start (base) address of the array, and the index register is the element number. Changing the index register moves through the array.
- A loop can process each element with one instruction, by increasing X each time.
LDX #0 ; X = 0
loop LDA 100,X ; load element X of the array that starts at 100
... ; use the value
ADD X, #1 ; next element (illustrative)
Summary of the example
| Mode | Instruction | How the data is found | Result |
|---|---|---|---|
| Immediate | LDA #20 |
The operand is the data | 20 |
| Direct | LDA 20 |
Data is at address 20 | 30 |
| Indirect | LDA (20) |
Address 20 holds the address of the data (30), so data is at 30 | 7 |
| Indexed | LDA 20,X |
Data is at address | 9 |
Comparison
| Mode | Memory accesses to get the data | Typical use |
|---|---|---|
| Immediate | 0 | Constants |
| Direct | 1 | Variables |
| Indirect | 2 | Pointers, and data whose location changes |
| Indexed | 1 (plus the addition) | Arrays and tables |
(The exact notation, such as #, ( ) and ,X, differs between assembly languages. Exam questions explain any notation they use.)
Worked example
Task: the memory holds address 40 contains 50, and address 50 contains 8. The index register is 3, and address 43 contains 6. What does the accumulator hold after each instruction: (a) LDA #40, (b) LDA 40, (c) LDA (40), (d) LDA 40,X?
- (a) Immediate: the accumulator holds 40.
- (b) Direct: it holds the contents of address 40, which is 50.
- (c) Indirect: address 40 contains 50, so the data is at address 50: the accumulator holds 8.
- (d) Indexed: the address is , so it holds 6.
Exam tip: work out the address first, then fetch the data. Write down each step: "address 40 contains 50, so go to address 50". For indirect, remember that there are two look-ups. For indexed, remember the addition.
Check yourself
What is the difference between immediate and direct addressing?
In immediate addressing the operand is the data itself. In direct addressing the operand is the address of the data.
Which addressing mode is best for accessing the elements of an array?
Indexed addressing, using the array's start address as the operand and an index register to choose the element.
Why is indirect addressing slower than direct addressing?
It needs two memory accesses: one to read the address stored in the operand's location, and one to read the data at that address.
If address 15 contains 22 and address 22 contains 9, what does LDA (15) load?
- Address 15 holds 22, and the data is at address 22.