0% found this document useful (0 votes)
14 views14 pages

L10 - Stack Organization - Unit 1

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)
14 views14 pages

L10 - Stack Organization - Unit 1

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/ 14

KIET Group of Institutions, Delhi-NCR, Ghaziabad

(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)

Stack Organization

Topics Covered:
• Introduction of Stack
• Operations to be performed onto the stack
• Stack pointer movement through the Stack
• Types of the Stack
• List of some applications of stack

After this lecture, a student will be able to


1. Define the stack.
2. Describe the working of a stack.
3. Insert an element into and remove an element from the
stack.
4. Analyse the pointer movement across the ends of a stack.
5. Differentiate between register and memory stacks.
6. Design register and memory stacks
What is Stack?
A stack is a list of data elements, usually words, 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.
The following figure shows the example stack of 4 books.

A stack is a storage device that stores information in such a


manner that the item stored last is the first item retrieved.

Last-in–First-out (LIFO) Principle: The last data item placed on the stack is the
first one removed when retrieval begins. In other way, we may call it as First-in-
Last out (FILO) principle; the first data item placed on the stack will be
removed in the last.
The structure is sometimes referred to as a pushdown stack.
How to work with Stack? Or
What are the operations to be performed onto the stack?
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.
Push: To insert an element into Stack
Pop: To remove the topmost element from the stack
A register is used to store the address of the topmost element of the stack which
is known as Stack pointer (SP). The stack pointer register SP contains a binary
number whose value is equal to the address of the element that is currently on
top of the stack.

What are types of Stack?


• Register Stack
• Memory Stack

Register Stack:
• A stack can be placed in a portion of a large memory or it can be
organized as a collection of a finite number of memory words or
registers.
• Two one-bit registers namely FULL and EMPTY will be used to get the
status of stack such as whether the stack if full (containing all the
elements according to the capacity) or empty (containing no element).
• Numbering for N registers in a register stack: 0 to (N-1).
Example: the registers will be number from 0 to 7 in 8-register stack.

Overflow: An error condition occurs during insertion a new


element when stack is full. This condition is called overflow)
Underflow: An error condition occurs during removal of an
element when stack is empty. This condition is also called
underflow)
Example: A register stack of 64 words or registers

FULL is a one-bit flag register for showing the status of stack whether it is Full
(containing all 64 elements) or not.
• FULL=0 (Stack is not full)
• FULL=1 (Stack is full)
EMPTY is also a one-bit flag register for showing the status of stack whether it
is Empty (containing no element) or not
• EMPTY =0 (Stack is Not empty)
• EMPTY = 1 (Stack is Empty)

DR is the data register that holds the binary data to be written in to or read out
of the stack.
Initially:
• FULL=0
• EMPTY=1
• SP=0

The above figure shows the organization of a 64-word register stack. The stack
pointer register SP contains a binary number whose value is equal to the address
of the element that is currently on top of the stack.

Suppose number of bits in register can be stored = k


Size of the register = k bits
Number of the registers in Stack = N
Then, value of k,

2k = N
K= ⌊log2 (N) ⌋ + 1

Example: In a 64-word stack, the stack pointer contains 6 bits because 26

Initially, SP is cleared to 0, Empty is set to 1, and Full is cleared to 0, so that SP


points to the word at address 0 and the stack is marked empty and not full. If the
stack is not full, a new item is inserted with a push operation.

The operation which is performed on the registers is called a


micro-operation.

PUSH Operation: The push operation is implemented with the


following sequence of micro-operations.

SP ←SP + 1 (Increment stack pointer)


M [SP] ← DR (Write item on top of the stack)
if (SP=0) then (Full ← 1) (Check if stack is full)
Empty ← 0 (Marked the stack not empty)
The stack pointer is incremented so that it points to the address of the next-
higher word. A memory write operation inserts the word from DR into the top
of the stack. Note that SP holds the address of the top of the stack and that M
[SP] denotes the memory word specified by the address presently available in
SP.

Note:
The first item stored in the stack is at address 1.
The second last (or prior to last) item stored in the stack is at address 63.
The last item is stored at address 0

Initially, SP points to 0. The first item stored in the stack is at address 1. The
last item is stored at address 0, if SP reaches 0; the stack is full of item, so
FULL is set to 1. This condition is reached if the top item prior to the last push
was in location 63 and after increment SP, the last item stored in location 0.
Once an item is stored in location 0, there are no more empty register in the
stack. If an item is written in the stack, obviously the stack cannot be empty, so
EMTY is cleared to 0.

Example: Insertion of items into the stack:


Add element ‘A’ to the Stack
• Initially: • Push an element:
FULL=0 SP= SP+1 = 0+1 =1
EMPTY=1 M[SP] DR ➔( M[1]=A)
SP=0 If (SP==0) ➔No
EMPTY=0

The following figure shows the Stack after inserting item ‘A’
The following figure shows the Stack after inserting item ‘B’
The following figure shows the Stack after inserting item ‘C’

Three items are placed in the stack: A, B, and C in the order. Item C is on the
top of the stack so the SP points to address 3.
How does a stack pointer move across the ends of the stack?
How does SP points to address 0 from address 63 after incrementing SP by 1?
In a 64-word stack, the stack pointer contains 6 bits because 26 =64.
Since SP has only six bits, it cannot exceed a number greater than 63 (111111 in
binary).
When after 63, SP is incremented by 1, the result is 0 since 111111 + 1 =1000000 in
binary according to the binary addition, but SP can accommodate only the six least
significant bits i.e. SP=000000.
How does SP points to address 63 from address 0 after decrementing by 1?
SP =0= 000000 (in binary)
SP=SP-1 =000000-1

The subtraction in digital computer system will be done using 2’s complement method.
So, the subtraction will be converted into binary addition like
SP = SP + (-1)
SP = SP + 2’s complement (+1)
= 000000 + 111111
= 111111
=63

How 2’s complement of 1 is calculated?


6-bit binary representation of 1 in binary is 000001
2’s complement (1) = 1’s complement (1) +1
= 1’s complement (000001) +1
= 111110 + 1
= 111111
How does SP points to address 0 from address 63 after
incrementing SP by 1?
Ans:

How does SP points to address 63 from address 0 after


decrementing by 1?
POP Operation: The pop operation is implemented with the following
sequence of micro-operations.
DR← M [SP] (Read item from the top of stack)
SP ← SP-1 (Decrement stack Pointer)
If (SP=0) then (Empty ← 1) (Check if stack is empty)
FULL ← 0 (Mark the stack not full)
The top item is read from the stack into DR. The stack pointer is then
decremented. If its value reaches zero, the stack is empty, so Empty is set to 1.
This condition is reached if the item read was in location 1. Once this item is
read out, SP is decremented and reaches the value 0, which is the initial value of
SP.
Note that if a pop operation reads the item from location 0 and then SP is
decremented, SP changes to 111111, which is equal to decimal 63. In this
configuration, the word in address 0 receives the last item in the stack. Note also
that an erroneous operation will result if the stack is pushed when FULL=1 or
popped when EMTY =1.
Example of removal of an element or pop operation:

• Given • Pop an item


Full = 0 DR← M [SP] ➔ DR= M[3]= C
Empty= 0 SP= SP-1 ➔ SP=3-1= 2
SP=3 If (SP==0) ➔ NO
M[SP]=C FULL=0

1. To remove the top item, the stack is popped by reading the memory word at
address 3 and decrementing the content of SP. Item B is now on top of the stack
since SP holds address
2. Note that item C has read out but not physically removed. But it is logically
out of the stack. This does not matter because when the stack is pushed, a new
item is written in its place.
Memory Stack:
A stack can exist as a stand-alone unit or can be implemented in a random
access memory attached to CPU. The implementation of a stack in the CPU is
done by assigning a portion of memory to a stack operation and using a
processor register as a Stack Pointer.
Figure given below shows a portion of computer memory partitioned in to three
segments program, data and stack.

The Program Counter PC points at the address of the next instruction in the
program.
The Address Register AR points at an array of data.
The Stack Pointer SP points at the top of the stack.
The three register are connected to a common address bus, and either one can
provide an address for memory. PC is used during the fetch phase to read an
instruction. AR is used during the execute phase to read an operand. SP is used
to push or POP items into or from the stack.
The initial value of SP is 4001 and the stack grows with decreasing addresses.
Thus the first item stored in the stack is at address 4000, the second item is
stored at address 3999, and the last address that can be used for the stack is
3000. No provisions are available for stack limit checks. We assume that the
items in the stack communicate with a data register DR.

Push Operation:
A new item is inserted with the push operation as follows.
SP← SP-1 (Decrement stack Pointer)
M [SP] ← DR (Write item on top of the stack)

The stack pointer is decremented so that it points at the address of the next
word. A Memory write operation inserts the word from DR into the top of the
stack.
Pop Operation:
A new item is deleted with a pop operation as follows.
DR← M [SP] (Read item from the top of stack)
SP←SP + 1 (increment stack Pointer)
The top item is read from the stack in to DR. The stack pointer is then
incremented to point at the next item in the stack.
Most computers do not provide hardware to check for stack overflow (FULL) or
underflow (Empty). The stack limit can be checked by using two processor
register:
One to hold upper limit and other hold the lower limit. After the pop or push
operation SP is compared with lower or upper limit register.

Note:
In case of memory stack, stack may grow in direction of increasing
addresses or decreasing addresses of memory. It depends on the design of
the system.
What are the applications of a Stack in a computer system?
There are the following applications in computer architecture.
• Subroutines Call
• Zero-address Instruction Implementation
• Infix notation to Post-fix notation conversion
• Evaluation of Arithmetic Expressions

You might also like