-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (50 loc) · 1.76 KB
/
main.py
File metadata and controls
65 lines (50 loc) · 1.76 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from tools.factory import Instr_Factory
from tools.utils import separate, trans
from tools.output import Output
import argparse
from math import log10, ceil
def main(mode: str, infile_path: str, outfile_path=None):
'''
input: file_path
mode: STD | FILE
'''
assert not (mode == "FILE" and outfile_path is None)
fin = open(infile_path)
origin_lines = fin.readlines()
fin.close()
origin_lines = [int(line.strip('\n'), 16) for line in origin_lines]
# get instruction type
sepa_lst = [separate(line) for line in origin_lines]
trans_lst = [trans(line) for line in sepa_lst]
# get addr
base_addr = 0x00400000
addr_lst = [hex(base_addr+idx*4) for idx, elem in enumerate(sepa_lst)]
# process instructions input
instr_txt_lst = []
for line in trans_lst:
instr = line[0]
instr_fact = Instr_Factory(
instr["instr"], instr["OP"], instr["Rs"],
instr["Rt"], instr["Rd"], instr["SA"], instr["FUNCT"]
)
instr_txt_lst.append(instr_fact.instr_txt)
# output result
out = Output(mode)
out.method(addr_lst, origin_lines, instr_txt_lst, outfile_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
choices = [
{ "method": "-d", "vmethod": "--dsasmb", "help": "disassemble *.txt" },
{ "method": "-o", "vmethod": "--output", "help": "output to *.txt" }
]
for choice in choices:
parser.add_argument(
choice["method"], choice["vmethod"], type=str,
help=choice["help"]
)
args = parser.parse_args()
infile_path = args.dsasmb
outfile_path = args.output
assert infile_path is not None
mode = "STD" if outfile_path is None else "FILE"
main(mode, infile_path, outfile_path)