Laboratory assignment · 24 September
Lab 7: VM Translator, Part I
Build the first part of a VM-to-Hack translator in Python, covering stack arithmetic, logical operations, and memory-access commands.
Back to course homeObjective and contract
Translate VM code into Hack assembly
The generated assembly must satisfy the standard VM mapping on the Hack platform.
Xxx.vm becomes Xxx.asm
When the generated assembly runs on the CPU emulator, it must produce the results required by the supplied test scripts and compare files.
- Program
- Write
VMTranslator.pyin Python. - Input
- One valid
.vmfile containing commands covered by this project. - Output
- A Hack assembly-language file with the same base name and the extension
.asm. - Example
python VMTranslator.py BasicTest.vmmust generateBasicTest.asm.
Implementation plan
Build and test in two stages
Complete each stage before depending on it in the next one.
Stack arithmetic and logic
Implement push constant x and all nine arithmetic/logical commands:
add,sub, andnegeq,gt, andltand,or, andnot
Memory access
Implement the complete push segment i and pop segment i commands for all eight VM segments:
constantlocal,argument,this, andthatpointerandtempstatic
Recommended Stage II order
- Reuse the already implemented
constantsegment. - Implement
local,argument,this, andthat. - Implement
pointerandtemp, including changes to the bases ofthisandthat. - Implement
static.
Test programs
Five focused tests
Each program targets the commands introduced at a particular implementation stage.
SimpleAdd
Pushes two constants and adds them. Use it as the first test of stack manipulation and add.
StackTest
Executes a sequence that exercises all arithmetic and logical commands.
BasicTest
Tests push and pop with the principal virtual memory segments.
PointerTest
Tests pointer, this, and that, including modification of the segment bases.
StaticTest
Tests translation and use of the static segment.
For each program Xxx, use Xxx.vm as input. The supplied XxxVME.tst demonstrates its intended VM behavior; Xxx.tst and Xxx.cmp test your translated assembly on the CPU emulator.
Workflow
Obtain, translate, and test each program
Use the VM emulator to understand the source program and the CPU emulator to verify your translation.
Implementation steps
- Open the Nand2Tetris VM Emulator and choose Load Files.
- Open
projects/07. It contains the five test-program folders listed above. - Enter a folder—for example,
BasicTest—selectBasicTest.vm, and copy or download it to your computer. - Run your translator:
It must producepython VMTranslator.py BasicTest.vmBasicTest.asm. - Repeat for the remaining programs at the appropriate implementation stage.
Testing and debugging each program
- Run
Xxx.vmin the VM emulator withXxxVME.tstto understand its intended behavior. - Translate
Xxx.vmwith your current translator to produceXxx.asm. - Inspect the assembly output for visible syntax, label, or control-flow errors.
- Load the generated assembly in the CPU emulator and run the supplied
Xxx.tsttest script. - Compare the result with
Xxx.cmp. If it fails, correct the translator and generate the assembly again.
Follow the prescribed order. Later tests depend on features exercised by earlier tests. Implementing later stages first can make failures difficult to diagnose.
Important for this lab
Initialization is supplied by the tests
Bootstrap code and segment initialization belong to the next VM-translator project.
A complete VM implementation normally begins with startup code that initializes the stack pointer and establishes the virtual segments' base addresses. You do not have to generate that startup code in this lab. The supplied Project 7 test scripts manually perform the initialization required by these five programs.