Skip to content

Complete Hack language assembler implementation

License

Notifications You must be signed in to change notification settings

vasll/hassembler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hack-assembler

A hack machine language assembler made for the nand2tetris course with python and Go.

Usage

usage: hassembler [-h] file [outfile]

positional arguments:
  file        path of the input .asm file
  outfile     path of the output .bin file

options:
  -h, --help  show this help message and exit

How does it work?

The hack platform from nand2tetris has two basic A instructions and C instructions that need to be parsed into binary.
The parsing of these instructions is done with different symbol tables consisting of a symbol and a binary value. Each line is read from the file and split into its various fields, then each field is parsed using the symbols table and finally written to the output file.

image

A instructions

They always start with a '@' and change the value of the Address register, they can point to:

  • A predefined symbol like a register @R15, @R2, @THIS, @KBD, @SCREEN, ...
  • A label @main_loop, ...
  • A memory address @1254, @56, ...

C instructions

They can be of two types and perform computation on the ALU like adding, subtracting, anding, comparisons, flipping bits and so on:

  • "dest=comp" D=M, D=D-A, ...
  • "comp;jump" 0;JMP, D;JGT, ...