✓ Free · Component 1 · 1.2.4 Types of Programming Language
1.2.4a Machine Code and Assembly Language
OCR H446 · A Level Computer Science · ~12 min read
Notes
Video
Slides
Worksheet
Quiz

Levels of Programming Languages

Programming languages exist at different levels of abstraction from the underlying hardware. Lower-level languages are closer to the hardware; higher-level languages are closer to natural language and further from hardware.

LevelExampleAbstraction
Low-levelMachine code, AssemblyClose to hardware — operates directly on CPU registers and memory
High-levelPython, Java, C++Far from hardware — abstract instructions, portable across platforms

Machine Code

Machine code is the only language a CPU can directly execute. It consists entirely of binary digits (0s and 1s) — every instruction is encoded as a binary pattern that the CPU's control unit decodes and executes.

  • Machine code is processor-specific: instructions for an Intel x86 processor cannot run on an ARM processor (they have different instruction sets).
  • There is no translation needed — machine code instructions are fetched, decoded, and executed directly during the Fetch-Decode-Execute cycle.
  • Machine code is extremely difficult for humans to write and read — programmers rarely write machine code directly today.

Example machine code instruction (8-bit hypothetical): 10110000 01100101 — the first byte might be the opcode (operation) and the second byte the operand (data or address).

Machine Code Instruction Format

Each machine code instruction contains:

  • Opcode: the operation to be performed (e.g. ADD, LOAD, STORE, JUMP). Tells the CPU what to do.
  • Operand: the data or memory address the instruction operates on. Can be: a value (immediate), a register number, or a memory address.
FieldPurposeExample (binary)
OpcodeSpecifies operation (ADD, LOAD, STORE, JUMP)1011 (4 bits)
OperandRegister number / memory address / immediate value00000101 (8 bits)

Assembly Language

Assembly language is a low-level programming language that uses mnemonics (human-readable abbreviations) to represent machine code instructions. Each assembly instruction corresponds one-to-one with a machine code instruction.

Assembly language is architecture-specific — assembly code for an x86 processor cannot run directly on an ARM processor.

Key Features of Assembly Language

  • Mnemonics: short, memorable abbreviations for operations — MOV (move/copy), ADD (add), SUB (subtract), LDR (load from memory), STR (store to memory), CMP (compare), B (branch/jump).
  • Registers: assembly refers directly to CPU registers by name — R0, R1, R2... (ARM) or AX, BX, CX (x86).
  • Labels: named points in the code used as targets for jump/branch instructions, e.g. LOOP: or START:.
  • Comments: using ; to annotate code.

Simple Assembly Program (ARM-style, OCR H446 pseudo-assembly)

; Load two values, add them, store result
LDR R1, 100      ; Load value from memory address 100 into R1
LDR R2, 101      ; Load value from memory address 101 into R2
ADD R3, R1, R2   ; R3 = R1 + R2
STR R3, 102      ; Store R3 into memory address 102
HALT             ; Stop the program

Common OCR H446 Assembly Mnemonics

MnemonicOperationExample
LDR Rx, <address>Load contents of memory address into register RxLDR R1, 200
STR Rx, <address>Store contents of Rx into memory addressSTR R1, 200
MOV Rx, <operand>Copy operand value (or register) into RxMOV R0, #5
ADD Rx, Ry, RzRx = Ry + RzADD R3, R1, R2
SUB Rx, Ry, RzRx = Ry − RzSUB R3, R1, R2
CMP Rx, <operand>Compare Rx with operand (sets condition flags)CMP R1, #0
B <label>Unconditional branch (jump) to labelB LOOP
BEQ <label>Branch if equal (zero flag set)BEQ END
BNE <label>Branch if not equalBNE LOOP
BGT <label>Branch if greater thanBGT BIGGER
BLT <label>Branch if less thanBLT SMALLER
HALTStop executionHALT
AND Rx, Ry, RzBitwise AND: Rx = Ry AND RzAND R1, R2, R3
ORR Rx, Ry, RzBitwise OR: Rx = Ry OR RzORR R1, R2, R3
EOR Rx, Ry, RzBitwise XOR: Rx = Ry XOR RzEOR R1, R2, R3
LSL Rx, Ry, <n>Logical shift left Ry by n bits, result in RxLSL R1, R1, #2
LSR Rx, Ry, <n>Logical shift right Ry by n bits, result in RxLSR R1, R1, #1

The Assembler

An assembler is a program that translates assembly language code into machine code. It replaces each mnemonic with its binary equivalent opcode and converts labels and addresses to their binary representations.

  • The translation is essentially a one-to-one mapping: one assembly instruction → one machine code instruction.
  • The assembler also handles labels: during a first pass it notes the address of each label; during a second pass it substitutes label names in branch instructions with the actual binary address.

Why Use Assembly Language?

Despite the existence of high-level languages, assembly is still used because:

  • Speed and performance: assembly gives direct control over CPU operations. No overhead from abstraction layers — critical for performance-sensitive code.
  • Memory efficiency: assembly produces very compact, memory-efficient machine code — critical in embedded systems with very limited RAM.
  • Direct hardware access: essential for device drivers, bootloaders, and OS kernels that must interact directly with hardware registers and memory-mapped I/O.
  • Embedded systems: small microcontrollers (e.g. in washing machines, medical devices) have limited resources and benefit from hand-optimised assembly.

Comparison: Machine Code vs Assembly vs High-level

FeatureMachine CodeAssembly LanguageHigh-level Language
ReadabilityNone — binaryLow — mnemonicsHigh — near natural language
PortabilityNone — CPU specificNone — architecture specificHigh — portable (with compiler)
Translation neededNone — direct executionAssemblerCompiler or interpreter
Execution speedFastestVery fastSlower (abstraction overhead)
Development speedVery slowSlowFast
Level of controlMaximumHighLow
Typical useGenerated by assembler/compilerDevice drivers, bootloaders, embeddedApplication software, web, games
Exam tip: Know the difference between machine code and assembly language clearly. Machine code is binary — directly executed by the CPU. Assembly uses mnemonics — translated to machine code by an assembler. Each assembly instruction maps one-to-one to a machine code instruction.
Exam tip: For the OCR H446 exam, you may be asked to trace through assembly programs, convert simple programs, or explain what mnemonics do. Learn the standard OCR H446 instruction set (LDR, STR, MOV, ADD, SUB, CMP, B, BEQ, BNE, BGT, BLT, AND, ORR, EOR, LSL, LSR, HALT).
⚠ Common Mistakes
  • Saying assembly language is directly executed — assembly must first be translated to machine code by an assembler. Only machine code is directly executed by the CPU.
  • Saying machine code is portable — machine code is processor-specific. Machine code for x86 will not run on ARM without translation.
  • Confusing opcode and operand — opcode is the operation (what to do); operand is the data/address (what to do it with).
✓ Notes completed!
Video coming soon
Click to advance · Arrow keys also work
Click slide or press arrow keys to navigate

Worksheet — 1.2.4a Machine Code & Assembly Language

8 questions · 20 marks · instantly marked

Q1Explain the difference between machine code and assembly language. State which requires an assembler and why.[3 marks]
✓ Mark scheme
Machine code consists of binary instructions (0s and 1s) that can be directly executed by the CPU [1]; assembly language uses human-readable mnemonics (e.g. ADD, LDR, MOV) to represent machine code instructions [1]; assembly language requires an assembler to translate it into machine code because the CPU can only directly execute binary machine code instructions [1].
Q2Describe the structure of a machine code instruction, identifying its two main parts and explaining the role of each.[3 marks]
✓ Mark scheme
The two parts are the opcode and operand [1]; the opcode specifies the operation to be performed (e.g. ADD, LOAD, STORE, JUMP) — it tells the CPU what to do [1]; the operand contains the data or memory address that the instruction operates on — it can be an immediate value, a register number, or a memory address [1].
Q3Trace through the following assembly program and state the final value stored in R3 and at memory address 102:[4 marks]
MOV R1, #12
MOV R2, #8
ADD R3, R1, R2
SUB R1, R3, R2
STR R3, 102
✓ Mark scheme
MOV R1, #12 → R1 = 12 [1]; MOV R2, #8 → R2 = 8 [1]; ADD R3, R1, R2 → R3 = 12 + 8 = 20 [1]; SUB R1, R3, R2 → R1 = 20 − 8 = 12 (R1 is changed, not R3); STR R3, 102 → memory address 102 is set to 20 [1]. Final: R3 = 20, memory address 102 = 20.
Q4Explain why machine code is described as 'processor-specific'. What does this mean for portability?[2 marks]
✓ Mark scheme
Machine code is processor-specific because different CPU architectures (e.g. x86, ARM) use different instruction sets — the binary encoding of the same operation differs between processors [1]; this means machine code has no portability — machine code written for one processor will not run on a different processor architecture without retranslation [1].
Q5Give three reasons why assembly language might be preferred over a high-level language in certain programming contexts. Give an example of where each reason applies.[3 marks]
✓ Mark scheme
Any three: Speed/performance — assembly executes faster with no abstraction overhead; e.g. real-time system or game engine critical loop [1]; Memory efficiency — assembly produces compact machine code with minimal memory usage; e.g. small microcontroller with 2KB RAM [1]; Direct hardware access — assembly can directly manipulate hardware registers and memory-mapped I/O; e.g. device driver or OS bootloader [1].
Q6Explain what a label is in assembly language and why it is needed.[2 marks]
✓ Mark scheme
A label is a named marker/identifier placed at a point in the assembly code (e.g. LOOP: or END:) [1]; it is needed so that branch/jump instructions (B, BEQ, BNE, etc.) can refer to a named destination rather than a raw memory address, making the code more readable and maintainable [1].
Q7Write an assembly program (using OCR H446 instruction set) that loads the value 10 into R1, loads the value 3 into R2, subtracts R2 from R1, and stores the result at memory address 200.[2 marks]
✓ Mark scheme
MOV R1, #10 [or LDR] ; R1 = 10 [1]
MOV R2, #3 [or LDR] ; R2 = 3
SUB R3, R1, R2 ; R3 = 10 − 3 = 7
STR R3, 200 ; Store 7 at address 200 [1]
(Award marks for correct SUB and STR instructions.)
Q8Describe the role of the assembler. What does it translate, and what two passes does it typically make through the code?[3 marks]
✓ Mark scheme
The assembler translates assembly language (mnemonics) into machine code (binary) [1]; first pass: the assembler scans the code and builds a symbol table, recording the memory address of each label [1]; second pass: the assembler converts each mnemonic to its binary opcode and replaces label names in branch instructions with the actual binary addresses from the symbol table [1].
Topic Quiz
1 of 15
You scored
out of 15
🎯

Mini Test — 1.2.4a Machine Code & Assembly

  • 10 questions · 10 marks · 10 minutes
  • 5 MCQ + 5 short answer
Card 1 of 15
Click to reveal
🎉
Complete!
TermDefinition
← 1.2.3b IDEs 1.2.4 Types of Programming Language Next: 1.2.4b Addressing Modes →