Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Assembly Language

4.7.3.5 Machine code / assembly language operations

Aligned to the AQA 7517 specification

Level
Intermediate
Reading time
5 min
Published
13 June 2026
Updated
1 July 2026
On this page
  1. 1.Assembly Language Basics
  2. 2.Data Transfer: LOAD and STORE
  3. 3.Arithmetic: ADD and SUBTRACT
  4. 4.Comparison and Branching
  5. 5.Bitwise Logic: AND, OR, NOT, XOR
  6. 6.Shift Instructions
  7. 7.HALT
  8. 8.Worked Program Example
  9. 9.Common Exam Mistakes

Key takeaways

  • Assembly language uses human-readable mnemonics that an assembler translates one-to-one into the binary machine code instructions for the target processor.
  • LDR copies data from memory or a literal into a register; STR copies data from a register into memory, with the destination register first in LDR and the source register first in STR.
  • COMPARE (CMP) subtracts two values and sets the status register flags without storing the result, so it must precede a conditional branch which tests those flags.
  • A logical shift left by 1 multiplies an unsigned value by 2 and a logical shift right by 1 performs integer division by 2; bits shifted out are discarded and zeros fill vacated positions.
  • Every well-formed assembly program ends with HALT; without it the processor continues into the next memory location and executes whatever binary data is there as instructions.

Assembly Language Basics

Assembly language uses human-readable mnemonics to represent machine code instructions. An assembler performs a one-to-one translation of each mnemonic into the corresponding binary machine code instruction for the target processor.

AQA assembly notation used in exam questions:

  • Rn refers to a general-purpose register (R0, R1, R2, …)
  • #n denotes an immediate operand (the literal value n)
  • n without # is a direct address (the value at memory address n)

Data Transfer: LOAD and STORE

LOAD copies data from memory (or a literal) into a register.

LDR R1, 200      ; R1 ← contents of memory address 200 (direct)
LDR R1, #25     ; R1 ← 25 (immediate — load the value 25 itself)

STORE copies data from a register into memory.

STR R1, 200     ; memory[200] ← R1

The processor cannot operate on data while it is in memory — it must be loaded into a register first. Results must be stored back to memory to persist after the program ends.

Arithmetic: ADD and SUBTRACT

ADD R1, R2, R3      ; R1 ← R2 + R3
ADD R1, R2, #10     ; R1 ← R2 + 10  (immediate)

SUB R1, R2, R3      ; R1 ← R2 − R3
SUB R1, R2, #5      ; R1 ← R2 − 5

Results are stored in the destination register (first operand). The status register flags (zero, carry, negative) are updated after arithmetic operations. Conditional branches then test these flags.

Worked examples:

InstructionBeforeAfter
ADD R1, R2, #10R2=5R1=15
SUB R1, R2, R3R2=20, R3=7R1=13
SUB R0, R0, #1R0=1R0=0, zero flag set

Comparison and Branching

COMPARE subtracts two values and sets the status register flags — without storing the result.

CMP R1, R2       ; set flags based on R1 − R2 (result discarded)
CMP R1, #0       ; compare R1 with zero

CMP is used exclusively to set flags for a subsequent conditional branch.

BRANCH instructions:

MnemonicConditionWhen executed
B labelUnconditionalJump to label
BEQ labelEqual (zero flag set)Jump if last CMP result was 0
BNE labelNot equalJump if last CMP result was not 0
BGT labelGreater thanJump if R1 > R2 in last CMP
BLT labelLess thanJump if R1 < R2 in last CMP

Branch instructions overwrite the Program Counter with the target address, redirecting execution.

How much of this have you taken in?

Quiz yourself on this section, free, no card needed.

Test myself

Bitwise Logic: AND, OR, NOT, XOR

These instructions apply Boolean operations bit by bit to each corresponding bit of the operands.

AND R1, R2, R3    ; R1 ← R2 AND R3 (bitwise)
ORR R1, R2, R3   ; R1 ← R2 OR R3
EOR R1, R2, R3   ; R1 ← R2 XOR R3 (Exclusive OR)
MVN R1, R2       ; R1 ← NOT R2 (bitwise invert — "Move Negative")

Common uses:

  • AND with a mask to isolate specific bits: AND R1, R1, #0b00001111 keeps only the lower 4 bits
  • ORR to set specific bits to 1 without affecting others
  • EOR to toggle specific bits
  • MVN to flip all bits (two's complement negation requires MVN + ADD #1)

Shift Instructions

Shift operations move all bits left or right within a register.

LSL R1, R2, #n    ; R1 ← R2 shifted left n bits (Logical Shift Left)
LSR R1, R2, #n    ; R1 ← R2 shifted right n bits (Logical Shift Right)

Effect on values (unsigned):

OperationEffectExample (8-bit)
Left shift by 1Multiply by 20000 0101 (5) → 0000 1010 (10)
Left shift by 2Multiply by 40000 0101 (5) → 0001 0100 (20)
Right shift by 1Integer divide by 20000 1010 (10) → 0000 0101 (5)

Bits shifted out are discarded; zeros fill vacated positions (logical shift).

HALT

HALT    ; stop execution

HALT terminates the program. The processor stops fetching and executing instructions.

Why HALT is necessary: without a HALT instruction, the processor continues into the next memory location after the program, executing whatever binary data happens to be there as if it were instructions. This produces undefined, often catastrophic behaviour — corrupted data, crashes, or security vulnerabilities. Every well-formed assembly program ends with HALT (or equivalent: RET for subroutine return, SWI for operating system service call).

In operating system terms, HALT typically triggers a software interrupt that returns control to the OS process scheduler rather than literally stopping the processor. The processor then executes the next scheduled process.

Worked Program Example

Sum of 1 to n (n stored at address 100, result to address 101):

LDR R0, 100      ; R0 ← n
LDR R1, #0       ; R1 ← 0 (sum accumulator)
LDR R2, #0       ; R2 ← 0 (counter)
LOOP:
  CMP R2, R0     ; compare counter with n
  BGT END        ; if counter > n, exit loop
  ADD R1, R1, R2 ; sum += counter
  ADD R2, R2, #1 ; counter++
  B LOOP         ; repeat
END:
  STR R1, 101    ; store result
  HALT

Common Exam Mistakes

1. Confusing LOAD and STORE direction

LDR moves data from memory into a register. STR moves data from a register into memory. The destination register comes first in LDR. The source register comes first in STR.

2. Forgetting COMPARE before a conditional branch

Conditional branches check the status register flags. Those flags are set by CMP (or the last arithmetic instruction). A conditional branch without a preceding CMP may jump based on stale flags from a previous operation.

3. Treating a shift as a multiplication instruction

Shifts multiply/divide by powers of 2 only, and only for non-negative values (logical shift discards overflow bits). They are not equivalent to a general multiply instruction — LSL #3 is efficient only if you specifically need to multiply by 8.

4. Confusing EOR and OR for toggling

ORR sets bits to 1 (cannot clear). EOR toggles bits — bits that are 1 in the mask flip; bits that are 0 in the mask stay unchanged. To toggle bit 3: EOR R1, R1, #0b00001000.

Key terms

Assembly language
A low-level language that uses human-readable mnemonics to represent machine code instructions.
Mnemonic
A human-readable code, such as LDR or ADD, that an assembler translates one-to-one into a binary machine code instruction.
Immediate operand
A literal value used directly in an instruction, denoted in AQA notation by #n.
Direct address
An operand written as n without #, referring to the value held at memory address n.
LOAD (LDR)
An instruction that copies data from memory or a literal into a register.
STORE (STR)
An instruction that copies data from a register into memory.
COMPARE (CMP)
An instruction that subtracts two values to set the status register flags without storing the result.
Branch instruction
An instruction that overwrites the Program Counter with a target address to redirect execution, either unconditionally or based on the status flags.
Mask
A bit pattern used with a bitwise operation, such as AND to isolate specific bits or EOR to toggle them.
Logical shift
A shift that moves all bits left or right within a register, discarding bits shifted out and filling vacated positions with zeros.
HALT
An instruction that terminates the program so the processor stops fetching and executing instructions.

Frequently asked questions

LDR (LOAD) copies data from memory or a literal into a register, with the destination register first. STR (STORE) copies data from a register into memory, with the source register first. The processor cannot operate on data in memory, so it must be loaded into a register first.

Conditional branches such as BEQ and BNE check the status register flags, and those flags are set by CMP or the last arithmetic instruction. A conditional branch without a preceding CMP may jump based on stale flags from a previous operation.

HALT terminates the program so the processor stops fetching and executing instructions. Without it the processor continues into the next memory location and executes whatever binary data is there as if it were instructions, producing undefined behaviour such as corrupted data or crashes.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Processor Components and the Fetch-Execute Cycle

Next

Interrupts and Processor Performance

Related lessons

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.