This project implements an assembler for the Hack computer from "The Elements of Computing Systems" (Nand2Tetris). The assembler translates Hack assembly language into 16-bit binary code, handling both symbol-less and symbol-rich programs using a two-pass process.
- assembler.py: Main Python script that implements the assembler.
- Parser Module: Reads and parses the input
.asmfile, removing comments and whitespace. - Code Module: Contains lookup tables and functions for converting assembly mnemonics (
comp,dest,jump) into binary codes. - SymbolTable Module: Manages symbols (labels and variables) and maps them to their corresponding addresses.
- test_code.py: Unit tests for verifying that the Code module functions correctly.
-
Assembly Process:
To assemble a Hack assembly file, run:python assembler.py <inputfile.asm> <outputfile.hack>
For example:
python assembler.py Add.asm Add.hack
-
Unit Testing:
Run the unit tests using:python -m unittest test_code.py
-
Comparison Testing:
I verified that the output.hackfiles match exactly with the reference output generated by the supplied Nand2Tetris assembler using the compare feature. -
CPU Emulator Testing:
I loaded the generated.hackfiles into the Hack CPU Emulator to confirm that the programs execute correctly on the simulated hardware.
You can view the complete source code, tests, and documentation in this repository.
This project is based on the work described in The Elements of Computing Systems by Noam Nisan and Shimon Schocken, MIT Press. The project is part of the Nand2Tetris course curriculum.