-
Notifications
You must be signed in to change notification settings - Fork 0
/
disasm.py
59 lines (49 loc) · 1.7 KB
/
disasm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python3
import sys
def main(argv):
if (len(argv) == 0):
print('Error: inform a filename to disassemble.')
exit()
# Nome do arquivo binário de entrada
filename = argv[0]
# Opção 'rb' abre o arquivo para leitura em formato binário
with open(filename, 'rb') as binfile:
byte = binfile.read(2)
while byte:
instruction = byte.hex()
opcode = instruction[0]
endereco = instruction[1:]
if(opcode in "0"):
answer = f"jns {endereco}"
elif(opcode in "1"):
answer = f"load {endereco}"
elif(opcode in "2"):
answer = f"store {endereco}"
elif(opcode in "3"):
answer = f"add {endereco}"
elif(opcode in "4"):
answer = f"subt {endereco}"
elif(opcode in "5"):
answer = "input"
elif(opcode in "6"):
answer = "output"
elif(opcode in "7"):
answer = "halt"
elif(opcode in "8"):
answer = f"skipcond {endereco}"
elif(opcode in "9"):
answer = f"jump {endereco}"
elif(opcode in "a"):
answer = "clear"
elif(opcode in "b"):
answer = f"addi {endereco}"
elif(opcode in "c"):
answer = f"jumpi {endereco}"
elif(opcode in "d"):
answer = f"loadi {endereco}"
elif(opcode in "e"):
answer = f"storei {endereco}"
print(answer)
byte = binfile.read(2)
if __name__ == "__main__":
main(sys.argv[1:])