[go: up one dir, main page]

0% found this document useful (0 votes)
325 views6 pages

11-Subroutine Call and Return

This document discusses subroutine calls and returns. It explains that a subroutine is a self-contained sequence of instructions that performs a computational task. When a subroutine is called, the program counter (PC) is pushed onto the stack and set to the first address of the subroutine. The subroutine executes until a return instruction, which pops the top of the stack back into the PC to continue execution after the subroutine call. The stack is identified as the best way to store return addresses, as it allows sequential and recursive subroutine calls by maintaining multiple return addresses. Micro-operations for a call involve decrementing the stack pointer and pushing the PC, while a return pops the stack into the PC and increments the stack pointer

Uploaded by

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

11-Subroutine Call and Return

This document discusses subroutine calls and returns. It explains that a subroutine is a self-contained sequence of instructions that performs a computational task. When a subroutine is called, the program counter (PC) is pushed onto the stack and set to the first address of the subroutine. The subroutine executes until a return instruction, which pops the top of the stack back into the PC to continue execution after the subroutine call. The stack is identified as the best way to store return addresses, as it allows sequential and recursive subroutine calls by maintaining multiple return addresses. Micro-operations for a call involve decrementing the stack pointer and pushing the PC, while a return pops the stack into the PC and increments the stack pointer

Uploaded by

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

SUBROUTINE CALL & RETURN

SUBROUTINE
 Subroutine is a self-contained sequence of instructions
that performs a given computational task.

 Call Subroutine.

 When called,
 Push [PC] to TOS
 [PC]  1st address of subroutine
 Execute SUB
 Finally execute RET instruction
 Pop TOS to PC
 Continue with the instruction which is next to SUB call.
LOCATIONS TO STORE THE RETURN
ADDRESS
 First memory location of the subroutine
 Fixed location in memory

 Processor registers

 Memory stack – best option


 Adv: In the case of sequential calls to subroutines. So, the top
of the stack always has the return address of the subroutine
which to be returned first.
MICRO-OPERATIONS
Call:
SP ← SP – 1 // decrement stack pointer
M[SP] ← PC // push content of PC onto the stack
PC ← effective address /* transfer control to the subroutine */

Return:
PC ← M[SP] // pop stack and transfer to PC
SP ← SP + 1 // increment stack pointer
 Recursive SUB:- Subroutine that calls itself
 Subroutine nesting:- One subroutine that calls another.

 If only one register or memory location is used to hold


the return address, when subroutine is called recursively,
it destroys the previous return address.
 So, stack is the good solution for this problem.
REFERENCES
Text Book
 William Stallings “Computer Organization and
architecture” Prentice Hall, 7th edition, 2006

You might also like