11 - Stack Organization
11 - Stack Organization
Organization
Contents
• Stack
• Operations on Stack
• Types of Stack
• Applications of Stack
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.
• 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.
Types of Stack
• 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.
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
M[SP] DR ( M[1]=A)
If (SP==0) No
EMPTY=0
Add an element B to the Stack
• Given
FULL=0
EMPTY=0
SP=1
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) NO
FULL=0
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.
.
Memory Stack:
• Push Operation:
• A new item is inserted with the push operation
as follows.