Skip to content

Assembler, VM-Translater, Compiler and OS of HackMachine (nand-to-tetris) written in go

Notifications You must be signed in to change notification settings

Keisn1/nand-to-tetris-in-go

Repository files navigation

From Nand To Tetris

Disclaimer

!!! All the Material (lectureNotes, instructions, tutorials etc.) was provided from the Team at NandToTetris and can be found on their webpage https://www.nand2tetris.org/ !!!

I am extremely thankful to all of the persons behind FromNandToTetris for their great work and for making this amazing course publicly available for everyone to study. And I don’t claim any credit other than for completing the projects myself. I re-share these materials here only for educational purposes and making this repo more readable and understandble.

From Nand to Tetris

imgs/nandToTetrisBigPicture.png

“Nand to Tetris” is a project-based course that teaches computer science students how to build a modern computer system from first principles.

Participants start by creating basic logic gates using the Nand gate and gradually progress to designing and implementing a fully functional computer system, including a CPU and an operating system.

The course covers various topics in computer architecture, software development, and digital design, providing a hands-on understanding of how computers work from the ground up.

Table of Contents

The Projects

To each of the projects you will find additional explanations inside the corresponding directory.

Project 01

We are building the Basic Logic Gates:

  • Not, Not16, Or, Or16, Or8Way, And, And16, Xor, Mux, Mux16, Mux4Way16, Mux8Way16, DMux, DMux4Way, DMux8Way

Project 02

We are doing some math with the Gates that we just built, finally building our ALU

  • HalfAdder, FullAdder, Inc16, Add16, ALU

Project 03

Our Chips discover the notion of time and there they will be able to remember (at least the state that they have been in the last second).

We are building:

  • Bit, PC, RAM8, RAM64, Register, RAM512, RAM4K, RAM16K

Project 04

We are discovering the architecture of the Hack Computer by getting to know its instruction set and the corresponding Machine Language.

We are writing first programs for our computer:

  • add, fill, flip, for_loop_with_pointer, keyboard, mult, screen, signum, sum1Ton

project_05

Now we are getting to know the actual intracacies of how to put together Chips to a Computer.

We talk about Fetch and Execute cycles and the actual architecture of the Hack Computer.

The project is to actually build the computer in Hardware Description Language.

project 06

In Week 6 of the “Nand to Tetris” course, participants work on building an assembler, which is a crucial component that translates symbolic assembly language (Hack Machine language) into binary machine code (Hack instructions).

This assembler plays a significant role in the overall process of creating a functioning computer system from scratch.

Project 07/08 - Virtual Machine Translator

In Week 7 and 8 of the “Nand to Tetris” course, students typically focus on implementing a virtual machine (VM) and a compiler.

During these weeks, participants learn how to design and build a virtual machine that executes a stack-based language and develop a high-level language compiler that translates a high-level language into the VM’s low-level language.

This part of the course delves into the principles of programming languages and compiler construction, providing a comprehensive understanding of software development processes.

Project 09 - Hack Programs

Project 10/11 - The Hack Compiler

Project 12 - HackOS

Go Packages

Install Assembler as a go program

make install-assembler

Usage

hack_assembler Max.asm

or

hack_assembler --out new.hack Max.asm

VM-translater

Compiler

Build / Install everything

Note on Development Process

Test Driven Development

For all of the software projects built in go, I adopted a Test-Driven Development (TDD) approach to refine my implementation.

Given the nature of the course and the supplied material it was easy defining comprehensive test cases to validate desired behavior first (translation into MachineCode/assembly; correctly parsing command lines ).

Before moving on to the next case, I ensured that each unit of code functioned correctly. This iterative testing methodology helped me identify and address potential issues early in the development cycle and allowed me to apply refactoring at each step.

Assembler

For both, the Assembler and the VM translator, propose a 2 tier design of a Parser and CodeWriter.

For the assembler I finally didn’t follow that advice and implemented the Assembler in one object. The assemble process consists of two stages. A first pass to get all Labels and a second one to actually translate assembly to machine code instructions.

VM Translator

The VM Translator consists of a Parser and a CodeWriter where I basically follow the proposed implementation design of the code authors.

For the VM translator, I employed Go’s text/template package to streamline the generation of code templates, making the translation process smoother and more structured.

In implementing the VM translator in Go, I leveraged the power of the language by making use of the “embed” feature, which allowed me to efficiently include the necessary VM translation files directly into the executable binary.

About

Assembler, VM-Translater, Compiler and OS of HackMachine (nand-to-tetris) written in go

Resources

Stars

Watchers

Forks

Packages

No packages published