You are on page 1of 24

Stack Organization

Praveen
Stack
• A stack is a storage device that stores information .
• The item stored last is the first item retrieved.
• Eg: stack of trays

Praveen
Stack in digital computers
• It is a memory unit with an address register that can count only
after an initial value load in it.
• Stack pointer (SP):
• holds the address of the stack
• The value of the stack pointer always points at the top item in the

Praveen
stack.
• Two operations on stack:
• Insertion of items  PUSH
• Deletion of items POP
• Register Stack:
• A stack can be placed in a portion of a large memory .
• or it can be organized as a collection of finite number of words or
registers.
REGISTER STACK ORGANIZATION
Register Stack
Stack Pointer: value is equal to the address
of the word that is currently on the top of the
stack.
• Three items are placed in the stack. A,B,C
• Item C is on the top of the stack so SP=3
• To remove the top item, the stack is
popped by reading the memory word at
address 3 and decrementing the content
of SP.

Praveen
• Item B is now on the top of the stack since
SP holds address 2.
Push, Pop operations • To insert a new item , the stack is pushed
/* Initially, SP = 0, EMPTY = 1, FULL = 0 */ by incrementing SP and writing a word in
PUSH POP the next-higher location in the stack .

SP  SP + 1 DR  M[SP]
M[SP]  DR SP  SP - 1
If (SP = 0) then (FULL  1) If (SP = 0) then (EMPTY  1)
EMPTY  0 FULL  0
Example for stack

Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
Praveen
https://www.slideshare.net/Thenmurugeshwari/infix-to-postfix-
conversion
MEMORY STACK ORGANIZATION
1000

Memory with Program, Data, PC


Program
(instructions)
and Stack Segments
- A portion of memory is used as a stack with a AR
Data
(operands)
processor register as a stack pointer
SP 3000
stack
- PUSH: SP  SP - 1
M[SP]  DR 3997
3998
- POP: DR  M[SP]

Praveen
3999
SP  SP + 1 4000
4001
- Most computers do not provide hardware to check
DR
stack overflow (full stack) or underflow(empty stack)
• Program: points at the address of the next instruction in the program.
• Address Register(AR):
• points the array of data.
• AR is used during the execute phase to read an operand.
• Stack pointer: (SP) points at the top of the stack.
• Sp is used to push or pop items into the or from the stack.
The three registers are connected to a common address bus, either one can
provide an address for memory.
By I. Mala serene (AP(SG), SCOPE
REVERSE POLISH NOTATION
Arithmetic Expressions: A + B
A+B Infix notation
+AB Prefix or Polish notation
AB+ Postfix or reverse Polish notation
- The reverse Polish notation is very suitable for stack
manipulation

Evaluation of Arithmetic Expressions

Praveen
Any arithmetic expression
can be expressed in
parenthesis-free
Polish notation, including
reverse Polish notation
(3 * 4) + (5 * 6)  34*56*+

6
4 5 5 30
3 3 12 12 12 12 42
3 4 * 5 6 * +
Reverse polish notation

Praveen
Reverse polish notation

Praveen
problems

Praveen
References
• Computer System Architecture by Morris Mano

Praveen

You might also like