Use of Stacks Assume a Machine has a single register and six instructions (Probl
ID: 3683565 • Letter: U
Question
Use of Stacks Assume a Machine has a single register and six instructions (Problem 2.3.10 from the LAT text.)
LD A places the operand A in the register
ST A places the contents of the register into the variable A
AD A adds the contents of the variable A to the register
SB A subtracts the contents of the variable A from the register
ML A multiples the contents of the register by the variable A
DV A divides the contents of the register by the variable A
Write a program that accepts a postfix expression containing single letter operands and the operators +, -, *, and / and prints a sequence of instructions to evaluate the expression and leaves the result in the register. Use variables of the form TEMPn as temporary variables. For example, using the postfix expression ABC*+DE-/ should print the following: Note: YOU CANOT USE STACK BUILD IN FUNCTION, define own stack.
LD B
ML C
ST TEMP1
LD A
AD TEMP1
ST TEMP2
LD D
SB E
ST TEMP3
LD TEMP2
DV TEMP3
ST TEMP4
Use the following postfix expressions for input
AB+C-
AB-C+DEF-+$
ABC-/BA-+/
ABCDE-+$*EF*
Explanation / Answer
ST TEMP1 # TEMP1 = REGISTER
LD C # REGISTER = C
AD TEMP1 # REGISTER = REGISTER + TEMP1
ST TEMP2 # TEMP2 = REGISTER
LD E # REGISTER = E
SB F # REGISTER = REGISTER-F
ST TEMP3 # TEMP3 = REGISTER
LD D # REGISTER = D
AD TEMP3 # REGISTER = REGISTER+TEMP3
ST TEMP4 # TEMP4 = REGISTER
LD TEMP2 # REGISTER = TEMP2
DV TEMP4 # REGISTER = REGISTER/TEMP4
ST TEMP5 # TEMP4 = REGISTER
ABC-/BA-+/ CORRECTED BY ABC-/DE-+/ SURPLUS The '/'
LD B # REGISTER = B
SB C # REGISTER = REGISTER-C
ST TEMP1 # TEMP1 = REGISTER
LD A # REGISTER = A
DV TEMP1 # REGISTER = REGISTER/TEMP1
ST TEMP2 # TEMP2 = REGISTER
LD D # REGISTER = D
SB E # REGISTER = REGISTER-E
ST TEMP3 # TEMP3 = REGISTER
LD TEMP2 # REGISTER = TEMP2
AD TEMP3 # REGISTER = REGISTER+TEMP3
ST TEMP4 # TEMP4 = REGISTER
ABCDE-+/*EF* CORRECTED BY '$' POR '/' AND FAILURE '/' ABCDE-+/*EF*/
LD E # REGISTER = E
SB E # REGISTER = REGISTER-E
ST TEMP1 # TEMP1 = REGISTER
LD C # REGISTER = C
AD TEMP1 # REGISTER = REGISTER+TEMP1
ST TEMP2 # TEMP2 = REGISTER
LD B # REGISTER = B
DV TEMP2 # REGISTER = REGISTER/TEMP2
ST TEMP3 # TEMP3 = REGISTER
LD A # REGISTER = A
ML TEMP3 # REGISTER = REGISTER*TEMP3
ST TEMP4 # TEMP4 = REGISTER
LD E # REGISTER = E
ML F # REGISTER = REGISTER*F
ST TEMP5 # TEMP5 = REGISTER
LD TEMP4 # REGISTER = TEMP4
DV TEMP5 # REGISTER = REGISTER/TEMP5
ST TEMP6 # TEMP6 = REGISTER
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.