AQA 7517, section 4.7.3.5
Machine code instructions are binary, which is hard to read, so they are written as assembly language using short mnemonics. You need to understand and use the basic operations below, with immediate and direct addressing.
The examples use an ARM-style assembly language with registers R0, R1, R2 ... and memory addresses. The exact mnemonics and layout in an exam are given in the question, so focus on understanding the operations.
The operations
| Operation | Example | Meaning |
|---|---|---|
| Load | LDR R0, 100 |
Copy the value at memory address 100 into R0 |
| Store | STR R0, 101 |
Copy R0 into memory address 101 |
| Add | ADD R2, R0, R1 |
R2 = R0 + R1 |
| Subtract | SUB R2, R0, #3 |
R2 = R0 − 3 |
| Move | MOV R0, #5 |
R0 = 5 |
| Compare | CMP R0, #10 |
Compare R0 with 10 and set the status flags (no result stored) |
| Branch (unconditional) | B loop |
Always jump to the instruction labelled loop |
| Branch if equal | BEQ done |
Jump if the last comparison was equal |
| Branch if not equal | BNE loop |
Jump if the last comparison was not equal |
| Branch if greater than | BGT label |
Jump if greater than |
| Branch if less than | BLT label |
Jump if less than |
| AND | AND R0, R1, #15 |
Bitwise AND |
| OR | ORR R0, R1, R2 |
Bitwise OR |
| NOT | MVN R0, R1 |
Bitwise NOT (invert every bit) |
| XOR | EOR R0, R1, R2 |
Bitwise exclusive OR |
| Logical shift left | LSL R0, R1, #2 |
Shift bits left by 2 places |
| Logical shift right | LSR R0, R1, #1 |
Shift bits right by 1 place |
| Halt | HALT |
Stop the program |
Branching
Normally instructions run in order. A branch changes the program counter to jump to another instruction.
- Unconditional branch: always jumps.
- Conditional branch: jumps only if a condition holds. The condition comes from a previous compare (which sets flags in the status register), such as equal, not equal, greater than or less than.
Branches give selection (jump over some code) and iteration (jump back to repeat).
Selection example
LDR R0, 100 ; R0 = value at address 100
CMP R0, #50 ; compare with 50
BLT low ; if less than 50, jump to low
MOV R1, #1 ; else R1 = 1 (pass)
B end
low: MOV R1, #0 ; R1 = 0 (fail)
end: STR R1, 101
HALT
Iteration example: the sum of 1 to 5
MOV R0, #0 ; total = 0
MOV R1, #1 ; counter = 1
loop: ADD R0, R0, R1 ; total = total + counter
ADD R1, R1, #1 ; counter = counter + 1
CMP R1, #6 ; compare counter with 6
BLT loop ; if counter < 6, repeat
STR R0, 50 ; store the total
HALT
Trace:
| Pass | R0 (total) | R1 (counter) after | CMP R1, #6 | Branch? |
|---|---|---|---|---|
| 1 | 1 | 2 | 2 < 6 | Yes |
| 2 | 3 | 3 | 3 < 6 | Yes |
| 3 | 6 | 4 | 4 < 6 | Yes |
| 4 | 10 | 5 | 5 < 6 | Yes |
| 5 | 15 | 6 | 6 < 6 is false | No, continue |
The total, 15, is stored at address 50.
Bitwise logical operations
These work on each bit position separately.
Uses:
- AND with a mask keeps only chosen bits, such as the lower four bits (
AND R0, R1, #15). - OR can set chosen bits to 1.
- XOR can flip chosen bits.
Shifts
A logical shift moves all the bits left or right, filling the empty places with 0.
- Shift left by 1 doubles an unsigned number (, which is ).
- Shift right by 1 halves it, rounding down (, which is ).
- Shifting by places multiplies or divides by .
Worked example
Task: write assembly to calculate , assuming R0 and R1 already hold values. Then trace it for R0 = 3 and R1 = 2.
LSL R3, R0, #2 ; R3 = R0 x 4 (shift left 2)
ADD R2, R3, R1 ; R2 = R3 + R1
With R0 = 3 (0011): LSL R3, R0, #2 gives 1100, which is 12. Then ADD R2, R3, R1 gives .
Exam tip: read each instruction carefully, and write the register values after every instruction. Remember that CMP doesn't store a result: it only sets flags for the following branch. Check whether the operand is immediate (#) or direct.
Check yourself
What does STR R1, 200 do?
It stores (copies) the value in R1 into memory address 200.
What is the difference between a conditional and an unconditional branch?
An unconditional branch always jumps. A conditional branch only jumps if the condition set by a previous compare is true.
What is the result of a logical shift left by 3 on 00000011?
00011000 (3 × 8 = 24).
Apply AND to 11001010 and 00001111
00001010.