Machine-code/assembly language operations — AQA A-Level Computer Science
Machine code consists of binary instructions executed directly by the CPU, whereas assembly language uses human-readable mnemonics representing low-level opcodes and operands. Mastering assembly operations, addressing modes, and bitwise manipulation is critical for tracing processor registers and writing efficient algorithms for AQA A-Level Computer Science.
Machine-code/assembly language operations explained
This statement introduces the basic machine-code operations that you must understand and apply, and the specific operations are listed after the colon.
Read the full explanation
Machine-code operations are the low-level instructions a processor executes, typically moving data between registers and memory, performing arithmetic and logic, and controlling the flow of execution through jumps and branches. Assembly language provides a readable mnemonic for each machine-code instruction. To apply these operations, you should be able to read an instruction, identify its operation and operands, and predict its effect on registers and memory. For example, an instruction that adds the contents of two registers changes the destination register's contents, while a jump instruction changes the address of the next instruction to be fetched.
Your focus
- Understand and apply the basic machine-code operations of:
Which command word is this?
Follow the step-by-step execution of code precisely, recording every register change chronologically in a trace table until execution halts.
Machine-code/assembly language operations exam tips
Topic Overview
Machine code represents the fundamental native instructions executed directly by the central processing unit, consisting entirely of binary bit patterns divided into an opcode and one or more operands. Assembly language provides a human-readable mnemonic representation of this machine code, forming a direct one-to-one correspondence that allows low-level access to processor registers and system memory.
Understanding assembly operations is central to AQA A-Level Computer Science because it bridges high-level procedural abstraction with physical computer architecture. It underpins how compilers generate executable code, how addressing modes determine operand retrieval, and how bit manipulation and status registers control hardware devices.
Key Concepts
- →Instruction Structure: Every machine instruction consists of an Operation Code (Opcode), which tells the control unit what operation to execute, and an Operand, which holds data or memory references.
- →Addressing Modes: Immediate addressing ('#value') uses the operand directly as the datum, direct addressing uses the operand as the memory address of the datum, while register addressing uses processor registers.
- →Status Register and Branching: The Compare instruction ('CMP') performs subtraction to update status flags (Zero, Negative, Carry), which conditional branch instructions ('BEQ', 'BNE', 'BGT', 'BLT') evaluate to direct program flow.
- →Bitwise Logical Operations: 'AND', 'ORR', 'EOR', and 'MVN' operate on corresponding individual bits in registers, enabling masking techniques to isolate, set, or invert specific bits.
- →Shift Operations: Logical Shift Left ('LSL') multiplies unsigned integers by powers of two by shifting bits left and inserting zeros, while Logical Shift Right ('LSR') divides by powers of two.
Marking Points
- Identifies the operation type of an instruction from its mnemonic or opcode.
- Identifies the operands and the storage locations they refer to.
- Predicts the effect of an instruction on registers or memory.
- Traces the sequence of execution through a short program correctly.
Examiner Tips
- 💡Write down the effect of each instruction on registers or memory as you trace the program.
- 💡Check the destination operand to see which storage location is changed.
- 💡Trace execution in order, noting any jump that changes the next instruction.
- 💡Always draw a trace table containing a column for each register and the Status Register condition flags whenever solving loop-based assembly questions.
- 💡When asked to explain the difference between addressing modes, always provide an annotated example showing how the operand field is decoded by the CPU for each mode.
- 💡Ensure you use valid AQA-specific mnemonics from the specification (e.g. 'LDR', 'STR', 'ADD', 'SUB', 'MOV', 'CMP', 'B', 'AND', 'ORR', 'EOR', 'LSL', 'LSR') rather than generic x86 or standard ARM syntax.
Common Mistakes
- Confuses the operation with its operands; the mnemonic states what is done, the operands state what it is done to.
- Assumes an instruction changes memory when it only changes a register; check the destination carefully.
- Ignores the effect of a jump or branch on the order of execution.
- Believing assembly instructions can access two arbitrary main memory locations simultaneously; in load-store architectures, memory values must first be loaded into registers ('LDR') before arithmetic processing, and then stored back ('STR').
- Confusing Logical Shift Right ('LSR') with Arithmetic Shift Right ('ASR'); LSR always inserts a zero into the most significant bit, whereas ASR preserves the sign bit to maintain correct two's complement negative values.
- Assuming that conditional branch instructions execute the comparison themselves; a conditional branch like 'BNE' only reads the condition flags already stored in the Status Register by a preceding instruction like 'CMP' or 'SUB'.
Revision Plan
- 1Day 1-3: Memorise the standard AQA instruction set mnemonics, distinguishing arithmetic, logical, data transfer, and branch instructions.
- 2Day 4-6: Practice manual register tracing using past exam trace tables, paying special attention to loop counter decrements and conditional flags.
- 3Day 7-9: Master bit masking and shifting exercises; write code snippets to extract, set, and toggle individual bits using AND, ORR, and EOR.
- 4Day 10-12: Write full assembly programs from high-level pseudocode algorithms, incorporating selection constructs ('IF-THEN-ELSE') and iteration ('WHILE', 'FOR').
- 5Day 13-14: Complete timed past paper questions assessing addressing modes, assembly theory, and machine-code binary instruction layouts.
Exam Question Types
- 📋Register Trace Tables: Candidates must track values across registers (R0, R1, R2, etc.) through sequential and branching instructions until a HALT instruction is reached.
- 📋Code Conversion / Writing: Candidates must write short snippets of assembly code from a given specification or convert a high-level pseudocode loop into assembly.
- 📋Bitwise Masking Problems: Candidates must supply the correct mask and bitwise operator (AND/ORR/EOR) to isolate or alter specific bit flags.
- 📋Conceptual Questions on Addressing Modes: Candidates are asked to compare immediate, direct, indirect, and indexed addressing modes using diagrams or written definitions.
Command Word Expectations (AQA)
Follow the step-by-step execution of code precisely, recording every register change chronologically in a trace table until execution halts.
Provide a detailed account of how a process works (e.g. how an addressing mode resolves an operand) with clear cause-and-effect reasoning.
Construct syntactically accurate assembly code using the approved AQA instruction set to achieve the precise algorithmic outcome specified.
How Students Lose Marks (Examiner Pitfalls)
Step-by-Step Worked Solutions
Question: Trace the following AQA assembly language program to find the final value stored in register R0: MOV R0, #0 MOV R1, #3 loop: ADD R0, R0, R1 SUB R1, R1, #1 CMP R1, #0 BNE loop HALT
- 1.Step 1: Initialise registers from the initial setup instructions. R0 is loaded with immediate value 0 (R0 = 0), and R1 is loaded with immediate value 3 (R1 = 3).
- 2.Step 2: Trace Iteration 1. 'ADD R0, R0, R1' results in R0 = 0 + 3 = 3. 'SUB R1, R1, #1' decrements R1 to 2. 'CMP R1, #0' checks if R1 is 0. Since R1 is 2, the Zero flag is not set, so 'BNE loop' branches back to 'loop'.
- 3.Step 3: Trace Iteration 2. 'ADD R0, R0, R1' results in R0 = 3 + 2 = 5. 'SUB R1, R1, #1' decrements R1 to 1. 'CMP R1, #0' tests R1. Since R1 is 1, 'BNE loop' branches back to 'loop'.
- 4.Step 4: Trace Iteration 3. 'ADD R0, R0, R1' results in R0 = 5 + 1 = 6. 'SUB R1, R1, #1' decrements R1 to 0. 'CMP R1, #0' tests R1. Since R1 equals 0, the Zero flag is set (Z=1), so the 'BNE loop' branch condition fails.
- 5.Step 5: Execution falls through to 'HALT'. Read the final stored value in register R0.
Question: Write a sequence of AQA assembly instructions to check whether the least significant bit (bit 0) of the value in R1 is 1. If it is 1, branch to the label 'odd'; otherwise, continue to 'even'.
- 1.Step 1: Select the appropriate bitwise logic operation. A bitwise AND operation with a mask isolates specific bits without affecting other registers.
- 2.Step 2: Determine the bitmask. To test bit 0, use the binary mask 00000001 (decimal 1).
- 3.Step 3: Apply the bitwise operation using an immediate operand: 'AND R2, R1, #1'. This leaves R2 as 1 if bit 0 was set, or 0 if bit 0 was clear.
- 4.Step 4: Compare the result to zero or one: 'CMP R2, #1'.
- 5.Step 5: Apply the branch instruction: 'BEQ odd', followed immediately by an unconditional branch or fallthrough to 'even'.