SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
CAIE 9618 · Paper 1 · Topic 1.4.1

Assembly Language
Programming

Loops · Conditional Branching · Subroutines · Stack Operations

CSZone Cambridge International AS & A Level Computer Science 9618
Loops in Assembly

Counting Down with a Counter

CAIE assembly loops typically use a counter variable and a conditional jump. The standard pattern: load counter → decrement → compare → jump back if not zero.
; Add 1 to counter 5 times     LDM #5     STO count     LDM #0     STO result loop:     LDD result     INC ACC     STO result     LDD count     DEC ACC     STO count     CMP #0     JPN loop     END
PATTERN BREAKDOWN
1. Initialise counter (5) and result (0)
2. Increment result
3. Decrement counter
4. CMP #0 — if count ≠ 0, jump back
5. Exit when count reaches 0
Final result = 5
Conditional Branching

Making Decisions in Assembly

; Check if value > 10     LDD value     CMP #10     JPE equal     JPN notEqual equal:     LDM #1     STO flag     JMP done notEqual:     LDM #0     STO flag done:     END
BRANCH INSTRUCTIONS
JMP label — always jump
JPE label — jump if ACC = 0 after CMP
JPN label — jump if ACC ≠ 0 after CMP

CMP #n: compares ACC – n
If ACC = n → result = 0 → JPE fires
If ACC ≠ n → result ≠ 0 → JPN fires
Subroutines & The Stack

Reusable Code Blocks

A subroutine is a named block of reusable code. CALL jumps to it, RET returns. The CPU uses the stack to remember where to return to.
; Main program     CALL double     STO result     END   double: ; subroutine     LDD num     ADD num     RET
HOW THE STACK WORKS
CALL: pushes current PC (return address) onto stack. Jumps to subroutine.
RET: pops return address from stack. Loads it into PC. Execution resumes after CALL.
PUSH / POP
PUSH reg: saves register to top of stack
POP reg: retrieves from top of stack
Stack pointer (SP) tracks top of stack
The Stack in Detail

LIFO — Last In, First Out

The stack is a LIFO data structure in memory, with a Stack Pointer (SP) register tracking the top. Each PUSH decrements SP and stores a value; each POP retrieves and increments SP.
PUSH ACC: SP ← SP – 1; Mem[SP] ← ACC
POP ACC: ACC ← Mem[SP]; SP ← SP + 1
CALL: pushes PC onto stack before jumping
RET: pops return address and places in PC
WHY STACK MATTERS FOR SUBROUTINES
Subroutines can call other subroutines (nesting). Stack remembers the chain of return addresses in the correct order. Without stack, nested calls would be impossible.
Exam Practice

Cambridge-style questions

Question 1
Explain the role of the stack when a subroutine is called using the CALL instruction, and what happens when RET is executed.
4 marks
1 mark
CALL: the current value of the Program Counter (PC) — the return address — is pushed onto the stack
1 mark
The PC is then loaded with the address of the subroutine, and execution continues from there
1 mark
RET: the return address is popped from the top of the stack
1 mark
This address is placed into the PC, so execution resumes at the instruction immediately after the original CALL
Common Mistakes

Don't lose easy marks

1
Saying CALL stores the current instruction — CALL stores the return address, which is the address of the instruction after the CALL (i.e., where to return to). This is the PC's value at the time of the call.
2
Confusing JMP and CALL — JMP unconditionally jumps but does NOT save a return address. CALL jumps AND saves the return address to the stack. Use CALL for subroutines, JMP for loops/branches.
3
Saying the stack uses FIFO — the stack is LIFO (Last In, First Out). This ensures the most recent return address is retrieved first, enabling correct handling of nested subroutine calls.
Topic Summary — 1.4.1

What You Need to Know

LOOPS
Use counter variable + DEC + CMP #0
JPN to loop if counter ≠ 0
Exit when counter = 0 (JPE)
BRANCHING
CMP sets flags without changing ACC
JPE: jump if equal (ACC - n = 0)
JPN: jump if not equal
JMP: unconditional jump
SUBROUTINES
CALL: pushes PC (return address) to stack
Jumps to subroutine label
RET: pops return address, loads into PC
Enables reusable, modular code
STACK
LIFO — Last In First Out
SP (Stack Pointer) tracks top
PUSH: SP--, Mem[SP] = value
POP: value = Mem[SP], SP++
CSZone

Next Video

1.4.2
Addressing Modes
Immediate · Direct · Indirect · Indexed · Symbolic
Head to CSZone.co.uk for the complete worksheet, quiz, and interactive tools