At A Level the final part of this topic, covered after all the content that was studied at AS, is the idea of addressing modes. The A Level text books like P138 – 146 in Surrall and Hamflett cover addressing modes in the context of assembly language programming in general. They use an x86 instruction set for examples but make no attempt to complete practical activities because LMC is limited in the form that it is used in the OCR GCSE and A Level exams, which are the most common in the UK. Hence for practical work it is necessary to use the AQA version of LMC which allows for more sophisticated code (inc indirect addressing) and is based on ARM architecture with some variations. The AQA LMC version goes beyond even what is expected or specified in AQA exams and makes some assumptions to allow us to do some useful things that are quite close to what you would do on a real CPU.
Addressing modes
You need to know of four different modes of addressing in A Level exams:
- Immediate
- Direct
- Indirect
- Relative
Before starting it is worth recapping on your understanding of labels from the GCSE version of LMC.
What do you think the outcome of the following program would be?
1: LDA LABEL1
2: ADD LABEL2
3: STA LABEL3
4: HLT
5: LABEL1 DAT 10
6: LABEL2 DAT 5
7: LABEL3 DAT

LMC – Simple version

LMC – AQA version
Activity 1
Open the two versions of LMC either in two browser windows side by side, or on two different machines, and discuss the similarities and differences with your partner.
Activity 2
MOV R0,#50
LDR R1,LABEL1
LDR R2,[R1]
STR R2,LABEL2
HALT
LABEL1: 100
LABEL2: DAT
What do you think the above code will do?
Enter the code into LMC and run it to see if you are right. Note that colons are needed when defining labels in this version of LMC.
How the different modes work
The four lines in the program above, show the operation of three of the four addressing modes:
- MOVE RO,#50 is using immediate addressing. It moves the actual literal value decimal 50 to the register R0
- LDR R1, LABEL1 is using direct addressing. The operand label1 is a reference to a memory address. You are not fetching the address, but what is stored at the address. This is the equivalent in the other more limited LMC of LDA LABEL1 except instead of loading the contents of the address into the accumulator, it loads it into a general purpose register, in this case R1
- LDR R2,[R1] is indirect addressing which we have not seen before. It does not use a label to fetch a memory address, it uses the contents of another register to fetch data. The other register, in this case R1, contains the address to fetch data from, and we fetch the data from that address and store it in R2
- STR R2, LABEL2 is storing the contents of R2 to the memory address indicated by label2.
Check indirect addressing mode by changing the value of the contents of memory location 100 before running the code. You should find that value is copied to the address represented by Label2 (address 6 if no code has been added). Note you can use a label inside a label if you want, so it will store one memory address at another, e.g
LABEL3: LABEL1
Activity 3
Write a program that takes the values stored at two different memory locations, adds them together and stores them at a third memory location. You must use indirect addressing in your answer.
Hint1: ADD R3, R2, R1 adds the contents of R1 to R2 and stores the result in R3
Hint2: STR R4, LABEL5 stores the contents of register 4 to the address LABEL5
Relative addressing
MOV R1,#0
MOV R2,#55
NEXT: STR R2, [R1+START]
ADD R1,R1,#1
CMP R1,#3
BNE NEXT
HALT
START: DAT 1
DAT 2
DAT 3
Look at the code above and in particular at the line marked by the label NEXT. What do you notice about the syntax in that line. What do you think is going there? Run the program to see the outcome.
Activity 4
Write a program that takes 5 consecutive values in memory and adds them all together and stores the result into a another memory location
You must use relative indirect addressing.
Hints:
LDR R0,[R1+BASE] loads R0 with the contents of address BASE
offset by the value stored in R1
BASE: DAT 1
DAT 2
DAT 3 stores values 1,2,3 in consecutive locations
Challenge
Write a program that searches 5 consecutive values in memory for a target value. If it finds the value, it outputs the position it was found at, if not, it outputs 65535.
The instruction to output values is OUT Rx,4
Knowledge check
- What was the only addressing mode that the original version of LMC used?
- Which register, used for almost everything in the original LMC, is absent from the more complex CPU architecture used in LMC AQA?
- Name the three additional addressing modes needed for A Level exams.
- Which addressing mode allows you to implement an array in machine code?
- The mnemonic used in this LMC for immediate addressing is different to the mnemonic for other addressing modes. Why might this be?
- Exam questions may only mention relative addressing, but there is a technical difference between relative addressing and relative indirect addressing. What is it?