CIE AS & A Level Computer Science, section 4.2 (Assembly language)
The instruction set
| Instruction | Explanation |
|---|---|
LDM #n |
load the number into ACC (immediate) |
LDD <address> |
load the contents of the address into ACC (direct) |
LDI <address> |
indirect: the address holds another address; load the contents of that |
LDX <address> |
indexed: address plus the contents of IX; load from there |
LDR #n |
load the number into IX |
MOV <register> |
move the contents of ACC to IX |
STO <address> |
store the contents of ACC at the address |
ADD <address> or ADD #n |
add to ACC |
SUB <address> or SUB #n |
subtract from ACC |
INC <register> / DEC <register> |
add or subtract 1 for ACC or IX |
JMP <address> |
jump to the address |
CMP <address> or CMP #n |
compare ACC with the contents or value |
JPE <address> / JPN <address> |
jump if the compare was equal / not equal |
IN / OUT |
read or write one character (ASCII) using ACC |
END |
stop |
# means a denary number, B a binary number and & a hexadecimal number.
Example 1: add two numbers
Memory: address 200 holds 7, address 201 holds 5.
LDD 200 ACC <- 7
ADD 201 ACC <- 12
STO 202 address 202 <- 12
END
Example 2: a loop that counts to 3
LDM #0
STO COUNT
LOOP: LDD COUNT
INC ACC
STO COUNT
CMP #3
JPN LOOP
END
COUNT: 0
| Step | ACC | COUNT | Compare |
|---|---|---|---|
after LDM #0 and STO |
0 | 0 | |
| pass 1 | 1 | 1 | , jump |
| pass 2 | 2 | 2 | , jump |
| pass 3 | 3 | 3 | , no jump |
The program then reaches END with COUNT equal to 3.
How to trace
Make a table with a column for ACC, IX and each memory location used, and update it after each instruction.
Common mistake: updating the wrong register. STO copies the ACC to memory and leaves the ACC unchanged. CMP does not change ACC; it only sets a flag used by JPE and JPN. Also, remember the PC moves to the next line unless a jump occurs.
Exam tip: use a trace table with a column for every register and memory location, and a row for every instruction executed (including each pass of a loop). Check the final values.
Check yourself
After `LDM #5` then `ADD #3`, what does ACC contain?
.
What does `JPN LOOP` do?
After a compare, it jumps to LOOP if the compared values were not equal.
Does `STO 300` change the accumulator?
No, it copies the accumulator to address 300 and leaves the accumulator unchanged.