impera Open Impera

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 codeAssemblyMeaning
0001 0110 0100LDR R0, 100load the value at address 100 into register R0
0011 ...ADD R2, R0, R1R2 = 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?

  • Many to one

  • One to many

  • One to one

  • There is no relationship

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.

InstructionMeaning
LDR Rd, memload the value at memory address mem into Rd
STR Rd, memstore the value in Rd at address mem
MOV Rd, operand2copy a value into Rd
ADD Rd, Rn, operand2Rd = Rn + operand2
SUB Rd, Rn, operand2Rd = Rn − operand2
CMP Rn, operand2compare, setting the status flags
B labelalways branch (jump) to label
BEQ / BNE / BGT / BLT labelbranch if equal / not equal / greater / less (after CMP)
AND, ORR, EOR, MVNbitwise AND, OR, XOR, NOT
LSL, LSRlogical shift left, right
HALTstop the program
Check yourself

What does MOV R3, #10 do?

  • Moves R3 to address 10

  • Puts the value 10 into register R3

  • Loads the value at address 10 into R3

  • Adds 10 to R3

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.

ModeOperand meansExample
immediatethe value itself#50 means 50
directthe address of the value50 means "the value at address 50" (70)
indirectthe address of the address of the valuego to 50, find 70, use the value at 70 (12)
indexeda base address plus the index register50 + 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?

  • 39

  • 9

  • 30

  • 45

Show the answer

9

Two steps.

4A first program

Worked example

Add two numbers

  1. LDR R0, 100 — load the first number.
  2. LDR R1, 101 — load the second number.
  3. ADD R2, R0, R1 — add them.
  4. STR R2, 102 — store the result at address 102.
  5. HALT.
  6. 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?

  • Memory cannot store numbers, only instructions waiting to run

  • The ADD instruction works on registers, which are fast storage inside the CPU

  • The HALT instruction needs the values to be in a register

  • Registers are larger than memory, so they hold bigger values

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.

Worked example

Add 1 to 5 in a loop

  1. MOV R0, #0 — total = 0.
  2. MOV R1, #1 — counter = 1.
  3. loop: ADD R0, R0, R1 — total = total + counter.
  4. ADD R1, R1, #1 — counter = counter + 1.
  5. CMP R1, #6 — has the counter reached 6?
  6. BNE loop — if not, go back to loop.
  7. STR R0, 200 — store the total.
  8. HALT. The total stored is 1 + 2 + 3 + 4 + 5 = 15.
Worked example

Store the larger of R0 and R1 in R2

  1. CMP R0, R1.
  2. BGT first — if R0 > R1, jump.
  3. MOV R2, R1 — otherwise R1 is larger.
  4. B done.
  5. first: MOV R2, R0.
  6. done: HALT.
Check yourself

In the loop example, how many times does BNE jump back?

  • 5

  • 4

  • 6

  • 1

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.

OperationUseExample
AND with maskkeep the bits where the mask is 1, clear the rest1011 0110 AND 0000 1111 = 0000 0110
ORR with maskset the bits where the mask is 11011 0110 ORR 0000 0001 = 1011 0111
EOR with maskflip the bits where the mask is 11011 0110 EOR 1111 0000 = 0100 0110
LSL #nshift 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?

  • 0011 0101

  • 1100 0000

  • 0000 1010

  • 1100 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