Skip to content

Latest commit

 

History

History
19 lines (10 loc) · 3.85 KB

README.md

File metadata and controls

19 lines (10 loc) · 3.85 KB

Nand2Tetris-VM Translator

This program translates VM code to assembly instructions for the Hack computer architecture. This project was completed as part of the Nand2Tetris course. This is a free online project based course where the student starts with a fundamental element, the Nand gate, and uses the Nand gate to build a computer architecture. Once the computer itself is built the student then goes on to design an assembler, VM translator, compiler, and operating system for the machine. The VM translator is completed in Chapters 7 and 8 of the course.

What is a Virtual Machine?

A virtual machine is a process that runs inside a host operating system. The virtual machine provides a runtime environment that abstracts elements of the computer architecture and operating system of the host machine. The virtual machine allows software written in a high level programming language to be portable. So long as the target computer architecture and OS have an implementation of the virtual machine installed, the program can run in the virtual machine's runtime environment. This sort of model for compiling programming languages was made popular by Java, which is designed to run in the Java Virtual Machine. This was also adopted by Microsoft as part of their .NET Framework which provides a virtual machine named the Common Language Runtime.

What does the Virtual Machine do?

For the scope of this project the virtual machine acts as a translator of virtual machine code. Programs written in a high level programming language are compiled into VM code by the compiler. The VM translator takes this VM code and translates it into assembly instructions for the host CPU. In more sophisticated virtual machines, code can be compiled during runtime using a method called just-in-time compilation. For this course we were only expected to write a program that translates the VM code prior to the program exectution and stores the resulting assembly code in an output file.

Explaination of the VM Language

VM code is an intermediate language between the high level object oriented Jack programming language and the Hack assembly language. VM code is designed around a stack machine abstraction. The language has two main operations called push and pop which add (push) and remove (pop) data from the stack. Arithmetic operations can be performed on stack elements such as addition, subtraction, and negation. Logical operations can be performed such as and, or, and not. And comparision operations can be performed such as equal to, greater than, and less than. These operations remove the arguments of the operation from the stack and push onto the stack the resulting value such as an integer, or the value of -1 for true and 0 for false.

VM code also provides program control features such as branching using the keywords label, goto, and if-goto. Function calls are implemented using the keywords function, call, and return. Another feature of the language, is the concept of memory segments. The memory segments are sections of memory denoted by their keywords in the VM language: local, argument, this, that, temp, pointer, and static. The memory segments provide a place to store data outside the stack datastructure. So an instruction such as push local 1 would push the data stored in the second element of the local segment to the stack. And an instruction such as pop argument 2 would pop the element at the top of the stack and store it in the 3rd element in the argument memory segment. The constant keyword denotes an integer literal, ie. push constant 5 pushes the value 5 to the stack.

An explaination of the assembly language can be found in my Nand2Tetris-Assembly repository. For a more technical understanding of how the program works read through the provided source code or visit the course webpage: https://www.nand2tetris.org/