Saturday, September 17, 2011

Simple Language Compiler

One of our projects that I have worked on for my Advanced Computer Science class at the Governor's School is a simple machine language assembler and compiler.

For the first part of my project, I coded an assembler that takes in four digit numeric commands and executes them. It can store up to a hundred lines of code, variables, and constants.

There is a set of instructions available for the assembler. They are very simple commands, hence the name of the project. The instructions are as follows:

10 Read – reads a word from the keyboard into the specified location in memory
11 Write – writes a word from the specified location in memory to the monitor screen
20 Load – loads a word from the specified location in memory to the accumulator
21 Store – stores a word from the accumulator into the specified location in memory
30 Add – adds a word from the specified location in memory to the accumulator
31 Subtract – subtracts a word from the specified location in memory from the accumulator
32 Divide – divides a word from the specified location in memory into the accumulator
33 Multiply – multiplies a word from the specified location in memory by the accumulator
40 Branch – sets the instruction pointer to go to the specified location in memory. ( branches or jumps)
41 BranchNegative – branches to the specified location in memory if the accumulator is negative
42 BranchZero - branches to the specified location in memory if the accumulator is zero.
43 HALT - the program execution is stopped.
For example, 1090 instruction orders the machine to get input from the keyboard and store it into address 90.

Once this was coded, my next task was to create a compiler that would take human language and covert it into machine-level language that make up the instructions that are passed into the assembler.

The compiler has several instructions that can be passed:


rem: Basically a comment function

input: Reads input from the keyboard. Serves the same purpose as the machine-level instruction 10.

print: Prints a variable onto the screen so that the user can read. Serves the same purpose as the machine-level instruction 11.

let: Computes a mathematical expression and makes it equal to the specified variable.

goto: Branches to a specific line denoted by its number.

if: Mandates a specific condition to be met in order to branch to the specified line.

end: Ends the program.
Bug testing and showing appropriate error messages were painful to accomplish. The let function of the compiler was also very difficult to manage, but it worked out in the end.

Here is the link that takes you to the code for the compiler, if any of you are interested in taking a look: CLICK HERE!

In case it wasn't clear as to what this program actually does:  The Simple Language Compiler is something like BlueJ (java), Microsoft Visual Studio compiler (C), and even basic command prompt compiler batches.  You can type commands in a text file and put that into the compiler.  The compiler will then parse through your code and write it out in a language that the machine, or the computer, can read and execute.

Anyways, thanks for reading! 

No comments:

Post a Comment