You are on page 1of 25

CSE205 - Computer Architecture and

Organization (CAO)

SUBROUTINE CALL AND


RETURN FUNCTIONS

Jimmy Mathew
Assistant Professor Senior,
School of Computing Science & Engineering (SCOPE),
VIT University, India
Email: jimmym@vit.ac.in / Office: SJT-313-A08
Reference textbooks
• [1] W. Stallings, “Computer organization and architecture”, Prentice-Hall, 8th
edition, 2009.

• [2] M. M. Mano, “Computer System Architecture”, Prentice-Hall, 3rd Edition,


1992.

• [3] J. P. Hayes, “Computer system architecture”, McGraw Hill, 3rd Edition,


2002.

• [4] John L. Hennessy and David A. Patterson, “Quantitative Approach –


Computer Architecture”, Morgan Kaufmann Publishers, 5th edition, 2011.

CAO Chapter 1.5 JMW VIT-U 2


SUBROUTINE CALL AND RETURN

CAO Chapter 1.5 JMW VIT-U 3


Subroutine Call and Return
• Subroutines /
Procedures /
Functions

• Nested procedures

CAO Chapter 1.5 JMW VIT-U 4


Subroutine Call and Return
• Features:

1. A procedure can be called from more than one location.

2. A procedure call can appear in a procedure. This allows the nesting of


procedures to an arbitrary depth.

3. Each procedure call is matched by a return in the called program.

• There are three common places for storing the return address:

– Register

– Start of called procedure

– Top of stack

CAO Chapter 1.5 JMW VIT-U 5


Subroutine Call and Return
• Stack:

CAO Chapter 1.5 JMW VIT-U 6


Subroutine Call and Return

CAO Chapter 1.5 JMW VIT-U 7


Subroutine
Subroutine is a self-contained sequence of instructions that
performs a given computational task.
It may be called many times at various points in the main
program
When called, branches to 1st line of subroutine and at the end,
returned to main program.
Different names to the instruction that transfers program
control to a subroutine
Call subroutine
Jump to subroutine
Branch to subroutine
Branch and save address

CAO Chapter 1.5 JMW VIT-U 8


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.

CAO Chapter 1.5 JMW VIT-U 9


Control transfer from called to caller
Subroutine instruction – Opcode + starting address
of the subroutine
Execution:
PC content (return address) is stored in a temporary location
Control is transferred to the subroutine
when return
Transfers the return address from the temporary location to
the PC.
Control is transferred back to the called routine

CAO Chapter 1.5 JMW VIT-U 10


Recursive subroutines
Subroutine that calls itself
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

CAO Chapter 1.5 JMW VIT-U 11


Interrupts
Mechanism by which other modules (e.g. I/O) may interrupt
normal sequence of processing
External interrupt
e.g. Interrupts from IO devices –IO device finished transfer of data
Power failure
Timeout interrupt
Internal interrupt
Illegal use of instruction or data
Attempt ot divide by zero,stack overflow,protection violation
Software interrupt
• Use to initiate an interrupt procedure at anypoint in the program

CAO Chapter 1.5 JMW VIT-U 12


Instruction Cycle State Diagram

CAO Chapter 1.5 JMW VIT-U 13


Program Flow Control

CAO Chapter 1.5 JMW VIT-U 14


Transfer of Control via Interrupts

CAO Chapter 1.5 JMW VIT-U 15


Program Timing Short I/O Wait

CAO Chapter 1.5 JMW VIT-U 16


Program Timing Long I/O Wait

CAO Chapter 1.5 JMW VIT-U 17


Instruction Cycle with Interrupts

CAO Chapter 1.5 JMW VIT-U 18


Interrupt Cycle
Added to instruction cycle
Processor checks for interrupt
Indicated by an interrupt signal
If no interrupt, fetch next instruction
If interrupt pending:
Suspend execution of current program
Save context
Set PC to start address of interrupt handler routine
Process interrupt
Restore context and continue interrupted program

CAO Chapter 1.5 JMW VIT-U 19


Instruction Cycle (with Interrupts) - State
Diagram

CAO Chapter 1.5 JMW VIT-U 20


Multiple Interrupts
Disable interrupts
Processor will ignore further interrupts whilst processing one
interrupt
Interrupts remain pending and are checked after first interrupt
has been processed
Interrupts handled in sequence as they occur
Define priorities
Low priority interrupts can be interrupted by higher priority
interrupts
When higher priority interrupt has been processed, processor
returns to previous interrupt

CAO Chapter 1.5 JMW VIT-U 21


Multiple Interrupts - Sequential

CAO Chapter 1.5 JMW VIT-U 22


Multiple Interrupts – Nested

CAO Chapter 1.5 JMW VIT-U 23


Time Sequence of Multiple Interrupts

CAO Chapter 1.5 JMW VIT-U 24


References
Text Book
 William Stallings “Computer Organization and
architecture” Prentice Hall, 7th edition, 2006

CAO Chapter 1.5 JMW VIT-U 25

You might also like