Subroutines: Reusing Code
A subroutine is a block of code designed to perform a specific task that can be called from anywhere in your program. This is how you avoid writing the same code repeatedly.
The Two Commands:
Command | Action | Analogy |
---|---|---|
CALL NNNN |
Jumps to the address NNNN (the subroutine). | Go to another room to perform a task. |
RET |
Jumps back to the instruction immediately following the CALL . |
Return to where you were before the task. |
The Stack: The LIFO Scratchpad
The Stack is an area of memory managed by the Stack Pointer (SP). When you call a subroutine, the Z80 automatically uses the stack to save the return address.
We use the stack with PUSH
and POP
instructions to save the contents of our registers before a subroutine call, preventing the subroutine from overwriting our main program’s data.
The LIFO Rule: Stack operations are LIFO (Last-In, First-Out). If you PUSH something first, you must POP it last.
Command | Action |
---|---|
PUSH BC |
Saves the 16-bit contents of the BC register pair onto the stack. |
POP BC |
Restores the 16-bit contents of the BC register pair from the stack. |
A Complete Subroutine Example
In this example, we save the HL
pair before calling a routine, and restore it afterward.
MAIN_PROGRAM:
LD DE, 5000H
PUSH DE ; #1: Save DE so the subroutine can't destroy it
CALL MY_ROUTINE ; #2: Go perform the task
POP DE ; #3: Restore DE (It still holds 5000H)
RET
MY_ROUTINE:
; This routine is safe because it only works with A and B,
; but if it used DE, PUSH/POP would be mandatory.
LD A, 1
LD B, 2
ADD A, B
RET ; Return to the instruction after the CALL