A 2-Pass Assembler built in Rust to compile Hack assembly code into binary machine code for the Nand2Tetris Hack computer.
This project implements a two-pass assembler in Rust for the Hack computer system.
It translates Hack assembly (.asm) programs into their corresponding binary (.hack) format.
- First Pass: Resolves labels and updates the symbol table.
- Second Pass: Translates instructions (
A,C, and variables) into binary.
- Converts
.asmfiles into.hackfiles. - Supports variables and labels with a symbol table.
- Strips comments and whitespace automatically.
- Outputs binaries into an
output/directory. - Error handling for invalid instructions.
- Handles multiple input files (up to a configurable maximum).
src/
├── assembler.rs # Core assembler logic
├── parser.rs # Breaks instructions into variants
├── translator.rs # Translates A and C instructions to binary
├── symbol_handler.rs # Manages labels & variables (symbol table)
├── lib.rs # Library entry point
└── main.rs # CLI entry point
git clone https://github.com/youngancient/Hack-Assembler.gitcd hack-assemblercargo build --releasePass one or more .asm files as arguments (Max 10 args)
cargo run -- ./input/Add.asm ./input/Max.asmOR
cargo run -- "./input/Add.asm" "./input/Max.asm"Using the quotes "file_path" helps to prevent the user from mistakenly joining the two or more filepaths together while results in invalid file referencing.
If the input files are in the Root directory of this project, you can reference them directly:
cargo run -- Add.asm Max.asmThe Output .hack files will be created in the output/ directory which is automatically created if it does not exist.