In assembly language, a subroutine (also called a procedure or function) is
a reusable block of code that performs a specific task.
It allows for code modularity, reusability, and easier management of
large programs by dividing tasks into smaller, manageable parts.
Key Concepts of Subroutines
1. Definition and Invocation:
o A subroutine is defined in a specific location in memory with a label
(like a function name in higher-level languages).
o It is invoked using a jump or call instruction that transfers control
from the main program to the subroutine.
2. CALL and RET Instructions:
o CALL: The instruction used to call a subroutine. It saves the return
address (address of the instruction following the call) on the stack,
then jumps to the subroutine.
Stack: A stack is a data structure that stores data in such a way that the last
piece of data stored, is the first one retrieved.
o RET: The instruction used to return from a subroutine. It pops the
saved return address from the stack and jumps back to it.
3. Stack for Return Address and Parameters:
o When a subroutine is called, the return address is often pushed onto
the stack, so the program can return to the correct point after
execution.