Session - 25 Subroutine Call Return Mechanisms
Session - 25 Subroutine Call Return Mechanisms
23EC1202
DIGITAL DESIGN & COMPUTER ARCHITECTURE
Session – 25
To familiarize students with the basic concept of Subroutine call and Return mechanism statements
INSTRUCTIONAL OBJECTIVES
LEARNING OUTCOMES
• A set of instructions that are used repeatedly in a program can be referred to as a subroutine.
functionalities.
• When a Subroutine is required it can be called many times during the execution of a particular
program.
routine, a method, or a subprogram. The generic term callable unit is sometimes used.
Subroutine call
• In the provided scenario, following the decoding of the 3-byte CALL 4000H instruction, the
program counter naturally advances to the next instruction's address at 2003H. However, the
execution needs to transfer the code flow to memory location 4000H.
• To redirect the program flow to 4000H, the program counter must be updated with the address of
the subroutine instructions. Therefore, the current value of the program counter needs to be
stored elsewhere.
• So, the program counter's content (2003H) must be pushed onto the top of the stack.
• Assuming the stack pointer initially points to FFFFH, during the execution of the CALL instruction,
the stack pointer decrements from FFFFH to FFFEH, pushing the address 2003H onto the stack.
Call & Return instruction
• Since the main memory is byte-addressable with a word length of 16 bits, the whole address (2003H) is not
pushed onto the stack at once.
• Initially, the higher byte of the address (20) is pushed onto the stack. Subsequently, as the stack pointer
decrements to FFFD, the lower byte (03) is again pushed onto the stack.
• After clearing the program counter, it is ready to load the address of the next instructions in the subroutine
starting from 4000H.
• Upon completion of executing the subroutine instructions from 4000H onwards, the code flow needs to
return to the main program.
• The RET instruction at the end of the subroutine transfers the code flow from the subroutine to the main
program. But from which memory location's instructions, does the code flow resume in the main program?
Call & Return instruction
• After the RET instruction, the content of the stack pointer will be popped. Specifically, the first two bytes of
the stack pointer (03 & 20) will be loaded into the program counter's lower byte and higher byte,
respectively. Additionally, the stack pointer's address is incremented by two, returning to its original address
of FFFFH.
• Now, the program counter is directed to the memory address location of 2003H, where the main program's
code flow will resume.
• In summary, when the CALL instruction is encountered, it entails pushing the value of the Program Counter
to the top of the stack. Subsequently, it involves jumping to the address specified in the CALL instruction to
execute the subroutine from there. Upon returning from the subroutine, the content of the stack pointer is
popped, and the address is loaded into the program counter to resume the main program's code flow.
SELF-ASSESSMENT QUESTIONS
1. What is a subroutine in computer programming?
2. When a subroutine is called, the address of the instruction following the CALL instructions is
stored in/on the ________________________________
A. Stack
B. Accumulator
C. Stack Pointer
D. Program Counter
REFERENCES FOR FURTHER LEARNING OF THE SESSION
Reference Books:
1. Computer Organization by Carl Hamacher, Zvonko Vranesic and Saftwat Zaky.
2. Computer System Architecture by M. Morris Mano
3. Computer Organization and Architecture by William Stallings