Fix bug with binary.
This commit is contained in:
parent
abb8aa8dc9
commit
b3747ae9f9
24
compiler.py
24
compiler.py
@ -24,6 +24,8 @@
|
||||
# P - Operand (only there if M is 01)
|
||||
#
|
||||
|
||||
from sys import stdin, stdout, argv, exit
|
||||
|
||||
class Data:
|
||||
def __init__(self, string):
|
||||
self.length = len(string) // 4
|
||||
@ -43,8 +45,12 @@ class Data:
|
||||
"""
|
||||
Convert the instruction to a binary string.
|
||||
"""
|
||||
code = [bin(x)[2:] for x in self.data]
|
||||
return "".join(('0' * (WORD_SIZE - len(x))) + x for x in code)
|
||||
result = []
|
||||
for x in self.data:
|
||||
c = bin(x)[2:]
|
||||
result.append(('0' * (4 - len(c))) + c)
|
||||
return "".join(result)
|
||||
|
||||
|
||||
class Instruction:
|
||||
"""
|
||||
@ -105,10 +111,11 @@ class Instruction:
|
||||
if self.length == 1:
|
||||
length = WORD_SIZE // 1
|
||||
else:
|
||||
length = WORD_SIZE // 2
|
||||
length = WORD_SIZE * 2
|
||||
code = bin(self.instruction)[2:]
|
||||
return ('0' * (length - len(code))) + code
|
||||
|
||||
|
||||
OPCODE_TABLE = {
|
||||
"LOAD" : 0b0000,
|
||||
"STORE" : 0b0001,
|
||||
@ -137,8 +144,6 @@ REGISTERS = {
|
||||
|
||||
WORD_SIZE = 16
|
||||
|
||||
from sys import stdin, stdout, argv, exit
|
||||
|
||||
output_format = "hex"
|
||||
input_file = False
|
||||
output_file = False
|
||||
@ -232,11 +237,13 @@ def parse_instruction(args):
|
||||
return Instruction(opcode, register, mode, address)
|
||||
|
||||
|
||||
def main():
|
||||
success = True
|
||||
line_number = 0 # The line in the source file
|
||||
word = 0 # The current word
|
||||
instructions = []
|
||||
tag_table = {}
|
||||
|
||||
for line in input_file:
|
||||
line_number += 1
|
||||
if "#" in line:
|
||||
@ -279,7 +286,7 @@ for instruction in instructions:
|
||||
success = False
|
||||
|
||||
if not success:
|
||||
exit(1)
|
||||
return 1
|
||||
|
||||
def print_as_hex(out, inst, newlines):
|
||||
string = instruction.to_hex()
|
||||
@ -306,9 +313,12 @@ else:
|
||||
|
||||
for instruction in instructions:
|
||||
print_func(output_file, instruction, newlines)
|
||||
|
||||
output_file.flush()
|
||||
output_file.close()
|
||||
|
||||
exit(0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
exit(main())
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user