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)
|
# 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,11 +237,13 @@ def parse_instruction(args):
|
|||||||
return Instruction(opcode, register, mode, address)
|
return Instruction(opcode, register, mode, address)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
success = True
|
success = True
|
||||||
line_number = 0 # The line in the source file
|
line_number = 0 # The line in the source file
|
||||||
word = 0 # The current word
|
word = 0 # The current word
|
||||||
instructions = []
|
instructions = []
|
||||||
tag_table = {}
|
tag_table = {}
|
||||||
|
|
||||||
for line in input_file:
|
for line in input_file:
|
||||||
line_number += 1
|
line_number += 1
|
||||||
if "#" in line:
|
if "#" in line:
|
||||||
@ -279,7 +286,7 @@ for instruction in instructions:
|
|||||||
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()
|
||||||
@ -306,9 +313,12 @@ else:
|
|||||||
|
|
||||||
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.flush()
|
||||||
output_file.close()
|
output_file.close()
|
||||||
|
|
||||||
exit(0)
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
exit(main())
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user