Function Call & Return
Call a function that doubles a value. See how CALL saves the return address and RET jumps back.
Function calls rely on the stack to remember where to return. CALL pushes the current PC onto the stack (so the CPU remembers the caller's location) and then jumps to the function's address. RET pops that saved address off the stack and loads it into PC, resuming execution right after the original CALL. This is how every function call and return works at the hardware level.
Simulator
Walkthrough Guide
- 1
Set up the argument
Do this: Step through MOV R0, #7
Watch for: R0 = 7
We load the argument into R0. In this simple calling convention, the function will find its input in R0 and leave its output there too.
- 2
Call the function
Do this: Step through CALL double
Watch for: PC jumps to the 'double' label. SP decrements and the return address appears on the stack
CALL does two things: it pushes the address of the next instruction (the one after CALL) onto the stack, then sets PC to the function's address. The pushed address is the 'return address' — where execution should resume after the function finishes.
- 3
Execute the function body
Do this: Step through ADD R0, R0
Watch for: R0 changes from 7 to 14
ADD R0, R0 doubles R0 by adding it to itself. The function has done its work — R0 now holds the result.
- 4
Return to the caller
Do this: Step through RET
Watch for: PC jumps back to the instruction after CALL. SP increments as the return address is popped
RET pops the return address from the stack and loads it into PC. Execution resumes at the HALT instruction that follows the original CALL. The stack is restored to its state before the call.
Objectives
- •See CALL push the return address onto the stack
- •Watch PC jump to the 'double' label
- •See R0 change from 7 to 14 after ADD
- •Watch RET pop the return address and jump back
Hints
- CALL is like PUSH PC + JUMP. RET is like POP PC.