1. Evaluate a short algebraic expression using code with three-operand instructi
ID: 3761693 • Letter: 1
Question
1.
Evaluate a short algebraic expression using code with three-operand instructions. The expression should have a minimum of three operands and 2 operators. Operands may be alpha or numeric. You may only use registers A through F, plus X and T. Registers A through F may not be changed, i.e. their values are fixed. Register T may be used as a temporary register, and Register X must contain the final answer. Show the postfix for the expression, and then use a stack to evaluate the expression.
***It is important to note that operational hierarchy dictates that:
we first perform all arithmetic inside inner parentheses;
then inside outer parentheses;
then do multiplication and division operations before addition and subtraction operations.
******This is an example provided by the teacher, consider X=A +B + C * D
The three-operand instructions would be:
ADD X, A, B
MUL T, C, D
ADD X, X, T
The Postfix would be AB+CD*+
The stack would be:
PUSH / A onto the stack
PUSH / B onto the stack
ADD /pop A & B, add them, and push the answer back on the stack
PUSH /C
PUSH / D
MUL /pop C & D, multiply them, and push the answer back on the stack
ADD / pop X & T, add them, and push the answer back on the stack
POP / X off the stack************** end of example provided by teacher
Please don’t use the example above just solve a different algebraic expression.
Explanation / Answer
Conisder this Infix Expression : (A +B * C) / (D + E * F)
NOTE POINTS:
1) Infix Expression Contains Operators between Operands.Example A+B.Here A and B are Operands and + (plus) is Operator.
2)In Postfix Expression Operator is Placed at the end of Operands .Example AB+ (its Infix is A+B).
3)In this example symbol ' * ' referrs to Multiplication Operation.
Three-Operand Instructions for Postfix Expression:
Infix Expression :( A + B * C ) / ( D + E * F)
MUL X,B,C
ADD X,X,A
MUL T,E,F
ADD T,T,D
DIV X,X,T
The Postfix Expression would be : X = A B C * + D E F * + /
NOTE POINT:
consider this code MUL X,B,C
here X is destination register , the operation is performed like this X <--- B * C , Product of B and C is Stored in X.
Evaluation of Postfix Expression using Stack:
NOTE POINT:
Stack is a Data Structure which follows FIRST IN LAST OUT Policy.Stack contains 2 operations PUSH AND POP.The operation involves insertion of element into the Stack and Pop involves deletion of element from the Stack.
Postfix Expression : X = A B C * + D E F * + /
The Stack would be:
PUSH A
PUSH B
PUSH C
MUL / pop B and C , multiply them, and push the answer back on the stack .
ADD / pop A and also the value which is on the top of the Stack that is Product of B & C add them, and push the answer back on the stack
PUSH D
PUSH E
PUSH F
MUL / pop E and F , multiply them, and push the answer back on the stack .
ADD /pop D and also the value which is on the top of the Stack that is Product of E & F add them, and push the answer back on the stack
DIV / Division operation
POP / pop the value on the top of the Stack and store in Register X
EXAMPLE :
let Register values as A = 8 , B = 2 , C = 5 , D = 3 , E = 3 , F = 2
Infix Expression : 8+2*5/3+3*2
Postfix Expression :825*+332*+/
After evaluation of the Postfix Expression we got 2 as result which is placed on top of the Stack .
STEPS REMAINING STRING INSTRUCTION STACK 1 8 2 5 * + 3 3 2 * + / PUSH 8 8 2 2 5 * + 3 3 2 * + / PUSH 2 8,2 3 5 * + 3 3 2 * + / PUSH 5 8,2,5 4 *+332*+/ MUL 8,10 5 +332*+/ ADD 18 6 332*+/ PUSH 3 18,3 7 32*+/ PUSH 3 18,3,3 8 2*+/ PUSH 2 18,3,3,2 9 *+/ MUL 18,3,6 10 +/ ADD 18,9 11 / DIV 2Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.