Professional Documents
Culture Documents
Subroutines
Program Stack
Exceptions
Topics
Subroutines
Jump to Subroutine (JSR)
Return from Subroutine (RTS)
The Processor Stack
Role of the Stack in Subroutine Processing
Stack Frame
Exceptions
Interrupts
Traps
Faults
Aborts
Objectives:
Describe how the order of execution of statements in a program is
affected by instructions that cause a change in the value of the
processor’s program counter
Describe how a subroutine
Describe how the stack is used to provide communication between a
subroutine and the calling environment
Understand how exceptions are used to coordinate the operation of the
system
Build a background for later discussion of
Processor I/O
Operating Systems
Control Flow
As the processor of a computer executes instructions, the Program Counter
is updated to “point” to the next instruction that is to be fetched and
executed
As each instruction is fetched the Program Counter is incremented to the
address of the immediately following instruction:
[PC] ← [PC] + 1 /* 8 bit instruction */
[PC] ← [PC] + 4 /* 32 bit instruction */
Address of each
8-bit instruction
Subroutines
Another example of abrupt change In control flow is in the execution of
subroutines
A subroutine is a group of instructions that performs some task
Typically a task that is needed at several points in the execution of a program
Concept that is used in HLLs:
Functions in C
Methods in Java
similar structures in other languages
Subroutines
A subroutine can be called from several places in a program
This is accomplished by setting the program to the address of the first
instruction in the group of instructions that make up the subroutine.
A subroutine is like a branch except that:
Once the subroutine is completed, control must be returned to the statement
following the statement that called the subroutine
A regular branch will not do in this case as the point to which control is not
always the same
Also, a subroutine will typically need inputs (referred to as arguments, or
parameters)
e.g. int x = square(4);
Subroutines
Most ISAs provide an instruction that is used to cause a branch to the first
instruction in a subroutine
This transfer of control is termed a subroutine call
For example, most ISAs provide an instruction that is named JSR (or something
similar) meaning Jump to Sub-Routine
SWAP1: BEQ L2
JSR SWAP1
L2: ……
RTS
Subroutines
The task of establishing communication with a subroutine is called subroutine
linkage.
The methods that are used to accomplish subroutine linkage will vary across
different ISAs
In some processors a special register, a link register, is used to store the
return address
Cannot be used with nested subroutine calls as the return address in register will be
overwritten by the nested call
Can also use an area of memory to store return address and parameters
One approach is to store return address in first instruction of sub-routine
Processor inserts address when subroutine is called
Also not good for nested sub-routine calls (especially recursive calls)
The Program(Runtime) Stack
Most processors use a stack to provide Address: 0
mechanisms for subroutine call and
return. Program
Instructions
The Stack is an area of a program’s
address space that grows and shrinks as
the program runs.
Variable
Usually high addresses of program space and
Entries are added to the stack when a Constants
subroutine is called
Stack Program Stack
When the subroutine returns the entries
grows each Is empty when
that were saved on the stack are
removed time a the program
subroutine starts
is called up
The Program Stack
The set of entries that are pushed onto the stack for an invocation of a
subroutine is called a stack frame.
Consider the following example:
MAIN: JSR SWAP1
.
.
SWaP1: ….
JSR SWAP2
….
RTS
SWAP2: …..
…..
RTS
The Program Stack
Control flow is described as shown below:
MAIN: JSR SWAP1
.
.
SWAP1: ….
JSR SWAP2
….
RTS
SWAP2: …..
…..
RTS
The Program Stack
Stack
frame for
call to
SWAP2
Stack Stack Stack
frame for frame for frame for
call to call to call to
SWAP1 SWAP1 SWAP1
The use of a stack for subroutine call and return has several advantages
over other approaches:
A subroutine can call another subroutine (nesting). The calling subroutine saves
its local variables, return addresses, and return address on the stack to be
restored when the called subroutine returns.
A subroutine can call itself (recursion). Each call saves its local variables on the
stack to be restored when the call returns.
Stack Overflow
A stack overflow occurs when the stack area of a program grows so large
that it meets the area which is used for variables and constants.
This error usually results from:
The stack area being too small
A high number of subroutine calls that are stacked one upon the other.
This would be the case where recursion is used without a base case
Subroutines are being passed a large number of parameters
Other conditions can lead to stack overflow but these are the most common.
Exceptions
*Except in
the case of
Abort.
(1) Program
issues TRAP TRAP #15
(2) Control passes to handler
(3) Trap handler
instruction runs
Next Instruction
(4) Handler
returns to
instruction
following the
TRAP
Faults
Faults result from an error condition that its handler may be able to
correct
Examples include requesting an address in a program that is not loaded
into memory at present
Most modern computer systems do not load all of a program at once
Instead portions of the program are brought into memory as needed
A page fault, or segment fault occurs when a portion of the program that is needed is not
in memory
More on this under the topic of Virtual memory.
Abrupt changes in the control flow of a program will occurs for several
reasons
Instructions that execute branching are the most common reason for changes
in control flow
Subroutine execution also causes branching
Exceptions need to be handled
The program’s stack area is used for subroutine linkage
A stack frame is a set of entries that is created when a subroutine is called
The stack frame will typically contain saved registers, parameters, space for a
returned value, and the return address of the subroutine
An interrupt is an asynchronous exception
Interrupts are primarily used for communicating with devices
Traps, faults and aborts are synchronous exceptions
The state of the processor must be saved before and exception is handled and
restored afterwards.