Types of Subroutines
Multiple Call Subroutines
- The figure shows the basic concept of multiple CALL subroutines.
- This is a subroutine called from many locations in the main program.
- For example, the DELAY routine is a multiple call subroutine.
- These types of routines are easy to trace and need minimal stack space.
- Initially, stack pointer content is XX55H so that the return address can be stored on the stack.
- When the CALL instruction starts to execute, the subroutine is called from the 8050H memory location.
- The return address is stored on the stack and the stack pointer is decremented by two locations to XX53H.
- When the subroutine is called by another subroutine, it is called a nested subroutine.
- When a subroutine calls another subroutine, all return addresses are stored on the stack.
- Therefore, only the number of available stack locations limits the extent of nesting.
- The structure of a nested subroutine is depicted in fig.
- The main program calls Subroutine I from location 8050H.
- The address of the next instruction, 8053H, is placed on the stack, and the program is transferred to Subroutine I at 8150H.
- During the execution of Subroutine I, Subroutine I calls Subroutine II from location 8190H.
- The address 8193H is placed on the stack and the program is transferred to Subroutine II.
- The sequence of execution and returns to the main program as shown in fig.
Multiple Ending Subroutines
- When a subroutine can be terminated at more than one place, it is called multiple ending subroutines, as illustrated in fig.
- The subroutine has conditional such as RZ – Return On Zero and RC – Return On Carry.
- This subroutine has an unconditional return – RET.
- While the Z flag is set, the subroutine returns from location 8050H, and if the CY flag is set, it returns from location 8090H.
- If neither flag is set, the subroutine returns from location 80A0H.