|
|
|
@ -25,12 +25,37 @@ def getreg(g):
|
|
|
|
|
temp = temp.replace('b', '0')
|
|
|
|
|
return temp
|
|
|
|
|
|
|
|
|
|
def getjmp(g):
|
|
|
|
|
temp = g
|
|
|
|
|
if registers.get(instruction[1], 'fail') != 'fail':
|
|
|
|
|
temp = temp + registers.get(instruction[1]) + ';'
|
|
|
|
|
temp = temp.replace('a', '1')
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
binified = format(int(instruction[1]), '08b')[:8].replace('-', '1')
|
|
|
|
|
temp = temp + binified + ';'
|
|
|
|
|
temp = temp.replace('a', '0')
|
|
|
|
|
|
|
|
|
|
if markers.get(instruction[2], 'fail') != 'fail':
|
|
|
|
|
temp = temp + format(int(markers.get(instruction[2])), '08b')
|
|
|
|
|
temp = temp.replace('a', '1')
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
binified = format(int(instruction[2]), '08b')[:8].replace('-', '1')
|
|
|
|
|
temp = temp + binified + ';'
|
|
|
|
|
temp = temp.replace('a', '0')
|
|
|
|
|
return temp + ';'
|
|
|
|
|
|
|
|
|
|
verbose = 0
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
|
print("Add arguments!")
|
|
|
|
|
quit()
|
|
|
|
|
if len(sys.argv) > 2:
|
|
|
|
|
if sys.argv[2] == '-v' or sys.argv[2] == '--verbose':
|
|
|
|
|
print("Running in verbose")
|
|
|
|
|
verbose = 1
|
|
|
|
|
|
|
|
|
|
markers = {}
|
|
|
|
|
|
|
|
|
|
file = open(sys.argv[1])
|
|
|
|
|
fullfile = file.readlines()
|
|
|
|
@ -38,6 +63,7 @@ file.close()
|
|
|
|
|
output = open(sys.argv[1].strip('.ra') + '.out', 'w+')
|
|
|
|
|
line = 0
|
|
|
|
|
|
|
|
|
|
markers = {}
|
|
|
|
|
registers = {
|
|
|
|
|
#normal
|
|
|
|
|
'acc': '01000000',
|
|
|
|
@ -55,41 +81,47 @@ for i in fullfile:
|
|
|
|
|
line = line + 1
|
|
|
|
|
instruction = i.strip()
|
|
|
|
|
instruction = instruction.lower().split(' ')
|
|
|
|
|
print(instruction)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print(instruction)
|
|
|
|
|
|
|
|
|
|
if instruction[0][:1] == '!':
|
|
|
|
|
line = line - 1
|
|
|
|
|
markers[instruction[0][1:].strip('\n')] = line
|
|
|
|
|
print("marker")
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("marker")
|
|
|
|
|
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print(markers)
|
|
|
|
|
|
|
|
|
|
print(markers)
|
|
|
|
|
for i in fullfile:
|
|
|
|
|
temp = None
|
|
|
|
|
binified = None
|
|
|
|
|
line = line + 1
|
|
|
|
|
|
|
|
|
|
instruction = i.strip()
|
|
|
|
|
instruction = instruction.lower().split(' ')
|
|
|
|
|
print(instruction)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print(instruction)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if instruction[0] == 'nul':
|
|
|
|
|
print("nul")
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("nul")
|
|
|
|
|
output.write('00000000;00000000;00000000\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'nop':
|
|
|
|
|
print("nop")
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("nop")
|
|
|
|
|
output.write('00101000\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'mov':
|
|
|
|
|
print("mov")
|
|
|
|
|
temp = '010000ab;'
|
|
|
|
|
temp = getreg(temp)
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("mov")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'add':
|
|
|
|
|
print("add")
|
|
|
|
|
temp = '11000yab;'
|
|
|
|
|
temp = getreg(temp)
|
|
|
|
|
if len(instruction) > 3:
|
|
|
|
@ -97,11 +129,12 @@ for i in fullfile:
|
|
|
|
|
temp = temp.replace('y', '1')
|
|
|
|
|
else:
|
|
|
|
|
temp = temp.replace('y', '0')
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("add")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'sub':
|
|
|
|
|
print("sub")
|
|
|
|
|
temp = '10100yab;'
|
|
|
|
|
temp = getreg(temp)
|
|
|
|
|
if len(instruction) > 3:
|
|
|
|
@ -109,11 +142,12 @@ for i in fullfile:
|
|
|
|
|
temp = temp.replace('y', '1')
|
|
|
|
|
else:
|
|
|
|
|
temp = temp.replace('y', '0')
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("sub")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'or':
|
|
|
|
|
print("or")
|
|
|
|
|
temp = '11100yab;'
|
|
|
|
|
temp = getreg(temp)
|
|
|
|
|
if len(instruction) > 3:
|
|
|
|
@ -121,11 +155,12 @@ for i in fullfile:
|
|
|
|
|
temp = temp.replace('y', '1')
|
|
|
|
|
else:
|
|
|
|
|
temp = temp.replace('y', '0')
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("or")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'xor':
|
|
|
|
|
print("xor")
|
|
|
|
|
temp = '10010yab;'
|
|
|
|
|
temp = getreg(temp)
|
|
|
|
|
if len(instruction) > 3:
|
|
|
|
@ -133,11 +168,12 @@ for i in fullfile:
|
|
|
|
|
temp = temp.replace('y', '1')
|
|
|
|
|
else:
|
|
|
|
|
temp = temp.replace('y', '0')
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("xor")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'and':
|
|
|
|
|
print("and")
|
|
|
|
|
temp = '11010yab;'
|
|
|
|
|
temp = getreg(temp)
|
|
|
|
|
if len(instruction) > 3:
|
|
|
|
@ -145,11 +181,12 @@ for i in fullfile:
|
|
|
|
|
temp = temp.replace('y', '1')
|
|
|
|
|
else:
|
|
|
|
|
temp = temp.replace('y', '0')
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("and")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'jmp':
|
|
|
|
|
print("jmp")
|
|
|
|
|
temp = '011000a0;'
|
|
|
|
|
if markers.get(instruction[1], 'fail') != 'fail':
|
|
|
|
|
temp = temp + format(int(markers.get(instruction[1])), '08b')
|
|
|
|
@ -160,45 +197,84 @@ for i in fullfile:
|
|
|
|
|
temp = temp + binified + ';'
|
|
|
|
|
temp = temp.replace('a', '0')
|
|
|
|
|
temp = temp + ';00000000;'
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp+ '\n')
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("jmp")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'jnz':
|
|
|
|
|
temp = '000100a0;'
|
|
|
|
|
temp = getjmp(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("jnz")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'jgz':
|
|
|
|
|
temp = '010100a0;'
|
|
|
|
|
temp = getjmp(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("jgz")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'jlz':
|
|
|
|
|
temp = '001100a0;'
|
|
|
|
|
temp = getjmp(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("jlz")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'jez':
|
|
|
|
|
temp = '011100a0;'
|
|
|
|
|
temp = getjmp(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("jez")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'dbl':
|
|
|
|
|
print("dbl")
|
|
|
|
|
temp = '00001000;00000000;00000000;'
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("dbl")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'dbb':
|
|
|
|
|
print("dbb")
|
|
|
|
|
temp = '01001000;00000000;00000000;'
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("dbb")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == 'hlt':
|
|
|
|
|
print("hlt")
|
|
|
|
|
temp = '00000100;00000000;00000000;'
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("hlt")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0] == '':
|
|
|
|
|
print("newline")
|
|
|
|
|
temp = '00000000;00000000;00000000;'
|
|
|
|
|
print(temp)
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("newline")
|
|
|
|
|
print(temp)
|
|
|
|
|
output.write(temp + '\n')
|
|
|
|
|
|
|
|
|
|
elif instruction[0][:1] == '!':
|
|
|
|
|
print("marker")
|
|
|
|
|
print("Ignoreing this")
|
|
|
|
|
if verbose == 1:
|
|
|
|
|
print("marker")
|
|
|
|
|
print("Ignoring this")
|
|
|
|
|
line = line - 1
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print("Error!")
|
|
|
|
|
print("Line: " + str(line))
|
|
|
|
|
#quit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Content: " + i)
|
|
|
|
|
quit()
|
|
|
|
|
|
|
|
|
|
print(markers)
|
|
|
|
|
output.close()
|
|
|
|
|
print("All done!")
|
|
|
|
|
print("Data written to: " + sys.argv[1].strip('.ra') + '.out')
|
|
|
|
|