VM Language and Implementation Summary

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

VM Language and Implementation Summary

cadet1620
Administrator
This post was updated on .
[This is a new document. Corrections, additions, etc. are encouraged!]

Memory Access Commands
push segment indexPushes a memory location's value or a constant, onto the stack.
pop segment indexPops a value off the stack and stores it in a memory location.

Arithmetic/Logical Commands
Arithmetic operations and comparisons are 2's complement.
Logical operations are bitwise.
addPop y, pop x, push x + y.
subPop y, pop x, push x - y.
andPop y, pop x, push AND(x, y).
orPop y, pop x, push OR(x, y).
ltPop y, pop x, push -1 if x < y, else 0.
eqPop y, pop x, push -1 if x = y, else 0.
gtPop y, pop x, push -1 if x > y, else 0.
negPop x, push −x.
notPop x, push NOT(x).
"Pop, pop, push" is the logical effect of the commands. Your translator does not need write actual push and pop commands.

Program Flow commands
Must be within a function, and label may be duplicated between multiple functions.
The ASM symbol should be functionName$label. ('$' is a legal ASM symbol character that is assumed will not be in functionName or label.)
label symbolDefines a target for goto and <i<if-goto</i> commands.
goto symbolUnconditional jump to symbol.
if-goto symbolPop x. If x ≠ 0, jump to symbol.
symbol is a string of letters, digits, underscore (_), dot (.), and colon (:) that does not begin with a digit.

Function commands
function functionName nLocalsBegin a new function that uses nLocals local variables.
call functionName nArgsCall functionName. nArgs arguments have been pushed on the stack.
returnReturn to the calling function. The current top-of-stack value is the return value.
functionName is a string of letters, digits, underscore (_), dot (.), and colon (:) that does not begin with a digit.

Memory segments
Pointer-based addressing
argument0 ≤ index < nArgs *(ARG + index)Arguments for the current function.
local0 ≤ index < nLocals *(LCL + index)Local variables for the current function.
thisindex ≥ 0 *(THIS + index)Data fields in class objects.
thatindex ≥ 0 *(THAT + index)Array indexing.
Absolute addressing
pointerindex = 0 or 1 THIS (0) or THAT (1) The base pointers for the this and that segments.
temp0 ≤ index ≤ 7 R5 - R12Project global temporary variables.
staticindex ≥ 0 filename.indexFile global variables.
constantindex ≥ 0 @indexA read only constant, value = index.

Implementation of function related commands

call f n

(calling a function f
after n arguments
have been pushed
onto the stack)

   push return-address // (Using the label declared below)
   push LCL            // Save LCL of calling function
   push ARG            // Save ARG of calling function
   push THIS           // Save THIS of calling function
   push THAT           // Save THAT of calling function
   ARG = SP-n-5        // Reposition ARG (n = number of args.)
   LCL = SP            // Reposition LCL
   goto f              // Transfer control
(return-address)       // Declare a label for the return-address
function f k

(declaring a function
f that has k local
variables)

(f)                    // Declare label for the function entry
   repeat k times:     // k = number of local variables
   PUSH 0              // Initialize all of them to 0
return

(from a function)

   FRAME = LCL         // FRAME is a temporary variable
   RET = * (FRAME-5)   // Put the return-address in a temp. var.
   *ARG = pop()        // Reposition the return value for the caller
   SP = ARG+1          // Restore SP of the caller
   THAT = *(FRAME-1)   // Restore THAT of the caller
   THIS = *(FRAME-2)   // Restore THIS of the caller
   ARG = *(FRAME-3)    // Restore ARG of the caller
   LCL = *(FRAME-4)    // Restore LCL of the caller
   goto RET            // Goto return-address (in the caller's code)

Bootstrap code

SP=256          // Initialize the stack pointer to 0x0100
call Sys.init   // Start executing (the translated code of) Sys.init
Note that this is a VM "call Sys.init 0" command, not a jump.
Command line

VMTranslator foo/bar/file.vm
  Translate foo/bar/file.vm to foo/bar/file.asm, do not write bootstrap code.

VMTranslator foo/bar/directory
  Translate all *.vm files in foo/bar/directory to foo/bar/directory/directory.asm, write bootstrap code.