1MADE BY RIDA ZAMAN
STACK
 Stack is a data structure in which last item inserted is taken
out first
 It works in LIFO manner (last in first out)
2MADE BY RIDA ZAMAN
OPERATIONS ON STACK
 We can perform two operation on a stack
 INSERTION (PUSH)
 DELETION (POP)
3MADE BY RIDA ZAMAN
STACK ORGANIZATION
IN STACK ORGANIZATION WE STORE OR PLACE OUR
DATA IN A MANNER OF A STACK.
4MADE BY RIDA ZAMAN
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
5MADE BY RIDA ZAMAN
DATA REGISTER
Whatever value we INSERT(PUSH) or DELETE(POP)
from the stack it stores in data register
It’s a 2byte register /16bits
6MADE BY RIDA ZAMAN
TYPES OF STACK ORGANIZATION
There are two types of stack organization we have for our
CPU
 REGISTER STACK
 MEMORY STACK
7MADE BY RIDA ZAMAN
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.
8MADE BY RIDA ZAMAN
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
9MADE BY RIDA ZAMAN
ALGORITHM FOR PUSH
 SPSP+1
 M{SP}DR
 IF(SP=0)THEN (F1)
 EMPTY0
10MADE BY RIDA ZAMAN
ALGORITHM FOR PUSH
STEP BY STEP
 SPSP+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 (F1) 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
 EMPTY0 Because we are putting values into the stack
so its not empty anymore
11MADE BY RIDA ZAMAN
ALGORITHM FOR POP
 DRM{SP}
 SPSP-1
 IF {SP=0}{EMPTY1}
 FULL0
12MADE BY RIDA ZAMAN
ALGORITHM FOR POP
STEP BY STEP
 DRM{SP}Whatever content I have in the memory word
that the stack pointer is pointing at present store it in the
data register
 SPSP-1 The stack pointer should point to the next top
element ,it must be decremented by a value-1
 IF{SP=0}{EMPTY1} 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
 FULL0 Because we are deleting values from stack so its
obvious that it wont become full at any point
13MADE BY RIDA ZAMAN
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
14MADE BY RIDA ZAMAN
MEMORY UNIT
15MADE BY RIDA ZAMAN
ALGORITHM’S
PUSH POP
SPSP-1
M{SP}DR
DRM{SP}
SPSP+1
16MADE BY RIDA ZAMAN
ALGORITHM FOR PUSH
STEP BY STEP
 SPSP-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
17MADE BY RIDA ZAMAN
ALGORITHM FOR POP
STEP BY STEP
 DRM{SP} Whatever content I have in the memory word
of stack pointer I have to place it into data register
 SPSP+1 Then we’ll increment our stack pointer by 1
18MADE BY RIDA ZAMAN
DIFFERENCE BETWEEN
REGISTER STACK AND MEMORY STACK
 The stack grows with
increasing address in
register stack
 Register stack is generally
on the CPU
 Access to register stack is
fast
 Register stack is limited size
wise
 The stack grows with
decreasing address in
register stack
 Memory stack is on the
RAM(main memory)
 Access to memory stack is
not as fast as register stack
 Memory stack is larger then
register stack size wise
19MADE BY RIDA ZAMAN
PRACTICAL APPLICATION
OF HOW STACK
ORGANIZATION WORKS
INSIDE OUR CENTRAL
PROCESSING UNIT
20MADE BY RIDA ZAMAN
DIFFERENT NOTATIONS WE HAVE IN
REPRESENTING ARITHMETIC NOTATIONS
 INFIX
 PREFIX (POLISH NOTATION)
 POSTFIX (REVERSE POLISH NOTATION/RPN)
21MADE BY RIDA ZAMAN
INFIX NOTATION
 In INFIX notation we have to write the operator in
between the operands
22MADE BY RIDA ZAMAN
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
23MADE BY RIDA ZAMAN
POSTFIX
(REVERSE POLISH NOTATION/RPN)
 In POSTFIX notation we have to write the operator after
the operands
24MADE BY RIDA ZAMAN
EXAMPLE:
25MADE BY RIDA ZAMAN
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
26MADE BY RIDA ZAMAN
HOW STACK ORGANIZATION
IS USE TO EVALUATE
ARITHEMATIC
EXPRESSIONS
27MADE BY RIDA ZAMAN
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
28MADE BY RIDA ZAMAN
EXAMPLE:
29MADE BY RIDA ZAMAN
30MADE BY RIDA ZAMAN

Stack Operations

  • 1.
  • 2.
    STACK  Stack isa data structure in which last item inserted is taken out first  It works in LIFO manner (last in first out) 2MADE BY RIDA ZAMAN
  • 3.
    OPERATIONS ON STACK We can perform two operation on a stack  INSERTION (PUSH)  DELETION (POP) 3MADE BY RIDA ZAMAN
  • 4.
    STACK ORGANIZATION IN STACKORGANIZATION WE STORE OR PLACE OUR DATA IN A MANNER OF A STACK. 4MADE BY RIDA ZAMAN
  • 5.
    STACK POINTER  It’san register which is used to store the address of the top item in the stack  It’s a 6-bit register 5MADE BY RIDA ZAMAN
  • 6.
    DATA REGISTER Whatever valuewe INSERT(PUSH) or DELETE(POP) from the stack it stores in data register It’s a 2byte register /16bits 6MADE BY RIDA ZAMAN
  • 7.
    TYPES OF STACKORGANIZATION There are two types of stack organization we have for our CPU  REGISTER STACK  MEMORY STACK 7MADE BY RIDA ZAMAN
  • 8.
    REGISTER STACK  Ifwe 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. 8MADE BY RIDA ZAMAN
  • 9.
    SINGLE BIT REGISTER Wehave 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 9MADE BY RIDA ZAMAN
  • 10.
    ALGORITHM FOR PUSH SPSP+1  M{SP}DR  IF(SP=0)THEN (F1)  EMPTY0 10MADE BY RIDA ZAMAN
  • 11.
    ALGORITHM FOR PUSH STEPBY STEP  SPSP+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 (F1) 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  EMPTY0 Because we are putting values into the stack so its not empty anymore 11MADE BY RIDA ZAMAN
  • 12.
    ALGORITHM FOR POP DRM{SP}  SPSP-1  IF {SP=0}{EMPTY1}  FULL0 12MADE BY RIDA ZAMAN
  • 13.
    ALGORITHM FOR POP STEPBY STEP  DRM{SP}Whatever content I have in the memory word that the stack pointer is pointing at present store it in the data register  SPSP-1 The stack pointer should point to the next top element ,it must be decremented by a value-1  IF{SP=0}{EMPTY1} 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  FULL0 Because we are deleting values from stack so its obvious that it wont become full at any point 13MADE BY RIDA ZAMAN
  • 14.
    MEMORY STACK  InMemory 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 14MADE BY RIDA ZAMAN
  • 15.
  • 16.
  • 17.
    ALGORITHM FOR PUSH STEPBY STEP  SPSP-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 17MADE BY RIDA ZAMAN
  • 18.
    ALGORITHM FOR POP STEPBY STEP  DRM{SP} Whatever content I have in the memory word of stack pointer I have to place it into data register  SPSP+1 Then we’ll increment our stack pointer by 1 18MADE BY RIDA ZAMAN
  • 19.
    DIFFERENCE BETWEEN REGISTER STACKAND MEMORY STACK  The stack grows with increasing address in register stack  Register stack is generally on the CPU  Access to register stack is fast  Register stack is limited size wise  The stack grows with decreasing address in register stack  Memory stack is on the RAM(main memory)  Access to memory stack is not as fast as register stack  Memory stack is larger then register stack size wise 19MADE BY RIDA ZAMAN
  • 20.
    PRACTICAL APPLICATION OF HOWSTACK ORGANIZATION WORKS INSIDE OUR CENTRAL PROCESSING UNIT 20MADE BY RIDA ZAMAN
  • 21.
    DIFFERENT NOTATIONS WEHAVE IN REPRESENTING ARITHMETIC NOTATIONS  INFIX  PREFIX (POLISH NOTATION)  POSTFIX (REVERSE POLISH NOTATION/RPN) 21MADE BY RIDA ZAMAN
  • 22.
    INFIX NOTATION  InINFIX notation we have to write the operator in between the operands 22MADE BY RIDA ZAMAN
  • 23.
    PREFIX NOTATION  InPREFIX notation we have to write the operator before the operands  Most of the computer use PREFIX notation to execute arithmetic operations 23MADE BY RIDA ZAMAN
  • 24.
    POSTFIX (REVERSE POLISH NOTATION/RPN) In POSTFIX notation we have to write the operator after the operands 24MADE BY RIDA ZAMAN
  • 25.
  • 26.
    SO WHAT THECOMPUTER 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 26MADE BY RIDA ZAMAN
  • 27.
    HOW STACK ORGANIZATION ISUSE TO EVALUATE ARITHEMATIC EXPRESSIONS 27MADE BY RIDA ZAMAN
  • 28.
    WHEN AN OPERATORIS 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 28MADE BY RIDA ZAMAN
  • 29.
  • 30.