Write documentation for first part of course

This commit is contained in:
2020-11-15 14:38:56 -05:00
parent 742db6d102
commit 16fa19d034
5 changed files with 186 additions and 18 deletions

View File

@@ -4,7 +4,6 @@ import sys
import re
def preprocess(lines):
lines = remove_whitespaces(lines)
lines = replace_symbols(lines)
@@ -36,7 +35,7 @@ def replace_symbols(lines):
"R15": "15",
"SCREEN": "16384",
"KBD": "24576",
}
}
# Find all labels, remove them, and add them to symbol table.
address = 0
@@ -116,6 +115,7 @@ def assemble_c_instruction(line):
ins_str = "111" + comp_lookup(comp) + dest_lookup(dest) + jump_lookup(jump)
return ins_str
def comp_lookup(comp_str):
return {
"0": "0101010",
@@ -176,26 +176,28 @@ def jump_lookup(jump_str):
if __name__ == "__main__":
try:
hack_asm_file = sys.argv[1]
if not sys.argv[1:]:
sys.exit("Call: ./assembler.py <hack_asm_file>")
for hack_asm_file in sys.argv[1:]:
if not hack_asm_file.endswith(".asm"):
sys.exit("Hack asm file must have a .asm file ending.")
hack_asm_ns_file = hack_asm_file.replace(".asm", ".nosymbol.asm")
hack_bin_file = hack_asm_file.replace(".asm", ".hack")
except IndexError:
sys.exit("Call: ./assembler.py <hack_asm_file>")
with open(hack_asm_file, 'r') as f:
assembly_lines = f.readlines()
with open(hack_asm_file, 'r') as f:
assembly_lines = f.readlines()
preprocessed_lines = preprocess(assembly_lines)
binary_lines = assemble(preprocessed_lines)
preprocessed_lines = preprocess(assembly_lines)
binary_lines = assemble(preprocessed_lines)
with open(hack_asm_ns_file, 'w') as f:
for line in preprocessed_lines:
f.write(line + "\n")
with open(hack_asm_ns_file, 'w') as f:
for line in preprocessed_lines:
f.write(line + "\n")
with open(hack_bin_file, 'w') as f:
for line in binary_lines:
f.write(line + "\n")
with open(hack_bin_file, 'w') as f:
for line in binary_lines:
f.write(line + "\n")
print(f"{hack_asm_file} -> {hack_bin_file}")