0% found this document useful (0 votes)
271 views19 pages

KCS302 - Stack Organization & RPN

The document covers register and memory stack organization. A register stack uses a stack pointer register and data register to implement push

Uploaded by

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

KCS302 - Stack Organization & RPN

The document covers register and memory stack organization. A register stack uses a stack pointer register and data register to implement push

Uploaded by

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

Computer Organization and

Architecture

Unit-I
• Stack Organization: Introduction
• Register Stack
• Memory Stack
Stack Organization: Introduction
• A stack is a storage device that stores
information in such a manner that the element
stored last is the first element retrieved.

• A stack is a memory unit with an address


register, which is known as Stack Pointer (SP),
because its value always points at the top
element in the stack.
Stack Organization (Operations)
• The two operations of a stack are the insertion
and deletion of elements.

• The operation of insertion is called push (or


push-down) operation whereas the operation
of deletion is called pop (or pop-up)
operation.

• Stacks belong in two categories i.e.


Register Stack & Memory Stack.
Register Stack

A stack can be organized as a collection


of a finite number of memory words or
registers.

Following figure shows the organization


of a 64-word register stack-
64-word Register Stack
Register Stack: Description
· 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.

• In a 64-word stack, the stack pointer contains 6 bits


because 26 = 64.

• Since SP has only six bits, it cannot exceed a number


greater than 63 (111111 in binary).
Register Stack: Description (Contd.)
• When 63 is incremented by 1, the result is 0 since
111111 + 1 = 1000000 in binary, but SP can
accommodate only the six least significant bits.
Similarly, when 000000 is decremented by 1, the result
is 111111.

• The one-bit register FULL is set to 1 when the stack is


full, and the one-bit register EMTY is set to 1 when
the stack is empty of elements.

• DR is the data register that holds the binary data to be


written into or read out of the stack.
Register Stack: Push Operation
• Initially, SP is cleared to 0, EMTY is set to 1, and
FULL is cleared to 0, so that SP points to the word at
address 0 and the stack is marked empty and not full.

• If the stack is not full (if FULL = 0), a new element is


inserted with a push operation. The push operation is
implemented with the following sequence of micro-
operations;

SP ← SP + 1 Increment stack pointer


M [SP] ← DR Write element on top of the stack
If (SP = 0) then (FULL ← 1) Check if stack is full
EMTY ← 0 Mark the stack not empty
Register Stack: Pop Operation
• A new element is deleted from the stack if the stack is
not empty (if EMTY = 0). The pop operation consists
of the following sequence of micro-operations:

DR ← M [SP] Read item from the top of stack


SP ← SP – 1 Decrement stack pointer
If (SP = 0) then (EMTY ← 1) Check if stack is empty
FULL ← 0 Mark the stack not full

• The top element is read from the stack into DR. The stack
pointer is then decremented. If its value reaches zero, the
stack is empty, so EMTY is set to 1.
• This condition is reached if the element read was in
location 1. Once this element is read out, SP is decremented
and reaches the value 0, which is the initial value of SP.
Memory Stack
Memory Stack: Description
In the diagram, three registers are connected to a common address
bus and either one can provide an address for memory:

• The Program counter PC points at the address of the next


instruction in the program (used during the fetch phase to
read an instruction).
• The address register AR points at an array of data (used
during the execute phase to read an operand).
• The stack pointer SP points at the top of the stack (used to
push or pop items into or from the stack).

As shown, the initial value of SP can be 4001 and the stack grows
with decreasing addresses. Thus the first element stored in the
stack is at address 4000, the second element is stored at address
3999, and the last address that can be used for the stack is 3000.
Push & Pop Operations in Memory Stack
A new element is inserted with the push operation as follows:
SP ← SP – 1 Decrement stack pointer
M [SP] ← DR Write element on top of the stack

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.

Similarly, an element is deleted with a pop operation as follows:


DR ← M [SP] Read item from the top of stack
SP ← SP + 1 Increment stack pointer

The top element is read from the stack into DR. The stack pointer
is then incremented to point at the next item in the stack.
Practice Set - 03
· Q1 (*MM8-5). Let SP = 000000 in the stack of Fig. 8-3. How
many elements are there in the stack if:
a. FULL = 1 and EMTY = 0?
b. FULL = 0 and EMTY = 1?

· Q2 (MM8-6). A stack is organized such that SP always points at


the next empty location on the stack. This means that SP can be
initialized to 4000 in Fig. 8-4 and the first element in the stack is
stored in location 4000. List the micro-operations for the push and
pop operations.

*Text Book- Computer System Architecture (M. Morris Mano)


Reverse Polish Notation (RPN)
Consider the simple arithmetic expression
A*B+C*D
To evaluate this arithmetic expression it is necessary to
compute the product A * B, store this product while
computing C * D, and then sum the two products.

• The Polish mathematician Lukasiewicz showed that


arithmetic expressions can be represented in prefix
notation. This representation, often referred to as Polish
notation, places the operator before the operands.

• The postfix notation, referred to as Reverse Polish


Notation (RPN), places the operator after the operands.
Reverse Polish Notation (RPN)
The following example demonstrate the three
representations:
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.

The expression A*B+C*D


is written in reverse Polish notation as
AB*CD*+
Conversion to RPN
Consider the expression
(A + B) * [C * (D + E) + F]
To evaluate the expression, we must first perform the
arithmetic inside the parentheses (A + B) and (D + E).
Next we must calculate the expression inside the
square bracket.
Similarly, the last operation is the multiplication of
the two terms between the parenthesis and square
bracket.
NOTE: The expression can be converted to RPN,
without the use of parentheses, by taking into
consideration the operation hierarchy.
The converted expression is
AB+DE+C*F+*
RPN: Operation
It is evaluated as follows:
1. Scan the RPN expression from left to right.
2. When an operator is reached, perform the
operation with the two operands found on the left
side of the operator.
3. Remove the two operands and the operator and
replace them by the number obtained from the result
of the operation.
4. Continue to scan the expression and repeat the
procedure for every operator encountered, until
there are no more operators.
Example: Stack Operations to understand RPN
To completely understand the above concept, go through
the stack operations to evaluate 3 * 4 + 5 * 6

RPN conversion is: 3 4 * 5 6 * +


Practice Set - 04
Q1 (*MM8.7). Convert the following arithmetic expressions from
infix to reverse Polish notation.
(i). A * B + C * D + E * F
(ii). A * B + A * (B * D + C * E)
(iii). A + B * [C * D + E * (F + G)]
(iv). [A * [B + C * (D + E)]] / [F * (G + H)]

Q2 (MM8.9). Convert the following numerical arithmetic


expression into reverse Polish notation and show the stack
operations for evaluating the numerical result.
(3 + 4) [10 (2 + 6) + 8]

*Text Book- Computer System Architecture (M. Morris Mano)

You might also like