From 87c0ee9205c623e3f0f0cbfc285ea54861f57e92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Gilabert=20G=C3=A1mez?= Date: Fri, 26 Jul 2024 12:50:28 +0200 Subject: [PATCH] fix extra newlines on translated and negative offset on memory --- main.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 4f24f0a..82aae72 100644 --- a/main.py +++ b/main.py @@ -31,11 +31,24 @@ def binary_to_hex(bin): for instruction in input_file.readlines(): instruction_splitted = instruction.split(" ") + instruction_splitted = [e for elem in instruction_splitted for e in elem.split("(")] + instruction_splitted = [elem for elem in instruction_splitted if elem != ""] instruction_splitted = [elem.upper() for elem in instruction_splitted] dec_to_hex = ["000", "001", "010", "011", "100", "101", "110", "111"] + dec_to_hex_signed = { + "0": "000", + "1": "001", + "2": "010", + "3": "011", + "-1": "111", + "-2": "110", + "-3": "101", + "-4": "100" + } + instruction_decoded = "" decoded = False @@ -69,16 +82,17 @@ def binary_to_hex(bin): instruction_decoded += "1010" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "111" + dec_to_hex[int(instruction_splitted[3][1])] decoded = True case "STV": - instruction_decoded += "1111" + dec_to_hex[int(instruction_splitted[2][1])] + dec_to_hex[int(instruction_splitted[1][3])] + "001" + dec_to_hex[int(instruction_splitted[1][0])] + instruction_decoded += "1111" + dec_to_hex[int(instruction_splitted[2][1])] + dec_to_hex[int(instruction_splitted[1][1])] + "001" + dec_to_hex_signed[instruction_splitted[1]] decoded = True case "LDV": - instruction_decoded += "1111" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][3])] + "010" + dec_to_hex[int(instruction_splitted[2][0])] + instruction_decoded += "1111" + dec_to_hex[int(instruction_splitted[1][1])] + dec_to_hex[int(instruction_splitted[2][1])] + "010" + dec_to_hex_signed[instruction_splitted[2]] decoded = True + if decoded: print(".word 0x" + str(binary_to_hex(instruction_decoded))) else: - if instruction[:-1] != "" and instruction[:-1] == "\n": + if instruction[-1] != "" and instruction[-1] == "\n": print(instruction[:-1]) else: print(instruction)