Hackasm is an assembler developed as part of Nand2Tetris, which translates programs
written in Hack assembly language into Hack binary code.
The name Hackasm is a play on words because it combines the Hack machine, assembly (ASM) and another word to describe
when a person feels a certain pleasurable sensation...
Sample Hack assembly programs which are used for the Nand2Tetris project are provided in the Programs folder in the E2E test project.
Note
This project is an educational exercise with the goal to be able to translate all the assembly programs provided Nand2Tetris. Therefore, committing time to robustness and production-readiness is not as prioritised, though I have followed practices that have helped me implement the initial assembler in parts as well as keep in mind future development. i.e. automated testing with CI, and separating the assembly passes into different classes.
The assembler is built using C# and is a Console Project.
# Run
dotnet run [FILE]
# Test
dotnet testThere are three stages to the assembly process, which the class names in the project should explain themselves:
- Preprocessing: Removes unneccessary parts that are not required for machine code translation. This includes comments, blank lines and leading/trailing whitespace.
- Label Symbol Resolving: Label symbols are pseudo-instructions which bind to the address of the next instruction in the program. Because label symbols can appear anywhere in the program, especially in previous instructions before the label symbol itself is declared, a separate pass is required to figure out which address these labels bind to. This allows the subsequent translation layer to be able to correctly translate the address when the label is used in another instruction such as a jump or A-Instruction.
- Translation: The actual conversion from assembly to Hack machine code.
These steps along with the file handling are all driven by the project's entrypoint in
Program.cs.