An assembler for an HDL-like symbolic code called the Hack machine language described in the Elements of Computing Systems (Nand2Tetris). The assembler, written in C converts symbolic programs into their binary representations in ASCII for the Hack computer.
The Hack Assembler uses input and output redirection, forcing the standard input stream to read from .asm
source files and the standard output to write to .out
files.
The assembly process consists of five main components:
- Tokenizer Module: Breaks input into meaningful symbols.
- Parser Module: Analyzes the syntax of the instructions.
- Symbol Table Module: Manages labels and variable symbols.
- Scanner Module: Reads and resolves labels and variable symbols.
- Code Module: Translates instructions into binary.
The A-instruction sets the A register to a given value.
- Symbolic syntax:
@value
(wherevalue
is a non-negative constant or symbol). - Binary syntax:
0vvvvvvvvvvvvvvv
(15-bit binary representation ofvalue
). - Example:
@21
→0000000000010101
The C-instruction performs computation and optionally stores the result or jumps.
- Symbolic syntax:
dest = comp ; jump
- Binary syntax:
111 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
- Components:
comp
(computation)dest
(destination of result)jump
(optional control flow change)
- Example:
D=M+1
→1110011111010000
Ensure you have gcc
installed. Then, compile the assembler using:
make
This generates an executable called assembler
.
To assemble a Hack assembly file (.asm
), use:
./assembler < input.asm > output.out
This converts input.asm
into a binary machine code file output.out
.
To remove the compiled assembler:
make clean
- Book: Elements of Computing Systems
- Website: Nand2Tetris
- Digital HDL Tools:
- Nand2Tetris VS Code Plugin
This project is licensed under the MIT License – see the LICENSE file for details.