MEMORY STACK
❖ A stack can exist as a stand-alone.
❖ Stack can be implemented in a random-access memory
(RAM) attached to a CPU.
❖ 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.
❖ A portion of computer memory divided into three
segments: program, data and stack.
❖ 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.
❖ The three register are connected to a common address bus,
and either one can provide an address for memory.
❖ PC is used during the phase to read an instruction.
❖ AR is used during the execute phase to read operand.
❖ SP is used to push or pop items into or from the stack.
Memory unit address
Program 1000
PC
(instructions)
AR
Data 2000
(operands)
stack 3000
3997
SP 3998
3999
4000
4001
DR
Example :
The items in the stack communicate with a data register. A new
item is inserted with the push operation as follows:
PUSH:
SP <-SP – 1
M[SP] <- DR
The stack pointer is decremented so that it points at the address
of the next word. A memory write operation inserts the word
from DR into the top of the stack. A new item is deleted with a
pop operation as follows:
POP:
DR <- M[SP]
SP <- SP + 1
INFIX TO POSTFIX
❖ A stack organisation is very effective for evaluating
arithmetic expressions.
❖ The common arithmetic expressions are written in
infix notation, with each operator written between the
operands.
A*B+C*D
❖ That the arithmetic operation can be represented in
prefix notation. These representation, often referred
to as polish notation, place sthe operator before the
operands.
❖ The postfix notation referred to as reverse polish
notation (RPN), places the operator after the
operands.
❖ Examples –
A+B INFIX NOTATION
+AB PREFIX OR POLISH NOTATION
AB+ POSTFIX OR REVERSE POLISH NOTATION
The reverse polish notation is in a form suitable for
stack manipulation. A*B+C*D
The expression written in reverse polish notation as
AB*CD*+.
Precedence Order and Associativity of Operators
The precedence order of operators is given in the below table.
Precedence Type Operators Associativity
1 Postfix () [] -> . ++ — Left to Right
2 Unary + – ! ~ ++ — (type)* & size of Right to Left
3 Multiplicative */% Left to Right
4 Additive +– Left to Right
5 Shift <<, >> Left to Right
6 Relational < <= > >= Left to Right
7 Equality == != Left to Right
8 Bitwise AND & Left to Right
9 Bitwise XOR ^ Left to Right
10 Bitwise OR | Left to Right
11 Logical AND && Left to Right
12 Logical OR || Left to Right
13 Conditional ?: Right to Left
14 Assignment = += -+ *= /= %= >>= <<= &= ^= |= Right to Left
15 Comma , Left to Right
RULES:
• Priority - ^, */, +-
• No two operators of the same priority can stay together in stack
column
• Lowest priority cannot be placed before highest priority.
A*B+C*D infix to postfix
SYMBOL SCANNED STACK POSTFIX DESCRIPTION
EXPRESSION
1 ( ( START
2 A ( A
3 * (* A
4 B (* AB
5 + (+ AB* ‘*’ Is at higher
precedence than
‘+’
6 C (+ AB*C
7 * (+ AB*C
8 D (+ AB*CD
9 ) EMPTY AB*CD*+ END
a-b-c infix to postfix
SYMBOL SCANNED STACK POSTFIX DESCRIPTION
EXPRESSION
1 a A START
2 - - A
3 b - Ab
No two operators of same
4 - -- ab- priority are stay together
5 c - ab-c
6 - ab-c-
A*(B+C+D) infix to postfix
SYMBOL SCANNED STACK POSTFIX DESCRIPTION
EXPRESSION
1 A A START
2 * * A
3 ( *( A
4 B *( AB
5 + *(+ AB
6 C *(+ ABC
No two operators of
7 + *(++ AB*C+ same priority are stay
together
8 D *(+ AB*C+D
9 ) *(+) AB*C+D+
AB*C+D+*
a + b * c infix to postfix
SYMBOL SCANNED STACK POSTFIX DESCRIPTION
EXPRESSION
1 a a START
2 + + a
3 b + ab
4 * +* ab
5 c +* abc* * has highest priority
than +
6 abc*+
A+B*(C+D)/F+D*E
SYMBOL SCANNED STACK POSTFIX DESCRIPTION
EXPRESSION
1 A A START
2 + + A
3 B + AB
4 * +* AB
5 ( +*( AB
6 C +*( ABC
7 + +*(+ ABC
8 D +*(+ ABCD
9 ) +*(+) ABCD+
10 / +*/ ABCD+* No two operators of
same priority are
stay together
11 F +/ ABCD+*F
12 + +/+ ABCD+*F/+ / has highest priority
than +
13 D + ABCD+*F/+D
14 * + * ABCD+F/+D
15 E +* ABCD+F/+DE
16 + * ABCD+F/+DE*
ABCD+F/+DE*+