A levels Computer Science · Computer systems
Assembly language
1Machine code and assembly language
The processor only understands machine code: binary instructions. Each instruction has an opcode (the operation) and usually an operand (the data or address to use).
Assembly language replaces the binary opcodes with short mnemonics such as LDR, ADD and STR. Each assembly instruction translates to exactly one machine-code instruction (a one-to-one relationship), using a program called an assembler.
| Machine code | Assembly | Meaning |
|---|---|---|
| 0001 0110 0100 | LDR R0, 100 | load the value at address 100 into register R0 |
| 0011 ... | ADD R2, R0, R1 | R2 = R0 + R1 |
Assembly is used where speed, small size or direct hardware control matter, such as device drivers and embedded systems. It is specific to one processor type.
Check yourself
What is the relationship between assembly instructions and machine-code instructions?
Show the answer
One to one
Each mnemonic is one instruction.
2The instruction set
This lesson uses a typical A-level instruction set. Rd and Rn are registers; operand2 is either a register or an immediate value written #n.
| Instruction | Meaning |
|---|---|
| LDR Rd, mem | load the value at memory address mem into Rd |
| STR Rd, mem | store the value in Rd at address mem |
| MOV Rd, operand2 | copy a value into Rd |
| ADD Rd, Rn, operand2 | Rd = Rn + operand2 |
| SUB Rd, Rn, operand2 | Rd = Rn − operand2 |
| CMP Rn, operand2 | compare, setting the status flags |
| B label | always branch (jump) to label |
| BEQ / BNE / BGT / BLT label | branch if equal / not equal / greater / less (after CMP) |
| AND, ORR, EOR, MVN | bitwise AND, OR, XOR, NOT |
| LSL, LSR | logical shift left, right |
| HALT | stop the program |
Check yourself
What does MOV R3, #10 do?
Show the answer
Puts the value 10 into register R3
means the number itself.
3Addressing modes
The addressing mode says how the operand should be understood.
The same operand, 50, gives four different values depending on the addressing mode.
| Mode | Operand means | Example |
|---|---|---|
| immediate | the value itself | #50 means 50 |
| direct | the address of the value | 50 means "the value at address 50" (70) |
| indirect | the address of the address of the value | go to 50, find 70, use the value at 70 (12) |
| indexed | a base address plus the index register | 50 + index 3 means the value at 53 |
Indexed addressing makes it easy to step through an array: increase the index register by 1 each time round a loop.
Check yourself
Address 30 holds 45 and address 45 holds 9. With indirect addressing and operand 30, what value is used?
Show the answer
9
Two steps.
4A first program
Add two numbers
- LDR R0, 100 — load the first number.
- LDR R1, 101 — load the second number.
- ADD R2, R0, R1 — add them.
- STR R2, 102 — store the result at address 102.
- HALT.
- If addresses 100 and 101 hold 25 and 17, address 102 ends up holding 42.
Assembly programs work through registers: values are loaded from memory into registers, processed, then stored back.
Check yourself
Why does the program load values into registers before adding them?
Show the answer
The ADD instruction works on registers, which are fast storage inside the CPU
Load, process, store.
5Selection and iteration with branches
There are no IF statements or loops in assembly. Instead, a CMP sets the status flags and a conditional branch jumps to a label.
Add 1 to 5 in a loop
- 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 — has the counter reached 6?
- BNE loop — if not, go back to loop.
- STR R0, 200 — store the total.
- HALT. The total stored is 1 + 2 + 3 + 4 + 5 = 15.
Store the larger of R0 and R1 in R2
- CMP R0, R1.
- BGT first — if R0 > R1, jump.
- MOV R2, R1 — otherwise R1 is larger.
- B done.
- first: MOV R2, R0.
- done: HALT.
Check yourself
In the loop example, how many times does BNE jump back?
Show the answer
4
Counter 2, 3, 4, 5 jump; 6 does not.
6Bitwise operations and masks
A mask is a binary pattern used with a bitwise operation to pick out or change certain bits.
| Operation | Use | Example |
|---|---|---|
| AND with mask | keep the bits where the mask is 1, clear the rest | 1011 0110 AND 0000 1111 = 0000 0110 |
| ORR with mask | set the bits where the mask is 1 | 1011 0110 ORR 0000 0001 = 1011 0111 |
| EOR with mask | flip the bits where the mask is 1 | 1011 0110 EOR 1111 0000 = 0100 0110 |
| LSL #n | shift left n places: multiply by 2ⁿ | 0000 0101 LSL #2 = 0001 0100 |
AND with 0000 0001 tests whether a number is odd (the result is 1 if it is).
Check yourself
What is 1100 1010 AND 0000 1111?
Show the answer
0000 1010
Keeps the low four bits.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera