Startup Code
A VM program is normally assumed to contain at least two functions: Main.main and Sys.init. When the VM translator translates a VM program, it generates machine language code that sets the stack pointer to 256 and then calls the Sys.init function, which then calls Main.main. In a similar fashion, when the VM emulator is instructed to execute a VM program (collection of one or more VM functions), it is programmed to start running the Sys.init function, which is assumed to exist somewhere in the loaded VM code. If a Sys.init function is not found, the emulator is programmed to start executing the first command in the loaded VM code.
The latter convention was added to the emulator in order to assist the gradual development of the VM implementation, which spans two chapters in the book. In chapter 7, we build only the part of the VM implementation that deals with pop, push, and arithmetic commands, without getting into subroutine calling commands. Thus, the test programs associated with Project 7 consist of “raw” VM commands without the typical function/return wrapping. Since we wish to allow informal experimentation with such commands, we gave the VM emulator the ability to execute “raw” VM code which is neither properly initialized nor properly packaged in a function structure.
Virtual Memory Segments
In the process of simulating the virtual machine’s operations, the VM emulator manages the virtual memory segments of the Hack VM (argument, local, etc.). These segments must be allocated to the host RAM—a task that the emulator normally carries out as a side effect of simulating the execution of call, function, and return commands. This means that when simulating “raw” VM code that contains no subroutine calling commands, we must force the VM emulator to explicitly anchor the virtual segments in the RAM—at least those segments mentioned in the current code. Conveniently, this initialization can be accomplished by script commands that manipulate the pointers controlling the base RAM addresses of the virtual segments. Using these script commands, we can effectively put the virtual segments in selected areas in the host RAM.
B.4.1 Example
The FibonacciSeries.vm file contains a series of VM commands that compute the first n elements of the Fibonacci series. The code is designed to operate on two arguments: the value of n and the starting memory address in which the computed elements should be stored. The script in figure B.6 is designed to test this program using the actual arguments 6 and 4000.
B.4.2 Variables
Scripting commands running on the VM emulator can access the following elements:
Contents of Virtual Memory Segments
local [i]:
value of the i-th element of the local segment;
argument [i]:
value of the i-th element of the argument segment;
this[i]: value of the i-th element of the this segment;
that[i]: value of the i-th element of the that segment;
temp [i]:
value of the i-th element of the temp segment.
Pointers to Virtual Memory Segments
local:
base address of the local segment in the RAM;
argument:
base address of the argument segment in the RAM;
this:
base address of the this segment in the RAM;
that:
base address of the that segment in the RAM.
Figure B.6
Testing a VM program on the VM emulator.
Implementation-Specific Variables
RAM [i]:
value of the i-th RAM location;
SP:
value of the stack pointer;
currentFunction:
name of the currently executing function (read only).
line:
contains a string of the form:
current-function-name.line-index-in-function
(read only).
For example, when execution reaches the third line of the function Sys.init, the line variable contains “Sys.init.3”. This is a useful means for setting breakpoints in selected locations in the loaded VM program.
B.4.3 Commands
The VM emulator supports all the commands described in section B.2.3, except for the following changes:
load
source:
Here source is either Xxx.vm, the name of a file containing one or more VM functions, or a series of “raw” VM commands, or Xxx, the name of a directory containing one or more .vm files (in which case all of them are loaded).
If the .vm files are located in the current directory, the source argument can be omitted.
tick/tock:
Not applicable.
vmstep:
Simulates the execution of a single VM command from the VM program, and advances to the next command in the code.
B.4.4 Default Script
The VM emulator’s GUI buttons (single step, run, stop, reset) don’t control the loaded VM code. Rather, they control the progression of the loaded script, which controls the code’s operation. Thus, there is a question of what to do if the user has loaded a program directly into the VM emulator without loading a script first. In such cases, the emulator uses the following default script:
Index
Abstraction
implementation paradigm
modules and
Adder gates
Addresses
direct addressing
indirect addressing
machine language and
mapping and
memory and(
see
also Memory)
program size limits and
registers and
subroutines and
symbol table and
VM-Hack mapping and
Addressing instruction (
A
-instruction)
Algorithms
efficiency and
graphics and
mathematics and
memory management and
operating systems and(
see
also Operating systems)
runtime and
syntax and
ALU. See Arithmetic Logic Unit
Analysis-synthesis paradigm
And function
implementation of
multi-bit versions of
Application Program Interface (API) notation
Architecture
bottom-up
chip set
CPU and
Hack
hardware
I/O and
Jack(
see
also Jack)
machine language and
memory and
modifications and
modules and
optimization and
registers and
sequential chip hierarchy and
standards and
stored program concept and
top-down
VM and(
see
also Virtual Machine)
von Neumann
Aristotle
Arithmetic addition
Arithmetic Logic Unit (ALU)
Boolean arithmetic and
combinational chips and
CPU and
description of
Hack and
operating systems and
visualized chip operations and
Arrays
data translation and
Jack and
operating systems and
stack processing and
variable-length
Virtual Machine (VM) and
ASCII code
Assembler
hash table
implementation of
labels and
machine language specification and
macros and
mnemonics and
program size limits and
symbols and
syntax and
test scripts and
as translator program
variables and
Best-fit
Big-Oh notation (
O
(n))
Binary code. See also Boolean logic
code generation and
graphics and
Jack and
Binary search
Bitmaps
Bit shifting
Bit-wise negation
Boolean arithmetic
addition
algebra and
ALU and
binary numbers and
CPU and
least significant bits (LSB)
memory and
most significant bits (MSB)
radix complement method
signed binary numbers
stack processing and
Boolean logic
abstraction of
algebra and
canonical representation
conditional execution
gates and
hardware construction and
HDL and
machine language and
repetition
subroutine calling
truth tables
two-input functions
Bootstrap code
Buses
C#
Jack and
C++
Canonical representation
Case conventions
Central Processing Unit (CPU)
ALU and
architecture and
control unit and
description of
emulators and
Hack and
instruction memory and
program counter and
registers and
testing and
von Neumann architecture and
Character output
Chips. See also Gates
adder
API specification and
Boolean logic and
built-in
buses and
clocks and
combinational
connections and
cost and