AQA 7517, section 4.7.3.4
An instruction's operand can mean different things. The addressing mode says how the operand is to be interpreted. You need to know two modes.
Immediate addressing
In immediate addressing, the operand is the datum: the actual value to be used.
ADD #5 ; add the number 5
The # (hash) in assembly language shows an immediate value. The processor uses the number in the instruction itself.
- Advantage: fast, since no extra memory access is needed to find the value.
- Disadvantage: the value is fixed in the program and is limited by the number of operand bits. It can't be used to refer to data that changes.
- Good for constants, such as a counter limit.
Direct addressing
In direct addressing, the operand is the address of the datum. The value is stored at that address in main memory, so the processor must go and get it.
ADD 5 ; add the value stored at address 5
- Advantage: can use data that changes, and the data can be stored anywhere in memory.
- Disadvantage: slower, because an extra memory access is needed to fetch the datum.
Comparison
Suppose memory holds these values:
| Address | 3 | 4 | 5 | 6 |
|---|---|---|---|---|
| Contents | 12 | 8 | 9 | 20 |
and the accumulator starts at 10.
| Instruction | Mode | What the operand means | Value added | Result in ACC |
|---|---|---|---|---|
ADD #5 |
Immediate | The number 5 | 5 | 15 |
ADD 5 |
Direct | The value at address 5 | 9 | 19 |
The same operand, 5, gives different results depending on the mode. With immediate, the datum is 5. With direct, the datum is at address 5, which is 9.
| Immediate | Direct | |
|---|---|---|
| Operand is | The datum | The address of the datum |
| Memory accesses to get the datum | 0 | 1 |
| Speed | Faster | Slower |
| Value can change while the program runs | No | Yes (change the contents of the address) |
Examples in assembly language
LDR R0, #7 ; immediate: R0 = 7
LDR R1, 100 ; direct: R1 = the value at address 100
STR R1, 101 ; direct: store R1 at address 101
ADD R2, R0, #3 ; immediate: R2 = R0 + 3
ADD R3, R0, R1 ; both values are in registers
(The exact syntax depends on the assembly language used in the question.)
Worked example
Task: memory address 20 holds 6 and address 21 holds 15. The accumulator is 0. Give the final value of the accumulator after (a) LOAD #20 and (b) LOAD 20, and (c) LOAD 21.
- (a)
LOAD #20: immediate, so the accumulator is 20. - (b)
LOAD 20: direct, so the accumulator takes the value at address 20, which is 6. - (c)
LOAD 21: direct, so the accumulator is 15.
Exam tip: the one-line definitions score marks. Immediate: the operand is the datum. Direct: the operand is the address of the datum. Check carefully whether there's a #, and, with direct, look up the contents of the address in the table you're given.
Check yourself
What is immediate addressing?
The operand is the datum itself, the actual value to use.
What is direct addressing?
The operand is the address of the datum, so the value is fetched from that memory location.
Address 8 contains 30. What is the value used by ADD #8, and by ADD 8?
ADD #8 uses 8 (immediate). ADD 8 uses 30 (direct, the contents of address 8).
Why is immediate addressing faster than direct addressing?
The value is part of the instruction, so no extra memory access is needed to fetch it.