0% found this document useful (0 votes)
112 views15 pages

Session - 25 Subroutine Call Return Mechanisms

Uploaded by

Arnold
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views15 pages

Session - 25 Subroutine Call Return Mechanisms

Uploaded by

Arnold
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Department of ECE

23EC1202
DIGITAL DESIGN & COMPUTER ARCHITECTURE
Session – 25

Subroutine Call & Return Mechanisms


AIM OF THE SESSION

To familiarize students with the basic concept of Subroutine call and Return mechanism statements

INSTRUCTIONAL OBJECTIVES

This Session is designed to:


1. Demonstrate the subroutine call and return statements
2. Describe the subroutine call and returns statements with examples and applications
3. List out the applications of subroutine and returns statements with live examples
4. Describe and explain the subroutine call and return statements

LEARNING OUTCOMES

At the end of this session, you should be able to:


1. Define subroutine call and returns statements
2. Describe the applications of procedures with returns statements
3. Summarize the concepts of subroutine call and return statements
Subroutine

• A set of instructions that are used repeatedly in a program can be referred to as a subroutine.

• Subroutines are called by a main program or other subroutines to accomplish particular

functionalities.

• When a Subroutine is required it can be called many times during the execution of a particular

program.

• In different programming languages, a subroutine may be called a procedure, a function, a

routine, a method, or a subprogram. The generic term callable unit is sometimes used.
Subroutine call

• A call subroutine instruction calls the subroutine.


• Care Should be taken while returning a subroutine as a subroutine can be called from a different
place from the memory.
• The content of the PC must be saved by the call subroutine Instruction to make a correct return to
the calling program.
Subroutine call (Cont.)
Nested Subroutine

• Stack data structure is the most


efficient way to store the return
addresses of the subroutines.
Call instruction

• The CALL instruction interrupts the flow of a


program by passing control to an internal or
external subroutine.
• An internal subroutine is part of the calling
program. An external subroutine is another
program.
Example: CALL 4000H (3 byte instruction)
• When CALL instruction is fetched, the processor
knows that the next two memory location contains
16-bit subroutine address in the memory.
Return instruction

• The RETURN instruction returns control from a


subroutine back to the calling program and
optionally returns a value.
• It is a 1 byte instruction.
Example: RET
• Retrieve the return address from the top of the
stack.
• Load the program counter with the return
address.
Call & Return instruction
Call & Return instruction

• 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?

A. A unique type of variable that stores data


B. A set of instructions that perform a particular task, used to structure programs
C. A data structure used to organize data
D. An error or bug in a program

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

Sites and Web links:


1. https://www.geeksforgeeks.org/subroutine-subroutine-nesting-and-stack-memory/
2. https://www.tutorialspoint.com/what-are-subroutines
THANK YOU

Team – Digital Design & Computer Architecture

You might also like