Skip to main content
Library homepage
Engineering LibreTexts

4: Assembler Program

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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.

    Figure 4-1: Assembly Process

    Screen Shot 2020-07-03 at 11.46.38 AM.png

    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.

    Figure 4-2: Assembler overview

    Screen Shot 2020-07-03 at 11.48.14 AM.png

    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, 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.

    This page titled 4: Assembler Program is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.