Edexcel International Advanced Level Computer Science, point 13.1.3
Definition
Assembly language is a low-level programming language that uses short, memorable text codes (mnemonics) in place of the binary machine code instructions. Each assembly instruction corresponds (usually one-to-one) to one machine code instruction, and is specific to a processor type. A program called an assembler translates it into machine code.
The specification uses ARMlite, a simplified version of the ARM instruction set (Appendix 3 of the specification), which you are given in the exam. It has 13 general-purpose registers, R0 to R12.
Function and why needed
- It gives direct control of the hardware, with small, fast programs (for embedded systems, device drivers, operating system parts, 11.1.5).
- It shows how the processor really works (the fetch-decode-execute cycle, 1.1.5).
- It is much easier to read and write than binary machine code.
Characteristics
- Low level: close to the machine, one instruction per processor operation.
- Machine-dependent (not portable): ARM code won't run on an x86 processor.
- Very detailed: you manage registers and memory yourself.
- Fast and small when written well, but slow to write, hard to read, and error-prone.
a. Mnemonics
A mnemonic is a short text name for an instruction: ADD, SUB, MOV, LDR, STR, CMP, B, HALT. It replaces the binary opcode, so you write ADD instead of 0100 0001....
b. Instructions: opcode and operand
An instruction has:
- An opcode (operation code): what to do (
ADD,LDR). - One or more operands: what to do it to (registers, values, or memory addresses).
ADD R2, R0, R1
^ ^ ^ ^
opcode | operands
destination register Rd, then Rn, then <operand>
In ARMlite, the first operand is normally the register where the result goes (Rd). The second operand of data-processing instructions can be an immediate value or a register.
c. Addressing modes
How an instruction says where its data is.
Immediate addressing
The operand is the data itself, written after a #. No memory access is needed.
MOV R0, #5 ; put the number 5 in R0
ADD R1, R0, #10 ; R1 = R0 + 10
CMP R0, #0 ; compare R0 with the number 0
Direct addressing
The operand is the address of a memory location (usually a label), and the data is read from or written to that location.
LDR R0, score ; load the contents of the location "score" into R0
STR R0, total ; store R0 into the location "total"
| Immediate | Direct | |
|---|---|---|
| Operand is | The value itself (#5) |
The address (a label) |
| Memory accessed | No | Yes |
| Speed | Faster | Slower (memory access) |
| Can change at run time? | No (fixed in the instruction) | Yes (the stored value can change) |
d. Operations (the ARMlite instruction set)
Arithmetic and logical
| Instruction | Meaning |
|---|---|
ADD Rd, Rn, <operand> |
Rd = Rn + operand |
SUB Rd, Rn, <operand> |
Rd = Rn - operand |
AND Rd, Rn, <operand> |
Bitwise AND |
ORR Rd, Rn, <operand> |
Bitwise OR |
EOR Rd, Rn, <operand> |
Bitwise exclusive OR (XOR) |
MVN Rd, <operand> |
Bitwise NOT |
LSL Rd, Rn, <operand> |
Logical shift left (multiply by a power of 2) |
LSR Rd, Rn, <operand> |
Logical shift right (divide by a power of 2) |
(Arithmetic is ADD and SUB. Bitwise is AND, ORR, EOR, MVN, LSL, LSR: see 2.2.2.)
Comparison and branching
| Instruction | Meaning |
|---|---|
CMP Rn, <operand> |
Compare and set the status flags (no result stored) |
B label |
Branch always (an unconditional jump) |
BEQ label |
Branch if equal |
BNE label |
Branch if not equal |
BGT label |
Branch if greater than |
BLT label |
Branch if less than |
Branches change the program counter (1.1.5), and make selection and loops possible.
Data movement
| Instruction | Meaning |
|---|---|
MOV Rd, <operand> |
Copy a value or register into Rd |
LDR Rd, <address> |
Load from memory into Rd |
STR Rd, <address> |
Store Rd into memory |
Halt
HALT stops the program.
e. Comments and labels
- A comment starts with a semicolon
;and is ignored by the assembler. Use comments on nearly every line, as assembly is hard to read otherwise. - A label is a name followed by a colon that marks a location, for branching (
loop:) or for data (total: .WORD 0).
Reading and tracing programs
Example 1: add two numbers
LDR R0, num1 ; R0 = value at num1
LDR R1, num2 ; R1 = value at num2
ADD R2, R0, R1 ; R2 = R0 + R1
STR R2, total ; store the result
HALT
num1: .WORD 7
num2: .WORD 5
total: .WORD 0
Trace:
| Instruction | R0 | R1 | R2 | total |
|---|---|---|---|---|
| (start) | 0 | 0 | 0 | 0 |
LDR R0, num1 |
7 | 0 | 0 | 0 |
LDR R1, num2 |
7 | 5 | 0 | 0 |
ADD R2, R0, R1 |
7 | 5 | 12 | 0 |
STR R2, total |
7 | 5 | 12 | 12 |
HALT |
Example 2: a loop (sum of 1 to 5)
MOV R0, #0 ; R0 = total, starts at 0
MOV R1, #1 ; R1 = counter, starts at 1
loop:
CMP R1, #5 ; compare counter with 5
BGT done ; if counter > 5, finish
ADD R0, R0, R1 ; total = total + counter
ADD R1, R1, #1 ; counter = counter + 1
B loop ; go round again
done:
STR R0, result ; store the total
HALT
result: .WORD 0
Trace (the loop):
| Pass | R1 at test | R1 > 5? |
R0 after |
|---|---|---|---|
| 1 | 1 | No | 1 |
| 2 | 2 | No | 3 |
| 3 | 3 | No | 6 |
| 4 | 4 | No | 10 |
| 5 | 5 | No | 15 |
| test | 6 | Yes: branch to done |
15 |
result holds 15. In Python this is total = 0; for i in range(1, 6): total += i.
Example 3: selection (the larger of two numbers)
LDR R0, a
LDR R1, b
CMP R0, R1 ; compare a with b
BGT first ; if a > b
STR R1, big ; else the bigger is b
B end
first:
STR R0, big ; the bigger is a
end:
HALT
a: .WORD 12
b: .WORD 9
big: .WORD 0
With a = 12 and b = 9, CMP finds 12 > 9, BGT branches to first, and 12 is stored in big.
How high-level code maps to assembly
| High-level | Assembly |
|---|---|
x = 5 |
MOV R0, #5 |
y = x + 3 |
ADD R1, R0, #3 |
if a > b: |
CMP R0, R1 then BGT label |
while i <= 5: |
a label, CMP, a conditional branch out, and B back to the label |
x = x * 4 |
LSL R0, R0, #2 |
Writing simple programs
- Plan in pseudocode (or comments).
- Choose registers for each variable, and labels for stored data.
- Load data from memory into registers (
LDR). - Do the calculation with registers.
- Use
CMPand branches for decisions and loops. - Store results (
STR) and end withHALT. - Comment every line, and trace it with test data.
Benefits
- Fast, small programs.
- Full control of the hardware.
- Helps understanding of how processors work.
- No translation inefficiency from a compiler.
- Essential for some embedded and driver code.
Drawbacks
- Hard and slow to write, and hard to read and debug.
- Not portable: specific to a processor.
- Long programs for simple tasks.
- More errors (no type checking, manual register and memory management).
- Harder to maintain.
Worked example
Task: write ARMlite code to double a number stored at n and store the answer at answer, then trace it with n = 6.
LDR R0, n ; load n into R0
LSL R1, R0, #1 ; R1 = R0 shifted left by 1 (R0 x 2)
STR R1, answer ; store the result
HALT
n: .WORD 6
answer: .WORD 0
| Instruction | R0 | R1 | answer |
|---|---|---|---|
LDR R0, n |
6 | 0 | 0 |
LSL R1, R0, #1 |
6 | 12 | 0 |
STR R1, answer |
6 | 12 | 12 |
Why LSL? Shifting left by 1 doubles a binary number (2.2.2). An ADD R1, R0, R0 would give the same result.
Exam tip: you are given the instruction set in the exam, so learn how to use it, not how to memorise it. Trace with a table of registers, one instruction at a time. Remember CMP only sets flags, and the branch uses them. Label immediate (#) and direct (label) addressing correctly.
Check yourself
What is the difference between an opcode and an operand?
The opcode says which operation to perform (such as ADD). The operand is the data, register or address that it acts on.
What is the difference between immediate and direct addressing?
Immediate addressing puts the data value in the instruction itself (#5). Direct addressing gives the memory address where the data is stored (a label).
What does CMP R0, #10 followed by BEQ done do?
It compares R0 with 10 and branches to done if they are equal.
What does LSL R1, R0, #2 do?
It shifts the value in R0 two places left and stores the result in R1, which multiplies it by 4.
Give two drawbacks of assembly language
Any two of: hard to read and write; not portable between processors; long programs; error-prone; hard to maintain.