CS 23001 Computer Science II: Data Structures & Abstraction
Project #3

Convert an infix expression into assembler.

Assembler languages are the simplest of programming languages. It is very close to what the CPU reads to execute programs. Assembly can be directly converted into machine instructions in hexadecimal and executed by the CPU. All high level programming languages (e.g., C++, Pascal) are converted into assembly through the compiling process. The final phase of the compiler is code generation. This programming project will give you a feel for the solution to (a small part of) the code generation process.

Here you are given a simple CPU with only one register and six instructions (akin to RISC architectures). The instructions (or commands) can only work with the register and one operand (parameter). The table below describes the instruction set. The instructions act on the single register. The register is the only place were operations can be executed. That is, it is the only "variable" to which you can add, subtract, mulitply, or divide. You can not add two memory locations together; the contents of a location must be loaded into the register and the contents of a location is then added to the register. The results of this computation can then be stored to some memory location.

Instruction set:
LD opn Loads operand into the register.
ST opn Stores the contents of register to operand.
AD opn Adds contents of operand to register.
SB opn Subtracts.
MU opn Multiplies.
DV opn Divides.

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:

Opcode Operand Comment
LD B Load in B.
MU C B * C.
ST TMP1 Save results of B * C.
LD A Load A.
AD TMP1 Add A to B * C.
ST TMP2 Save result.
LD D Load D.
SB E D - E.
ST TMP3 Save result.
LD TMP2 Get A + B * C.
DV TMP3 Divide it by D - E.
ST TMP4 Save result, also still in register.

Your program output should be:
Infix Expression: ( ( AX + ( BY * C ) ) / ( D4 - E ) )
Postfix Expression: AX BY C * + D4 E - /

   LD     BY
   MU     C
   ST     TMP1
   LD     AX
   AD     TMP1
   ST     TMP2
   LD     D4
   SB     E
   ST     TMP3
   LD     TMP2
   DV     TMP3
   ST     TMP4


URL: http://www.cs.kent.edu/~jmaletic/CS33001/programs/proj3.html
Last update: Mon Oct 21 17:42:51 2019 EST