You are on page 1of 21

Computer Organization

Lec # 8 : Stack & Expression Evolution

Bnar Mustafa
bnar.mustafa8@gmail.com

Spring 2022
1
Video of this lecture
• Part 1:
https://www.youtube.com/watch?v=lV-4ylf51yo&t=33s
• Part 2:
https://www.youtube.com/watch?v=X_MRXFFOVr8&t=5s1
• Part 3:
https://www.youtube.com/watch?v=yG1cq7Szdf8&t=22s

2
Stack Implementation
A stack is a storage element that stores information in such a
manner that the item stored last is the first item retrieved., the
point of access called the top of the stack, and the items may be
added to or deleted from the stack by (PUSH), and (POP)
instruction, the operation principle of stack based on (LIFO) Last
Input First to be Output.

Computer Organization - Bnar Mustafa 3


• Push –
This operation results in inserting one operand at the top of the stack
and it decrease the stack pointer register.
• Pop –
This operation results in deleting one operand from the top of the
stack and it increase the stack pointer register. It deletes the data word
at the top of the stack to the specified address.
• Operation type instruction does not need the address field in this CPU
organization. This is because the operation is performed on the two
operands that are on the top of the stack.
The advantages of Stack based CPU organization –
• Efficient computation of complex arithmetic expressions.
• Execution of instructions is fast because operand data are stored in
consecutive memory locations.
• Length of instruction is short as they do not have address field.

Computer Organization - Bnar Mustafa 4


The disadvantages of Stack based CPU organization –
• The size of the program increases.

• Note: Stack based CPU organization uses zero address instruction.


Zero Address Instructions means Address is stored in the opcode, in the
zero address instruction.

• The two operations of a stack are the insertion and deletion of items.
However, nothing is pushed or popped in a computer stack. These
operations are simulated by incrementing or decrementing the stack
pointer register.

• To remove the top item, the stack is popped by reading the memory
word at address 3 and decrementing the content of SP.

Computer Organization - Bnar Mustafa 5


Stack Registers
 Stack Pointer:- Contain the address of the top of the stack, so it will
decremented or incremented its value depending on the operation of
PUSH or POP dependency.

 Stack Base:- Contain the address of the bottom location in the reserved
block.

 Stack Limit:- Contain the address of the other end of the reserved
block.

Computer Organization - Bnar Mustafa 6


Computer Organization - Bnar Mustafa 7
The basic operation of PUSH and POP to be done as follows:
PUSH R1: copy content of R1 to memory addressing by SP
(Similar to STOR) (input to stack)
; M[SP] R1
POP R1: copy content of memory addressing by SP to R1
(Similar to LOAD) (out of stack)
; R1 M[SP]
Where SP was the stack pointer register which holding the top of the
stack address

Computer Organization - Bnar Mustafa 8


Example : Basic stack operation

9
Expression Evolution
Arithmetic Expressions can be written in one of three forms:
• Infix Notation: Operators are written between the operands they operate
on, e.g. 3 + 4.
• Prefix Notation: Operators are written before the operands, e.g + 3 4
• Postfix Notation: Operators are written after operands. 3 4 +

Mathematical formulas are usually expressed in what is known as infix


notation.
Ex: (a+b)
; where the operation appears between two operands
To minimize the use of parenthesis the precedence operations have implied
where generally multiplication and division take precedence over
addition and subtraction.

Computer Organization - Bnar Mustafa 10


Ex: write the following expression in postfix notation (a+b*c)

(a+b) becomes ab+


a+(b*c) becomes abc*+
(a+b)*c becomes ab+c*

The postfix notation used as a guide for generating the machine


instructions.

Computer Organization - Bnar Mustafa 11


Example :- Use the stack procedure to compute this expresion :

y=
Solu:
Firstly transfer the expression to postfix notation to become

yab-cde*+\=

Then, use the stack to implement this formula step by step by pushing
all the identifiers and implementing each operation using two source
operands from top of the stack (pop two operands; perform the
operation and then push the result back to the stack) finally pop the
contains of the stack where the expression have been performed in
it by infix notation

Computer Organization - Bnar Mustafa 12


Computer Organization - Bnar Mustafa 13
The process (Algorithm) of converting infix
notation to postfix notation using the stack
1. Examine the next element in the input
2. If it is an operand, output it
3. If it is an opening parenthesis, push it to the stack
4. If it is an operator, then
•If the TOS is an opening parenthesis, then push the operator.
•If it has higher priority than the TOS (where Multiply and
Division have higher priority than addition and subtraction),
then PUSH the operator
•Else, POP operation from stack to output, and repeat step
5. If it is a closing parenthesis, POP operators to the output until an
opening parenthesis are encountered. POP and discard the opening
parenthesis.
6. If there is more input, go to step1.
7.If there is no more input, unstuck the remaining operator to output.
14
Computer Organization - Bnar Mustafa
Input Output Stack(top on the right)
A+B*C+(D+E)*F empty empty
+B*C+(D+E)*F A empty
B*C+(D+E)*F A +
*C+(D+E)*F AB +
C+(D+E)*F AB +*
+(D+E)*F ABC +*
(D+E)*F ABC*+ +
D+E)*F ABC*+ +(
+E)*F ABC*+D +(
E)*F ABC*+D +(+
)*F ABC*+DE +(+
*F ABC*+DE+ +
F ABC*+DE+ +*
Empty ABC*+DE+F +*
Empty ABC*+DE+F*+ empty

Computer Organization - Bnar Mustafa 15


Example on stack :
Create a stack based program that performs the following operation
X=(2+7*3)
Solution : this operation to be written as infix therefore it must be
transfer to postfix notation firstly:
273*+
X273*+=
Then implement by stack based
PUSH 2
PUSH 7
PUSH 3
MUL
ADD
POP X

Computer Organization - Bnar Mustafa 16


Compare between Stack base and General
Purpose Register Architecture
1. Stack based architecture were used in same earlier computer system for two
reasons:
• Because the operands and destination of the instruction take fewer bits to
encode that they do in general purpose register GPR.
This reduce the amount of memory taken up- by program which was a
significant issue on earlier machine.
• Stack based arch. manage register automatically, freeing programmer from the
need to decide which data should kept in which register file.

2- In GPR arch. the design is based on instruction between (Special purpose


register)(SPR). It becomes dominated in more recent years because of
improvement In technology and the switch to high level language.
As memory capacity have increased and memory costs have decreased the amount
of space taken up by the program has becomes less important more significantly
compilers for GPR arch. are generally able to achieve better performance will
given number of GPR than the stack based arch. 17
Example :
Write a grogram using GPR assume by language that performs
the following computation “assuming that all registers start
out containing 0”;
7+3*5-8
MOV R1,7
MOV R2,3
MOV R3,5
MOV R4,8
MUL R5,R2,R3
ADD R6,R5,R1
SUB R7,R6,R4

Computer Organization - Bnar Mustafa 18


Homework
• write the following expression in postfix notation, then write a
program to perform that computation using general purpose
registers (GPR)

Z = ( A - B) * ( C - D) * [ E + ( F / G)]

Computer Organization - Bnar Mustafa 19


Good luck 

Computer Organization - Bnar Mustafa 20


Attachments

21

You might also like