MADE BY RIDA ZAMAN 1
STACK
Stack is a data structure in which last item inserted is taken
out first
It works in LIFO manner (last in first out)
MADE BY RIDA ZAMAN 2
OPERATIONS ON STACK
We can perform two operation on a stack
INSERTION (PUSH)
DELETION (POP)
MADE BY RIDA ZAMAN 3
STACK ORGANIZATION
IN STACK ORGANIZATION WE STORE OR PLACE OUR
DATA IN A MANNER OF A STACK.
MADE BY RIDA ZAMAN 4
STACK POINTER
It’s an register which is used to store the address of the top
item in the stack
It’s a 6-bit register
MADE BY RIDA ZAMAN 5
DATA REGISTER
Whatever value we INSERT(PUSH) or DELETE(POP)
from the stack it stores in data register
It’s a 2byte register /16bits
MADE BY RIDA ZAMAN 6
TYPES OF STACK ORGANIZATION
There are two types of stack organization we have for our
CPU
REGISTER STACK
MEMORY STACK
MADE BY RIDA ZAMAN 7
REGISTER STACK
If we have for example finite number of words which we
want to insert at top of another and making stack out of it
through register stack.
MADE BY RIDA ZAMAN 8
SINGLE BIT REGISTER
We have two single bit registers
FULL: If the entire stack is full then this full register will
set to be 1
EMPTY: If the entire stack is empty then this empty
register will set to be 1
MADE BY RIDA ZAMAN 9
ALGORITHM FOR PUSH
SPSP+1
M{SP}DR
IF(SP=0)THEN (F1)
EMPTY0
MADE BY RIDA ZAMAN 10
ALGORITHM FOR PUSH
STEP BY STEP
SPSP+1 Increment stack by a value 1
M{SP}DR Whatever content present in data register
place it inside the memory word to which stack pointer is
presently pointing
IF(SP=0)THEN (F1) We are performing push
operation so we may encounter a position where our stack
would become full so we have to set our full bit to 1
If our stack pointer is equal to zero set full=1
EMPTY0 Because we are putting values into the stack
so its not empty anymore
MADE BY RIDA ZAMAN 11
ALGORITHM FOR POP
DRM{SP}
SPSP-1
IF {SP=0}{EMPTY1}
FULL0
MADE BY RIDA ZAMAN 12
ALGORITHM FOR POP
STEP BY STEP
DRM{SP}Whatever content I have in the memory word
that the stack pointer is pointing at present store it in the
data register
SPSP-1 The stack pointer should point to the next top
element ,it must be decremented by a value-1
IF{SP=0}{EMPTY1} We are performing POP operation
so we may encounter a position where our stack would
become entirely empty at that point we will set empty =1
FULL0 Because we are deleting values from stack so its
obvious that it wont become full at any point
MADE BY RIDA ZAMAN 13
MEMORY STACK
In Memory stack we have a Memory Unit
In memory unit we divide memory into 3 portions
PROGRAM(INSTRUSTIONS): it is use to access/fetch
instructions from memory
DATA(OPERANDS): it is use to access/fetch data and
operands from memory
STACK: it is use to push or pop item into the stack
In memory stack our stack grows with decreasing address
There is different algorithm for both memory and register
stacks
MADE BY RIDA ZAMAN 14
MEMORY UNIT
MADE BY RIDA ZAMAN 15
ALGORITHM’S
PUSH POP
SPSP-1 DRM{SP}
M{SP}DR SPSP+1
MADE BY RIDA ZAMAN 16
ALGORITHM FOR PUSH
STEP BY STEP
SPSP-1 The first operation we will do is we will
decrement our stack pointer
M{SP}DR Whatever data we have in the data register we
should place it at the memory word at which the stack
pointer is pointing at
MADE BY RIDA ZAMAN 17
ALGORITHM FOR POP
STEP BY STEP
DRM{SP} Whatever content I have in the memory word
of stack pointer I have to place it into data register
SPSP+1 Then we’ll increment our stack pointer by 1
MADE BY RIDA ZAMAN 18
DIFFERENCE BETWEEN
REGISTER STACK AND MEMORY STACK
The stack grows with The stack grows with
increasing address in decreasing address in
register stack register stack
Register stack is generally Memory stack is on the
on the CPU RAM(main memory)
Access to register stack is Access to memory stack is
fast not as fast as register stack
Register stack is limited size Memory stack is larger then
wise register stack size wise
MADE BY RIDA ZAMAN 19
PRACTICAL APPLICATION
OF HOW STACK
ORGANIZATION WORKS
INSIDE OUR CENTRAL
PROCESSING UNIT
MADE BY RIDA ZAMAN 20
DIFFERENT NOTATIONS WE HAVE IN
REPRESENTING ARITHMETIC NOTATIONS
INFIX
PREFIX (POLISH NOTATION)
POSTFIX (REVERSE POLISH NOTATION/RPN)
MADE BY RIDA ZAMAN 21
INFIX NOTATION
In INFIX notation we have to write the operator in
between the operands
MADE BY RIDA ZAMAN 22
PREFIX NOTATION
In PREFIX notation we have to write the operator before
the operands
Most of the computer use PREFIX notation to execute
arithmetic operations
MADE BY RIDA ZAMAN 23
POSTFIX
(REVERSE POLISH NOTATION/RPN)
In POSTFIX notation we have to write the operator after
the operands
MADE BY RIDA ZAMAN 24
EXAMPLE:
MADE BY RIDA ZAMAN 25
SO WHAT THE COMPUTER DOES?
It scan the particular expression from left to right whenever
it encounters an operator it does the particular operation on
the previous two elements/operands that are scanning
MADE BY RIDA ZAMAN 26
HOW STACK ORGANIZATION
IS USE TO EVALUATE
ARITHEMATIC
EXPRESSIONS
MADE BY RIDA ZAMAN 27
WHEN AN OPERATOR IS
ENCOUNTERED
The two top most elements in the stack are use for the
operation
The stack is popped and the result of the operation replaces
the lower operand
MADE BY RIDA ZAMAN 28
EXAMPLE:
MADE BY RIDA ZAMAN 29
MADE BY RIDA ZAMAN 30