Stack Organization
Stack Organization
Stack Organization
by
Upendra Mishra
KIET Group of Institutions, Delhi-NCR, Ghaziabad
Contents
• Stack
• Operations on Stack
• Types of Stack
• Applications of Stack
What is Stack?
How to work with Stack?
• Operations on Stack
• Push: to insert an element into the stack
• Pop: To remove the top 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 word that is currently on top of the 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.
• Example: A register stack of 64 words or
registers
Initially:
FULL=0
EMPTY=1
SP=0
Register Stack (contd..)
FULL=0 (Stack is not full) • FULL is a one-bit flag register for showing the
FULL=1 (Stack is full) status of stack whether it is Full (containing all
64 elements) or not.
EMPTY =0 (Stack is Not empty) • EMPTY is also a one-bit flag register for showing
the status of stack whether it is Empty
EMPTY = 1 (Stack is Empty) (containing no element) or not
Push an element:
SP= SP+1 = 1+1 =2
M[SP] DR
( M[2]=B)
If (SP==0) No
EMPTY=0
Add an element C to the Stack
• Given
FULL=0
EMPTY=0
SP=2
Push an element:
SP= SP+1 = 2+1 =3
M[SP] DR ( M[3]=C)
If (SP==0) No
EMPTY=0
Removing an element from the stack
• POP operation: • The top item is read from the
• DR← M [SP] (Read item from the top of stack) stack into DR.
• SP ← SP-1 Decrement stack Pointer
• If ( SP==0) then (Empty ← 1) Check if stack • The stack pointer is then
is empty decremented.
• FULL ← 0 (Mark the stack not full)
• If its value reaches zero, the
stack is empty, so Empty is set to
1.
Removing the top element from the stack
Given
Full = 0
Empty= 0
SP=3
Pop an item
DR← M [SP] DR= M[3]= C
SP= SP-1 SP=3-1= 2
If (SP==0) then (Empty 1)
FULL=0
000000
- 1
111111
Important Point
• 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.