Problems to solve yourself, from easy to hard. There's no automatic checker: run your program and compare its output against the test values shown.
Toggle: Shift+T
Rd is the destination register, Rn the first operand register and op2 is a register (R3) or a number (#12). The registers are R0 to R12, plus SP (see Subroutines and the stack).
| Instruction | What it does | Opcode | Cycles |
|---|---|---|---|
LDR Rd, mem | Load: Rd ← the word at the memory address | 1 | 7 |
STR Rd, mem | Store: the word at the memory address ← Rd | 2 | 7 |
ADD Rd, Rn, op2 | Rd ← Rn + op2 | 3 | 5 |
SUB Rd, Rn, op2 | Rd ← Rn − op2 | 4 | 5 |
MOV Rd, op2 | Rd ← op2 | 5 | 5 |
CMP Rn, op2 | Compare: work out Rn − op2, keep only the status flags | 6 | 5 |
AND Rd, Rn, op2 | Bitwise AND | 7 | 5 |
ORR Rd, Rn, op2 | Bitwise OR | 8 | 5 |
EOR Rd, Rn, op2 | Bitwise exclusive OR | 9 | 5 |
MVN Rd, op2 | Bitwise NOT of op2 | 10 | 5 |
LSL Rd, Rn, op2 | Logical shift left by op2 places (0 to 31) | 11 | 5 |
LSR Rd, Rn, op2 | Logical shift right by op2 places (0 to 31), filling with zeros | 12 | 5 |
B label | Branch always | 13 | 5 |
BEQ label | Branch if equal (Z = 1) | 14 | 5 |
BNE label | Branch if not equal (Z = 0) | 15 | 5 |
BGT label | Branch if greater than, signed (Z = 0 and N = V) | 16 | 5 |
BLT label | Branch if less than, signed (N ≠ V) | 17 | 5 |
INP Rd, device | Input into Rd (see Input / output) | 18 | 6 |
OUT Rd, device | Output the value in Rd (see Input / output) | 19 | 6 |
MOVT Rd, #value | Set the top 16 bits of Rd (0 to 65535), keeping the bottom 16 | 20 | 5 |
JSR label | Jump to subroutine: push the return address on the stack, then branch to the label | 21 | 7 |
RTS | Return from subroutine: pop the return address from the stack into PC | 22 | 7 |
PUSH Rd | Push Rd on the stack: SP ← SP − 1, then the word at SP ← Rd | 23 | 7 |
POP Rd | Pop the top of the stack into Rd, then SP ← SP + 1 | 24 | 7 |
HALT | Stop the processor | 0 | 5 |
Only CMP changes the status flags, so a branch uses the result of the most recent CMP.
Every instruction takes 3 cycles to fetch, 1 to decode and 1 to 3 to execute. MOVT is rarely typed by hand: a MOV of a number too big for 17 bits is assembled as MOV plus MOVT.
The last four instructions go beyond the AQA specification. The stack is a block of memory (addresses 7168 to 8191) that grows downwards from the top. The register SP, the stack pointer, holds the address of the word on top and starts at 8192, which means empty. JSR pushes the address of the next instruction and RTS pops it again, so subroutines can call other subroutines, and themselves.
MOV R0, #5
JSR double // call the subroutine
OUT R0 // prints 10
HALT
double: ADD R0, R0, R0
RTS // return to the OUT
Use PUSH and POP to save registers a subroutine needs and give them back afterwards (pop in the reverse order). SP can also be used like any other register, for example LDR R1, [SP+1]. Pushing too much stops the program with a stack overflow, and popping an empty stack stops it with an underflow.
| Mode | Written as | Meaning |
|---|---|---|
| Register | R3 | The value in the register (the op2 of ADD, MOV, CMP and so on). |
| Immediate | #12 #-5 #0xFF #0b101 #'A' | The number itself. It must fit in 17 bits (−65536 to 65535). MOV can load bigger numbers. |
| Direct | LDR R0, totalSTR R0, 100 | The address is in the instruction: a label or a number from 0 to 8191. |
| Indirect | [R1] | The value in R1 is the memory address to use. |
| Indexed | [R1+4] [R1-2] [R1, #4] [R1+table] | The address is the value in R1 plus a fixed offset (which may be negative or a label). Use it to step through arrays and strings. |
LDR and STR are the only instructions that touch memory, and they take direct, indirect or indexed addresses.MOV R1, #label.R1 + R2, add them into a register first and use that.MOV #value (outside −65536 to 65535) is split into two words automatically: a MOV for the bottom 16 bits and a MOVT for the top 16 – see Instructions.// add up five numbers
MOV R0, #0
MOV R1, #0
loop: LDR R2, [R1+numbers] // indexed
ADD R0, R0, R2
ADD R1, R1, #1
CMP R1, #5
BNE loop
HALT
numbers: DAT 10, 20, 30, 40, 50
Every instruction is one 32-bit word:
Rn (mode 3) or the device code for INP and OUT.HALT, so a program that runs off its end stops.Example: ADD R2, R0, R1 is stored as
00011 00 0010 0000 00000000000000001 ADD reg R2 R0 operand 2 = R1 (406847489)
The Cycle button steps through how the processor fetches, decodes and executes it.
JSR, RTS, PUSH and POP. It is not part of the AQA specification and can be ignored unless you want to explore the stack.CMP: N negative (top bit of the result), Z zero (result is 0), C carry (set when Rn ≥ op2 as unsigned numbers, that is no borrow), V signed overflow.8192 words of 32 bits. Each address holds a whole word, not a single byte.
| Addresses | Use |
|---|---|
| 0 – 4095 | Your program and data. The program is loaded at address 0 and starts there. |
| 4096 – 7167 | Video memory: the 64 × 48 colour screen. |
| 7168 – 8191 | The stack (shaded green in the Memory panel). It grows downwards from 8191. Free memory when you do not use it. |
INP Rd, deviceLeaving out the device means device 2.
| Device | Gives Rd |
|---|---|
2 | A number typed in the input box. The program waits until you press Enter. |
4 | The code of the last key pressed, or 0 if none. |
5 | As 4, then forgets the key so it is only read once. |
8 | A random 32-bit number. |
Key codes: left 37, up 38, right 39, down 40, space 32, Enter 13, the digits 48 to 57 and the letters A to Z 65 to 90 (upper case). On a phone or tablet use the on-screen keys.
OUT Rd, deviceLeaving out the device means device 4.
| Device | Shows the value in Rd as |
|---|---|
4 | A signed decimal number, then a new line. |
5 | An unsigned decimal number, then a new line. |
6 | Hexadecimal such as 0xFF, then a new line. |
7 | A single character (its ASCII code), with no new line. |
8 | A string. Rd holds the address of the first character (see DAT strings). |
Device numbers match Peter Higginson's AQA simulator for compatibility.
Video memory starts at address 4096. Each pixel is one word, in the form 0xRRGGBB (red, green and blue, 8 bits each; the top byte is ignored). The pixel at column x and row y (0 to 63, 0 to 47) is at address 4096 + y × 64 + x. Store a colour there and the pixel changes.
MOV R0, #4096
MOV R1, #0xFF8800 // orange
STR R1, [R0] // top left pixel
HALT
A label is a name followed by a colon, at the start of a line (it may be on its own line). It stands for the address of the next instruction or data word. Use it as a branch target (BNE loop), as a memory address (LDR R0, total) or as a value (MOV R1, #table).
R1 or SP.[R1+table], #table+1.A label starting with a dot, such as .loop:, belongs only to the nearest ordinary label above it - usually a subroutine. The same local name can be reused in every subroutine without clashing, so every loop can just be called .loop instead of inventing names like loop2 or loopDraw.
drawTet:
MOV R3, #0
.loop:
CMP R3, #4
BEQ .done
ADD R3, R3, #1
B .loop
.done:
RTS
testTet:
MOV R3, #0
.loop: // a different ".loop", local to testTet
CMP R3, #4
BEQ .done
ADD R3, R3, #1
B .loop
.done:
RTS
EQUA name, a space, EQU and a value gives that name to a fixed number, without a colon and without using up any memory. Use it anywhere a label could go, such as an immediate value or an offset.
screenStart EQU 4096
maxScore EQU 100
MOV R0, #maxScore
LDR R1, x
STR R2, [R1+screenStart]
EQU line (no forward references).DATDAT puts values into memory, one word each, at that point in the program. A label in front of it names the first word.
total: DAT 0 // one variable, starts at 0 nums: DAT 5, 10, 15 // three words in a row big: DAT 0xFF00FF, -1 // hex and negative values letter: DAT 'A' // a character code msg: DAT "Hello\n" // text, ended by a zero ptr: DAT msg // the address of another label
Put your data after HALT so the processor never tries to run it as instructions. In a string, four characters are packed into each word, and a zero ends it. Escapes: \n new line, \t tab, \\ backslash, \" quote.
42, negative -7, hex 0x2A, binary 0b101010 and characters 'A'.# in front of a number used as an operand: MOV R0, #42./ or ; and run to the end of the line. Writing // also works.CLOCK 1000 directive sets and locks the clock speed to the nearest available value whenever this program assembles - handy for a program that only looks right at a particular speed. It emits no instruction or data. Remove the directive and reassemble to choose your own speed again.Write and run assembly language programs, suitable for AQA A Level Computer Science, and watch a simple CPU execute them: registers, memory, buses and the fetch-decode-execute cycle.
INP and OUT handle the keyboard and output.Version
© 2026 Neil Kendall - More @ www.korovatron.co.uk