diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-08-23 23:41:29 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-08-23 23:41:29 +0800 |
commit | b3ee892850d512f143822b78f7714951d25d064a (patch) | |
tree | 60ceed00a2a4251eca0ba932384a9ff3353c1671 | |
parent | d60d493fa409311c2c0713c302c5f90221af6328 (diff) |
hack-vm: init code
-rw-r--r-- | projects/hack-vm/__main__.py | 4 | ||||
-rw-r--r-- | projects/hack-vm/vminit.py | 46 |
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) |