Function Calls and the CALL Instruction
What you'll learn: How the CALL and RET instructions work together to implement reusable subroutines while automatically managing return addresses.
The Problem: How Does the CPU Know Where to Return?
When your program jumps to a subroutine (a reusable block of code), it must eventually return to the exact instruction that comes after the call. But once you've jumped away using an unconditional jump, how does the CPU remember where to come back?
The CALL Instruction
The CALL instruction solves this elegantly:
- Saves the return address — It automatically pushes the address of the next instruction (the one after
CALL) onto the stack - Jumps to the subroutine — It transfers control to the target label, just like an unconditional jump
CALL myFunction ; Pushes return address, jumps to myFunction
ADD AX, 1 ; <-- This address is saved on the stack
The RET Instruction
When the subroutine finishes, RET reverses the process:
- Pops the return address from the stack
- Jumps back to that address, resuming execution right after the original
CALL
Think of It Like a Bookmark
Imagine reading a book and temporarily flipping to an appendix. You place a bookmark (return address) at your current page, read the appendix (subroutine), then use the bookmark to return exactly where you left off. The stack holds these "bookmarks" for nested function calls.
Key Takeaway: The CALL instruction pushes the return address onto the stack and jumps to a subroutine, while RET pops that address and returns control—enabling reusable, modular code with automatic return-path management.