The Elements of Computing  Systems / Nisan & Schocken / www.idc.ac.il/tecs 

Project 8:
The VM: Program Control

Objective: Extend the basic VM translator built in Project 7 into a full-scale VM translator. In particular, add the ability to handle the program flow and function calling commands of the VM language.

Resources (same as Project 7): You will need two tools: the programming language in which you will implement your VM Translator, and the CPU Emulator supplied with the book. This emulator allows executing the machine code generated by your VM Translator -- an indirect way to test the correctness of the latter. Another tool that may come handy in this project is the visual VM Emulator supplied with the book. This program allows experimenting with a working VM implementation before you set out to build one yourself. For more information about this tool, refer to the VM Emulator Tutorial.

Contract: Write a full-scale VM-to-Hack translator, extending the translator developed in Project 7, and conforming to the VM Specification, Part II (Section 8.2) and the to the Standard VM-on-Hack Mapping, Part II (Section 8.3.1). Use it to translate the .vm programs supplied below, yielding corresponding .asm programs written in the Hack assembly language. When executed on the supplied CPU emulator, these assembly programs should deliver the results mandated by the supplied test scripts and compare files.

Test Programs

We recommend completing the implementation of the translator in two stages. First implement the program flow commands, and then the function calling commands. This will allow you to unit-test your implementation incrementally, using the test programs supplied below.

For each program Xxx, the  XxxVME.tst script allows running the program on the supplied VM Emulator, so that you can gain familiarity with the program�s intended operation. After translating the program using your VM Translator, the supplied Xxx.tst and Xxx.cmp scripts allow testing the translated assembly code on the CPU Emulator.

Testing the program flow commands implementation:

Program Description Test script

BasicLoop.vm

Computes the sum 1+2+...+n and pushes the result onto the stack. This program tests the implementation of the VM language's goto and if-goto commands. 

BasicLoopVME.tst

BasicLoop.tst

BasicLoop.cmp 

FibonacciSeries.vm

Computes and stores in memory the first n elements of the Fibonacci series.  This typical array manipulation program provides a more challenging test of the VM's branching commands.

FibonacciSeriesVME.tst

FibonacciSeries.tst

FibonacciSeries.cmp

Testing the function calling commands implementation:

Program Description Test script

SimpleFunction.vm

Performs a simple calculation and returns the result. This program provides a basic test of the implementation of the function and return commands.

SimpleFunctionVME.tst

SimpleFunction.tst

SimpleFunction.cmp

FibonacciElement:

Main.vm

Sys.vm

A full test of the implementation of the VM's function calling commands, the bootstrap section and most of the other VM commands. The program directory consists of two .vm files:

  • Main.vm contains one function called fibonacci. This recursive function returns the n�th element of the Fibonacci series;

  • Sys.vm contains one function called init.  This function calls the Main.fibonacci function with n=4, and then loops infinitely.

(The bootstrap code of the VM implementation includes a call to Sys.init).

Since the overall program consists of two .vm files, the entire directory must be compiled in order to produce a single FibonacciElement.asm file. (compiling each .vm file separately will yield two separate .asm files, which is not desired here).

FibonacciElementVME.tst

FibonacciElement.tst

FibonacciElement.cmp

StaticsTest:

Class1.vm

Class2.vm

Sys.vm 

A full test of the implementation's handling of static variables. Consists of two .vm files, each representing the compilation of a stand-alone class file, plus a Sys.vm file. The entire directory should be compiled in order to produce a single StaticsTest.asm file.

StaticsTestVME.tst

StaticsTest.tst

StaticsTest.cmp

 

Tips

Start by creating a directory named projects/08 on your computer and extracting project 08.zip to it (preserving the directory structure embedded in the zip file).

Initialization: In order for any translated VM program to start running, it must include a preamble startup code that forces the VM implementation to start executing it on the host platform. In addition, in order for any VM code to operate properly, the VM implementation must store the base addresses of the virtual segments in the correct locations in the host RAM.  The first three test programs in this project assume that the startup code was not yet implemented, and include test scripts that effects the necessary initializations "manually".  The last two programs assume that the startup code is already part of the VM implementation.

Testing/debugging:  For each one of the five test programs, follow these steps:

  1. Run the program on the supplied VM emulator, using the XxxVME.tst test script, to get acquainted with the intended program's behavior.

  2. Use your partial translator to translate the .vm file(s). The result should be a single text file containing a translated .asm program, written in the Hack assembly language.

  3. Inspect the translated .asm program. If there are visible syntax (or any other) errors, debug and fix your translator.

  4. Use the supplied .tst and .cmp files to run your translated .asm program on the CPU emulator. If there are run-time errors, debug and fix your translator.

Note: The supplied test programs were carefully planned to test the specific features of each stage in your VM implementation. Therefore, it�s important to implement your translator in the proposed order, and to test it using the appropriate test programs at each stage. Implementing a later stage before an early one may cause the test programs to fail.

Tools (same as in Project 7)

The VM emulator: The book's software includes a Java-based VM implementation. This VM emulator was built for one purpose: illustrating how the VM works, using visual GUI and animation effects. Specifically, it allows executing VM programs directly, without having to translate them first into machine language. This practice enables experimentation with the VM environment before you set out to implement one yourself.  Here is a typical screen shot of the VM emulator in action: