Workshop In Computer Construction - From Nand to Tetris
Project 7 - The Virtual Machine (part 1)
Deadline April 4th, 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:
Stack Arithmetic:
Program Description Test file Compare file
Simple Add Addition of two constants. SimpleAdd.tst SimpleAdd.cmp
Stack Test A sequence of arithmetic and logical operation. StackTest.tst StackTest.cmp

Memory Access:
Program Description Test file Compare file
Basic Test Pop and Push operations, to and from various memory segments. BasicTest.tst BasicTest.cmp
Static Test Pop and Push operations, to and from the static segment. StaticTest.tst StaticTest.cmp
Pointer Test Pop and Push operations, using the pointer segment. PointerTest.tst PointerTest.cmp

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.
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 contains all your source files (well documented !), the VMTranslator shell script (if needed), the makefile and a README.
Resources
  • project7.zip (download): Extract to an empty directory, and keep the default directory structure as is.
  • Chapter 7 of the book - "The Virtual Machine".
  • VM Emulator Tutorial (PPS,HTML,Software)
  • CPU Emulator Tutorial (PPS,HTML,Software)
  • Assembler Tutorial (PPS,HTML,Software)
  • Guidelines.