From 51c8325661eab4667a11431175f12eb6a08cc36a Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 22 Aug 2022 17:24:46 +0800 Subject: hack-vm: branching --- projects/hack-vm/branching.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 projects/hack-vm/branching.py (limited to 'projects/hack-vm/branching.py') 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) -- cgit v1.2.3