Assembler erweitert

This commit is contained in:
Sven Riwoldt
2024-04-04 19:37:49 +02:00
parent c7bc862c6f
commit efc4bd08a5
6 changed files with 326 additions and 22 deletions

View File

@@ -168,8 +168,8 @@ def load_asm_file():
Carriage returns are removed.
"""
ic(Path(sys.argv[1]).expanduser().read_text().replace(
'\r', '').split('\n'))
#ic(Path(sys.argv[1]).expanduser().read_text().replace(
# '\r', '').split('\n'))
return Path(sys.argv[1]).expanduser().read_text().replace(
'\r', '').split('\n')
@@ -179,18 +179,18 @@ def load_asm_file():
Anything inside a line, after a "//" sequence is a comment.
"""
def filter_comment_and_blank_in_line(l):
ic(re.sub('\s', '', l).split('//')[0])
#ic(re.sub('\s', '', l).split('//')[0])
return re.sub('\s', '', l).split('//')[0]
ic([filter_comment_and_blank_in_line(l) for l in lines])
#ic([filter_comment_and_blank_in_line(l) for l in lines])
return [filter_comment_and_blank_in_line(l) for l in lines]
def remove_empty_lines(file):
ic("remove_empty_lines ",[ l for l in file if len(l) > 0])
#ic("remove_empty_lines ",[ l for l in file if len(l) > 0])
return [l for l in file if len(l) > 0]
#ic("vor remove empty lines")
ic(remove_empty_lines(filter_comment_and_blank_in_lines(read_lines())))
#ic(remove_empty_lines(filter_comment_and_blank_in_lines(read_lines())))
return remove_empty_lines(
filter_comment_and_blank_in_lines(
@@ -198,17 +198,17 @@ def load_asm_file():
def is_label(line):
ic(line)
#ic(line)
"""Recognise "label" declarations
A label is an line in the form `"(" LABEL_NAME ")"`
"""
ic(line.startswith('(') and line.endswith(')'))
#ic(line.startswith('(') and line.endswith(')'))
return line.startswith('(') and line.endswith(')')
def extract_label_name(label_declaration):
ic("extract_label_name ", label_declaration.strip('()'))
#ic("extract_label_name ", label_declaration.strip('()'))
"""Extract the label name from a label instruction"""
return label_declaration.strip('()')
@@ -218,7 +218,7 @@ def is_a_instruction(line):
An A-instruction starts with "@"
"""
ic("is_a_instruction ",line.startswith('@'))
#ic("is_a_instruction ",line.startswith('@'))
return line.startswith('@')
@@ -256,7 +256,7 @@ def default_symbol_table():
def inc_p_c(line, program_counter):
ic("inc_p_c ", line, program_counter )
#ic("inc_p_c ", line, program_counter )
"""Increment `program_counter` if `line` is an instruction"""
if is_label(line):
return program_counter
@@ -429,7 +429,7 @@ def get_op_code(c_instruction):
def assemble_op_code_no_M(op_code):
ic(op_code)
#ic(op_code)
"""Convert an assembly op code to its binary counterpart
Note that this method assumes that the A/M switch is made. It
@@ -492,14 +492,14 @@ def assemble_op_code_no_M(op_code):
def assemble_op_code(op_code):
ic("assemble_op_code ", op_code)
#ic("assemble_op_code ", op_code)
"""Assemble the A/M switch and the op-code"""
return ('1' if 'M' in op_code else '0') + \
assemble_op_code_no_M(op_code.replace('M', 'A'))
def assemble_c_instruction(c_instruction):
ic("assemble_c_instruction ", c_instruction)
#ic("assemble_c_instruction ", c_instruction)
"""Assemble a c-instruction
The binary representation of a c-instruction is