Fix bug with binary.

This commit is contained in:
Edvard Thörnros 2019-04-02 19:34:39 +02:00
parent abb8aa8dc9
commit b3747ae9f9

View File

@ -24,6 +24,8 @@
# P - Operand (only there if M is 01) # P - Operand (only there if M is 01)
# #
from sys import stdin, stdout, argv, exit
class Data: class Data:
def __init__(self, string): def __init__(self, string):
self.length = len(string) // 4 self.length = len(string) // 4
@ -43,8 +45,12 @@ class Data:
""" """
Convert the instruction to a binary string. Convert the instruction to a binary string.
""" """
code = [bin(x)[2:] for x in self.data] result = []
return "".join(('0' * (WORD_SIZE - len(x))) + x for x in code) for x in self.data:
c = bin(x)[2:]
result.append(('0' * (4 - len(c))) + c)
return "".join(result)
class Instruction: class Instruction:
""" """
@ -105,10 +111,11 @@ class Instruction:
if self.length == 1: if self.length == 1:
length = WORD_SIZE // 1 length = WORD_SIZE // 1
else: else:
length = WORD_SIZE // 2 length = WORD_SIZE * 2
code = bin(self.instruction)[2:] code = bin(self.instruction)[2:]
return ('0' * (length - len(code))) + code return ('0' * (length - len(code))) + code
OPCODE_TABLE = { OPCODE_TABLE = {
"LOAD" : 0b0000, "LOAD" : 0b0000,
"STORE" : 0b0001, "STORE" : 0b0001,
@ -137,8 +144,6 @@ REGISTERS = {
WORD_SIZE = 16 WORD_SIZE = 16
from sys import stdin, stdout, argv, exit
output_format = "hex" output_format = "hex"
input_file = False input_file = False
output_file = False output_file = False
@ -232,12 +237,14 @@ def parse_instruction(args):
return Instruction(opcode, register, mode, address) return Instruction(opcode, register, mode, address)
success = True def main():
line_number = 0 # The line in the source file success = True
word = 0 # The current word line_number = 0 # The line in the source file
instructions = [] word = 0 # The current word
tag_table = {} instructions = []
for line in input_file: tag_table = {}
for line in input_file:
line_number += 1 line_number += 1
if "#" in line: if "#" in line:
line = line[:line.index("#")] line = line[:line.index("#")]
@ -270,7 +277,7 @@ for line in input_file:
print("ERROR:{} Unknown symbol \"{}\"".format(line_number, line)) print("ERROR:{} Unknown symbol \"{}\"".format(line_number, line))
success = False success = False
for instruction in instructions: for instruction in instructions:
try: try:
if type(instruction) == Instruction: if type(instruction) == Instruction:
instruction.bake(tag_table) instruction.bake(tag_table)
@ -278,10 +285,10 @@ for instruction in instructions:
print("LINKING ERROR: {}".format(e)) print("LINKING ERROR: {}".format(e))
success = False success = False
if not success: if not success:
exit(1) return 1
def print_as_hex(out, inst, newlines): def print_as_hex(out, inst, newlines):
string = instruction.to_hex() string = instruction.to_hex()
if newlines: if newlines:
while string: while string:
@ -290,7 +297,7 @@ def print_as_hex(out, inst, newlines):
string = string[WORD_SIZE // 4:] string = string[WORD_SIZE // 4:]
out.write(string) out.write(string)
def print_as_bin(out, inst, newlines): def print_as_bin(out, inst, newlines):
string = instruction.to_bin() string = instruction.to_bin()
if newlines: if newlines:
while string: while string:
@ -299,16 +306,19 @@ def print_as_bin(out, inst, newlines):
string = string[WORD_SIZE:] string = string[WORD_SIZE:]
out.write(string) out.write(string)
if output_format == "hex": if output_format == "hex":
print_func = print_as_hex print_func = print_as_hex
else: else:
print_func = print_as_bin print_func = print_as_bin
for instruction in instructions: for instruction in instructions:
print_func(output_file, instruction, newlines) print_func(output_file, instruction, newlines)
output_file.flush()
output_file.close()
exit(0) output_file.flush()
output_file.close()
if __name__ == "__main__":
exit(main())