-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisassembler.py
More file actions
49 lines (36 loc) · 1.13 KB
/
disassembler.py
File metadata and controls
49 lines (36 loc) · 1.13 KB
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
import binascii
import sys
from helpers import get_registers, opcodes8080
#File Reading
source_file = str(sys.argv[1])
output_file_name = source_file.split('.')[0] + "_disassembled.txt"
with open(source_file, 'rb') as f:
source_code = f.read()
hex_dump = binascii.hexlify(source_code).decode()
bit_size = int(len(hex_dump) / 2)
output = ""
opcode = 0
data_address_bytes = 0
for x in range(bit_size):
if data_address_bytes > 0:
data_address_bytes -= 1
opcode += 1
continue
position = (x * 2)
byte = hex_dump[position] + hex_dump[position + 1]
opcode_tuple = opcodes8080[byte]
instruction = opcode_tuple[0]
if not instruction:
opcode += 1
continue
offset = opcode_tuple[1] - 1
op_register = opcode_tuple[2] if opcode_tuple[2] else ""
if offset > 0:
arguments = get_registers(hex_dump, offset, position, op_register)
data_address_bytes = int(offset)
else:
arguments = op_register
output += hex(opcode) + "\t" + instruction + "\t" + arguments + "\n"
opcode += 1
with open(output_file_name, "w") as f:
f.write(output)