The assembler is the program that translates an assembly language program into machine code. The assembler will read a source assembly program consisting of assembly instructions. The assembler will then write two files which will be used to run the program in the Logisim CPU.
The first output file contains the machine code segment containing the machine code program that will be used in the CPU. This is a translation of the assembly instructions into machine instructions. The second output file contains the initialized data segment that will be used in the CPU. This is illustrated in the following figure.
The assembler is a two-pass assembler. A two-pass assembler reads the input file twice, or in 2 passes from the start to the end of the source assembly file. The first pass will calculate an address for each label in the program to create a symbol table. A symbol table is a list that contains labels in the program and their address in memory. The second pass will translate each instruction in the input file into machine code, and write out a file that corresponds to machine code for the data and the text segments for the program. The second pass through the file uses the symbol table to resolve any label references that are used in assembly instructions. The format of the program is shown in the following figure.
The assembler is written in Java. There is no particular reason or advantage to writing this assembler in Java. The major issue in the assembler is the writing of call backs or virtual methods/functions to parse the individual instructions. These would probably be easier in C/C++, and definitely faster using function pointers instead of polymorphism. But the concept of a call back is difficult in any language for readers not familiar with the concept, and this assembler is more than fast enough.
The assembler uses static initializers with factory patterns and singleton objects to implement the polymorphic call back structure for processing the commands. This could be done in a large “if” statement, but this polymorphic solution is cleaner and easier to extend. It is also instructive for readers who are not familiar with the factory pattern, singleton pattern, and static initializers to see them. But the assembler is a relatively short and straight forward program, and readers are welcome to rewrite it using any structure or language they prefer.
The assembler can be obtained from the author's web site, http://chuckkann.com. The zip file contains the assembler, the circuit definition file for the one-address CPU, and some programs and files for the user to text and work with the CPU.