0% found this document useful (0 votes)
41 views27 pages

Computer System Organization: On Chapter 2: Stacks and Subroutines

This document discusses stacks, subroutines, and their implementation in computer systems. It begins by introducing modular programming and the divide and conquer approach. It then describes stacks as last-in, first-out data structures used for memory organization and subroutine execution. Key concepts like push, pop, and stack pointer register are explained. Subroutines are implemented using call and return instructions that save return addresses on a stack. Parameter passing techniques including registers and stack are presented. Nested subroutines require the stack to manage multiple return addresses. A stack frame stores local data and parameters for a subroutine call.

Uploaded by

ayush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
41 views27 pages

Computer System Organization: On Chapter 2: Stacks and Subroutines

This document discusses stacks, subroutines, and their implementation in computer systems. It begins by introducing modular programming and the divide and conquer approach. It then describes stacks as last-in, first-out data structures used for memory organization and subroutine execution. Key concepts like push, pop, and stack pointer register are explained. Subroutines are implemented using call and return instructions that save return addresses on a stack. Parameter passing techniques including registers and stack are presented. Nested subroutines require the stack to manage multiple return addresses. A stack frame stores local data and parameters for a subroutine call.

Uploaded by

ayush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

Computer System Organization

Lecture
on
Chapter 2: Stacks and Subroutines

<Course Code> 1
Stacks and Queues
Important Data Structures involved in Memory
Organization and Subroutine Execution

9/17/2021 2
Modular Programming

Principle
– Divide and Conquer
Steps
– Divide the problem into sub-problems
– Solve the sub-problems
– Combine the (sub-)solutions

9/17/2021 3
How do you solve the sub-problem?

Divide-and-conquer again!
Steps
1. Divide the problem into sub-problems
2. Repeat step 1 for each sub-problem
until the problem is small enough to have
an atomic solution.
3. Combine the solutions.

9/17/2021 4
Graphical Representation of Divide and Conquer

Task

Subtask1 Subtask2 Subtask3

Subtask1.1 Subtask1.2
9/17/2021 5
Stacks
• Data storage and module (or function/subroutine/procedure)
execution popularly makes use of memory organized as stack.
• A stack is a list of data elements, usually words or bytes,
• With the accessing restriction that elements can be added or
removed at one end of the list only
• This end is called the top of the stack, and the other end is called
the bottom
• last-in-first-out (LIFO) stack: the last data item placed on the stack is
the first one removed when retrieval begins.
• The terms push and pop are used to describe placing a new item on
the stack and removing the top item from the stack, respectively.

6
A Stack in Memory

7
Push and Pop Operations
SP regiter stores the address of top of
the stack

9/17/2021 8
9
More on Stacks
• Stack has been provided with fixed amount of soace in
memory
• It is necessary to avoid pushing elements when it is
FULL
• Also avoid poping elements when it is EMPTY
• So need to check whether it is FULL while PUSH
operation
• And need to check whether it is EMPTY while POP
operation

9/17/2021 10
9/17/2021 11
Queues
• Another important and useful data structure
• Data are stored in and retrieved from a queue on a first-in-first-out
(FIFO) basis.
• queue grows in the direction of increasing addresses in the memory
(generally)
• new data are added at the back (high-address end)
• retrieved from the front (low-address end) of the queue.

12
Important differences between how a stack and a queue

• Stacks: • Queues:
– One end of the stack is – both ends of a queue move
fixed (the bottom), while to higher addresses as
the other end rises and data are added at the back
falls as data are pushed and removed from the front
and popped. – two pointers are needed to
– A single pointer is needed keep track of the two ends
to point to the top of the of the queue
stack at any given time.

9/17/2021 13
Circular Queue
• a queue would continuously move through the memory of a
computer in the direction of higher addresses.
• One way to limit the queue to a fixed region in memory is to use a
circular buffer
• assume that memory addresses from BEGINNING to END are
assigned to the queue.
• The first entry in the queue is entered into location BEGINNING
• successive entries are appended to the queue by entering them at
successively higher addresses.
• By the time the back of the queue reaches END, space will have
been created at the beginning if some items have been removed
from the queue.
• Hence, the back pointer is reset to the value BEGINNING and the
process continues.
• Care must be taken to detect when the region assigned to the data
structure is either completely full or completely empty
9/17/2021 14
Subroutines
• It is often necessary to perform a particular subtask many times on different
data-values
• For example,a subroutine may evaluate the sine function or sort a list of
values into increasing or decreasing order.
• One way is to place a block of instructions that constitute a subroutine at
every place where it is needed in the program
• Another way is to Call the subroutine and return from the subroutine
• The way in which a computer makes it possible to call and return from
subroutines is referred to as its Subroutine Linkage Method
• Subroutine linkage method saves the return address in a specific location,
which may be a register dedicated to this function.
• Such a register is called the Link Register.
• Return instruction returns to the calling program by branching indirectly
through the link register

9/17/2021 15
Call and Return: Subroutines
• The Call instruction is just a special branch instruction
that performs the following operations
– Store the contents of the PC in the Link Register
– Branch to the target address specified by the instruction

• The Return instruction is a special branch instruction that


performs the operation
– Branch to the address contained in the Link Register .

9/17/2021 16
9/17/2021 17
Without and with Subroutine

9/17/2021 18
Using Subroutines
• When using subroutines we need to know the following:
– Where is the NEXT instruction’s address
– How to remember the RETURN address
• Nested Subroutines are commonly used and Link
register is not sufficient to implement nesting.
• Return addresses are generated and used in LIFO order.
• Thus, processor stack is required to be used to maintain
the sequence of return addresses in nested subroutine
calls
• A special register SP (Stack Pointer) is used for pointing
to the top of the processor
• Subroutines are based on Call and Return instructions
and use STACK
9/17/2021 19
LIFO order of
Nested
Subroutine calls
and Use of Stack

9/17/2021 20
Call and Return: Nested Subroutines
• Uses Processor Stack instead of Link Register
• The Call instruction performs the following operations
– Pushes the contents of the PC (thst is return address) to the
top of the stack
– SP points to this top of the stack
– Branch to the target address specified by the instruction

• The Return instruction performs the following operation


– Pops the return address from the processor stack into the PC
– Branch to that address

9/17/2021 21
Parameter Passing

• A subroutine can have


– parameters that control its operation
– local variables for computation.
– A subroutine may pass a return value back to the
caller.
• Space in data memory must be reserved for
parameters, local variables, and the return value.
• Parameter passing from one callee to caller
subroutine can be done in two ways
– Through general purpose registers
– Through processor stack
9/17/2021 22
Example: Parameter passing through registers

9/17/2021 23
Parameter Passing through Processor Stack

Pass by Reference: #NUM1


Pass by Value: N

9/17/2021 24
Stack Frame and Frame Pointer FP
• Stack Frame is a set of memory locations
that constitute the parameters associated with
the subroutine which are created at the time
subroutine is called and freed up when the
subroutine returns control to the calling
function
• Frame Pointer (FP): is a register that
contains the address of a memory location
within the stack frame.
• For example, FP points to location above the
return address in figure. It helps to access
other locations within the stack frame.
• Content of FP is fixed throughout the
execution of the subroutine unlike SP
• Which location should be fixed for FP?

25
26
• Questions?

9/17/2021 Computer Architecture CS F342 27

You might also like