0% found this document useful (0 votes)
48 views5 pages

Subroutine

A subroutine is a set of instructions that can be reused within a program, allowing for efficient memory usage and modularity. While subroutines offer advantages like code reuse and encapsulation, they also present challenges such as overhead and potential side effects. Subroutine nesting and stack memory are key concepts in managing subroutine calls, but they can lead to issues like stack overflow and security vulnerabilities.

Uploaded by

neethuj.aaps
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)
48 views5 pages

Subroutine

A subroutine is a set of instructions that can be reused within a program, allowing for efficient memory usage and modularity. While subroutines offer advantages like code reuse and encapsulation, they also present challenges such as overhead and potential side effects. Subroutine nesting and stack memory are key concepts in managing subroutine calls, but they can lead to issues like stack overflow and security vulnerabilities.

Uploaded by

neethuj.aaps
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/ 5

A set of instructions used repeatedly in a program can be

referred to as a Subroutine. Only one copy of this Instruction is


stored in the memory. When a Subroutine is required, it can be
called many times during the Execution of a particular program.
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 PC content must be Saved by the call Subroutine
Instruction to make a correct return to the calling program.

Process of a subroutine in a program

The subroutine linkage method is a way in which computers


call and return the Subroutine. The simplest way of Subroutine
linkage is saving the return address in a specific location, such
as a register which can be called a link register called
Subroutine.

Advantages of Subroutines

• Code reuse: Subroutines can be reused in multiple


parts of a program, which can save time and reduce the
amount of code that needs to be written.
• Modularity: Subroutines help to break complex
programs into smaller, more manageable parts, making
them easier to understand, maintain, and modify.
• Encapsulation: Subroutines provide a way to
encapsulate functionality, hiding the implementation
details from other parts of the program.

Disadvantages of Subroutines

• Overhead: Calling a subroutine can incur some


overhead, such as the time and memory required to
push and pop data on the stack.
• Complexity: Subroutine nesting can make programs

more complex and difficult to understand, particularly


if the nesting is deep or the control flow is complicated.
• Side Effects: Subroutines can have unintended side

effects, such as modifying global variables or changing


the state of the program, which can make debugging
and testing more difficult.
What is Subroutine Nesting?
Subroutine nesting is a common Programming practice In
which one Subroutine calls another Subroutine.
A Subroutine calling another subroutine

From the above figure, assume that when Subroutine 1 calls


Subroutine 2 the return address of Subroutine 2 should be
saved somewhere. So if the link register stores the return
address of Subroutine 1 this will be (destroyed/overwritten) by
the return address of Subroutine 2. As the last Subroutine
called is the first one to be returned ( Last in first out format).
So stack data structure is the most efficient way to store the
return addresses of the Subroutines.

The Return address of the subroutine is stored in stack memory

What is Stack Memory?


A Stack is a basic data structure that can be implemented
anywhere in the memory. It can be used to store variables that
may be required afterwards in the program Execution. In a
stack, the first data put will be the last to get out of a stack. So
the last data added will be the first one to come out of the
stack (last in first out).

Stack memory having data A, B & C

So from the diagram above first, A is added then B & C. While


removing the first C is Removed then B & A.

Advantages of subroutine nesting and Stack Memory

• Flexibility: Subroutine nesting allows for the creation


of complex programs with many levels of abstraction,
making it easier to organize code and reuse
functionality.
• Efficient use of memory: Stack memory is used to
allocate and deallocate local variables, allowing for
efficient use of memory resources.
• Error handling: Stack Memory can be used to keep
track of the state of the program, allowing for recovery
from errors and exceptions.
Disadvantages of Subroutine Nesting and Stack Memory

• Stack overflow: If too many subroutine calls are


nested or if the local variables are too large, the stack
memory can overflow, causing the program to crash.
• Security vulnerabilities: Stack-based buffer overflows
can be exploited by attackers to execute malicious code
or crash the program.
• Performance: The use of stack memory can impact
program performance, particularly if the program
requires a large amount of memory or if the stack needs
to be frequently accessed.

You might also like