0% found this document useful (0 votes)
8 views

11 - Stack Organization

Uploaded by

kudesiat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

11 - Stack Organization

Uploaded by

kudesiat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Stack

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.

• Last-in–First-out (LIFO): the last data item placed on the


stack is the first one removed when retrieval begins.

• The structure is sometimes referred to as a pushdown 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.
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.

• 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

• DR is the data register that holds the binary data


to be written in to or read out of the stack.
Register Stack (contd..)
PUSH Operation: • PUSH Operation: The push operation is
• SP ←SP + 1 (Increment stack pointer) implemented with the shown sequence
of micro-operation.
• M [SP] ← DR (Write item on top of the
stack)
• The stack pointer is incremented so that
it points to the address of the next-
• if (SP==0) then (Full ← 1) (Check if stack higher word.
is full)
• Empty ← 0 (Marked the stack not • A memory write operation inserts the
empty) word from DR into the top of the stack.
Note that SP holds the address of the top
Add element A to the Stack
• Initially:
FULL=0
EMPTY=1
SP=0
Push an element:
SP= SP+1 = 0+1 =1

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.

• The last item is stored at address 0.


Stack Pointer Movement
Stack Pointer Movement
Memory Stack:
• 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.
• 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.

.
Memory Stack:
• 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)
Memory Stack:
• 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)


Applications of Stack
• Subroutines Call
• Zero-address Instruction Implementation
• Infix notation to Post-fix notation
• Evaluation of Arithmetic Expressions
Thank
You

You might also like