Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your problem is to convert a given infix expression into a sequence of assembly

ID: 3801611 • Letter: Y

Question

Your problem is to convert a given infix expression into a sequence of assembly instructions that evaluates the expression and leaves the result in the register. You will do this by using postfix expressions. First, you will convert a given infix expression into the corresponding postfix expression. In the second step, you will convert postfix to the required sequence of assembly instructions. You will read the infix expression from a file, and write the result to another file.

For example,
given the following infix expression:
( ( A + ( B * C ) ) / ( D - E ) )

we get the postfix expression:
A B C * + D E - /

This results in an assembly program that looks like:



Your program output should be:



Requirements:

You CANNOT use std::string and must construct your stack using a linked list you build.

Implementation:

Create a generic (template) ADT stack class.

Implement using a linked list.

You must implement a destructor, copy constructor, constant time swap, and assignment.

You will need to have a stack of strings.

Use the following files as a starting point:

-----------------------------------------------------------------------

stack.hpp

-----------------------------------------------------------------------

Opcode Operand Comment LOAD B Load in B. MULR C B * C. STOR TMP1 Save results of B * C. LOAD A Load A. ADDR TMP1 Add A to B * C. STOR TMP2 Save result. LOAD D Load D. SUBR E D - E. STOR TMP3 Save result. LOAD TMP2 Get A + B * C. DIVR TMP3 Divide it by D - E. STOR TMP4 Save result, also still in register.

Explanation / Answer

#ifndef STACK_HPP #define STACK_HPP #include const int MAX_VALUES = 256; template class stack { private: int tos; type values[MAX_VALUES]; public: stack() : tos(-1) {} /** @pre non-full stack */ void push(const type & object) { assert(!full()); ++tos; values[tos] = object; } /** @pre non-empty stack */ void pop() { assert(!empty()); --tos; } /** @pre non-empty stack */ type top() const { assert(!empty()); return values[tos]; } /** @pre non-empty stack */ type & top() { assert(!empty()); return values[tos]; } bool empty() const { return tos == -1; } bool full() const { return tos == (MAX_VALUES - 1); } }; #endif