Workshop In Computer Construction - From Nand to Tetris
Project 8 - The Virtual Machine (part 2)
Deadline April 11th, 2003 at Ross closing time
Description

Objectives: Develop the Stack Arithmetic and Memory Access parts of the VM Translator, according to the specification and implementation notes in sections 6.2 and 6.3. The VM Translator should implement the Virtual Machine on the Hack computer platform.

The contract: Write the VM Translator program. Use it to compile all the .vm test programs supplied below, yielding corresponding .asm programs written in assembly. When executed on the CPU Emulator, the .asm programs that your Translator produces should deliver the results mandated by the supplied test scripts and compare files.

Implementation tips: The best way to implement the Translator is to start with a program that has limited capabilities, test it, and then extend it with additional capabilities. In particular, the implementation of your partial VM Translator should be done in two stages:

  • Stack Arithmetic
  • Memory Access
For each stage, we supply test programs designed to test the specific functionality of the respective part of the VM Translator. In each one of the two stages, you should test your work as follows:
  1. Use your partial VM Translator to compile the supplied sample .vm program. This compilation should produce a text file containing a translated .asm program, written in the Hack assembly language.
  2. Inspect the translated program (text file). If there are problems, debug your VM Translator.
  3. Use the supplied .tst and .cmp files to run your translated .asm program on the CPU Emulator. If there are run-time problems, something is screwed up with your VM Translator. The fact that each one of the two stages tests a very well-defined part of the Translator should ease the debugging task.

Important note: The test programs that we supply were carefully planned to test the specific features of each part. Therefore, it's important to implement your VM translator in the proposed order, and to test it using the appropriate VM programs at each stage. Implementing a later stage before an early one may cause the test programs to fail.

Test programs:
Program Flow:
Program Description Test file Compare file
Basic Loop Simple test of goto and if-goto. Computes the sum 1+ 2 + ... + n and pushes the result unto the stack. BasicLoop.tst BasicLoop.cmp
Fibonacci Series A more complex test. Computes and stores in memory the first n elements of the Fibonacci series to the memory using loops. FibonacciSeries.tst FibonacciSeries.cmp

Function Calls:
Program Description Test file Compare file
Simple Function Pop and Push operations, to and from various memory segments. SimpleFunction.tst SimpleFunction.cmp
Fibonacci Element
Sys.vm, Math.vm
Ppushes the fourth fibonacci element onto the stack. FibonacciElement.tst FibonacciElement.cmp
Static Test
Sys.vm, Class1.vm, Class2.vm
Setting and getting values of static variables in two different classes. StaticTest.tst StaticTest.cmp
Notice that for the last two exercises you will have to add the bootstrap code as described in the chapter.

Requirements: You should submit an executable called VMTranslator (or shell script with that name, in case you write in Java) with the following usage: .

  • VMTranslator <file> - The VMTranslator translates the vm file to an asm file with the same name. (To be clear running VMTranslator add.vm will create add.asm.
  • VMTranslator <dir> - The VMTranslator translates all the .vm files in that directory to a single asm file with the name of the directory and sibling to it in the parent directory. (Running VMTranslator somedir on the directory somedir containing the vm files a.vm and b.vm, will create somedir.asm.
Your code should be based on the code you wrote for project 7. If there is any problem to do so, contact the TA ASAP. You should also supply a makefile to compile your code. Keep your code well organized and documented, as you would in every programming project.

Submission Submit a tar file (created by `tar cvf`) contains all your source files (well documented !), the VMTranslator shell script (if needed), the makefile and a README.
Resources
  • project8.zip (download): Extract to an empty directory, and keep the default directory structure as is.
  • Chapter 8 of the book - "The Virtual Machine".
  • VM Emulator Tutorial (PPS,HTML,Software)
  • CPU Emulator Tutorial (PPS,HTML,Software)
  • Assembler Tutorial (PPS,HTML,Software)
  • Guidelines.