summaryrefslogtreecommitdiff
path: root/projects/hack-vm/branching.py
diff options
context:
space:
mode:
Diffstat (limited to 'projects/hack-vm/branching.py')
-rw-r--r--projects/hack-vm/branching.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/projects/hack-vm/branching.py b/projects/hack-vm/branching.py
new file mode 100644
index 0000000..b73ed94
--- /dev/null
+++ b/projects/hack-vm/branching.py
@@ -0,0 +1,24 @@
+from .utils import *
+
+LABEL_ASM = "({label})\n"
+
+GOTO_ASM = """@{label}
+0;JMP
+"""
+
+IF_GOTO_ASM = """@SP
+AM=M-1
+D=M
+@{label}
+D;JNE
+"""
+
+BRANCHING_ASM = {
+ "label": LABEL_ASM,
+ "goto": GOTO_ASM,
+ "if-goto": IF_GOTO_ASM,
+}
+
+
+def translate_branching(action, label):
+ return BRANCHING_ASM[action].format(label=label)