A small assembler for the Hack computer from the nand2tetris course.
It translates Hack assembly (.asm) into Hack machine code (.hack). The project also includes a simple wxWidgets GUI for loading/pasting code, assembling it, previewing the output, and saving the result.
About this repo: This started as a student project while learning nand2tetris.
- Assemble Hack assembly (
.asm) → machine code (.hack) - Classic two-pass assembler
- Pass 1: collect labels
(LOOP)and resolve ROM addresses - Pass 2: translate A/C instructions and allocate variables
- Pass 1: collect labels
- Symbol table
- Predefined symbols:
SP,LCL,ARG,THIS,THAT,R0..R15,SCREEN,KBD - User labels:
(LABEL) - User variables: allocated starting at
RAM[16]
- Predefined symbols:
- GUI
- open
.asmfile or paste code - assemble
- preview
.hackoutput - save output
- shows a simple symbol-table dump (
SymbolTable.txt)
- open
Main pipeline is implemented in Assembler.cpp:
- Cleanup (
Assembler::asmEdit)- removes comments (
// ...) - removes spaces
- stores non-empty lines in a queue
- removes comments (
- First pass (
Assembler::firstPass)- detects labels like
(LOOP) - stores
label -> ROM address - produces a second queue without label lines
- detects labels like
- Second pass (
Assembler::secondPass)- translates each instruction via
Parser::parseInst - writes 16-bit machine code to the output file
- translates each instruction via
@123→0+ 15-bit value@symbol→ resolved with the symbol table- new variables are allocated starting at address 16
Encoded as:
111+comp(7 bits) +dest(3 bits) +jump(3 bits)
The lookup tables for comp, dest, jump are implemented in Parser.cpp.
Assembler.*— two-pass assembler pipeline + symbol dump (SymbolTable.txt)Parser.*— parsing A/C instructions + comp/dest/jump dictionariesSymbolTable.*— predefined symbols + label/variable allocationMainFrame.*,App.*— wxWidgets GUItestAsm/— small example programs for manual testing
This repository contains a Visual Studio project:
V45k3 - Assembler.vcxproj
General steps:
- Open the
.vcxprojin Visual Studio - Install/configure wxWidgets for your toolset
- Build and run
Example input programs can be found in testAsm/ (Counting to 10, Fibonacci, etc.).
- Hack is intentionally small; some computations do not exist.
- Example:
testAsm/Parity of number.asmcontainsD=D%Aas a reminder/test case.
- Example:
- The GUI currently writes temporary files into the working directory (e.g.
temp.asm,output.hack,SymbolTable.txt).
MIT — see LICENSE.