summaryrefslogtreecommitdiff
path: root/projects/hack-vm
diff options
context:
space:
mode:
Diffstat (limited to 'projects/hack-vm')
-rw-r--r--projects/hack-vm/__main__.py4
-rw-r--r--projects/hack-vm/vminit.py46
2 files changed, 49 insertions, 1 deletions
diff --git a/projects/hack-vm/__main__.py b/projects/hack-vm/__main__.py
index bacbc94..5335583 100644
--- a/projects/hack-vm/__main__.py
+++ b/projects/hack-vm/__main__.py
@@ -1,6 +1,7 @@
import os
from pathlib import Path
from argparse import ArgumentParser
+from .vminit import vminit
from .memory import translate_memory
from .arith_logic import translate_arith_logic
from .compare import translate_compare
@@ -25,6 +26,7 @@ def vm_translate(input_path, verbose):
# assembly translated from all .vm files will be concatenated here
asm = ""
+ # asm = vminit(input_path.stem)
for input_fn in vm_files:
# load all vm commands from file, e.g. program/Main.vm
@@ -56,7 +58,7 @@ def vm_translate(input_path, verbose):
case [("lt" | "eq" | "gt") as command]:
asm += translate_compare(command, verbose)
case [("label" | "goto" | "if-goto") as action, label]:
- asm += translate_branching(action, label, verbose)
+ asm += translate_branching(action, label, prog, verbose)
case [("call" | "function") as action, function, n]:
asm += translate_function(action, function, n, prog, verbose)
case ["return"]:
diff --git a/projects/hack-vm/vminit.py b/projects/hack-vm/vminit.py
new file mode 100644
index 0000000..73f79ce
--- /dev/null
+++ b/projects/hack-vm/vminit.py
@@ -0,0 +1,46 @@
+from .utils import *
+
+VMINIT_ASM = """@256
+D=A
+@SP
+M=D
+@{prog}$ret_init
+D=A
+@SP
+A=M
+M=D
+@LCL
+D=M
+@SP
+AM=M+1
+M=D
+@ARG
+D=M
+@SP
+AM=M+1
+M=D
+@THIS
+D=M
+@SP
+AM=M+1
+M=D
+@THAT
+D=M
+@SP
+AM=M+1
+M=D
+@SP
+MD=M+1
+@LCL
+M=D
+@5
+D=D-A
+@ARG
+M=D
+@Sys.init
+0;JMP
+({prog}$ret_init)
+"""
+
+def vminit(prog):
+ return VMINIT_ASM.format(prog=prog)