Skip to content

Commit 8386252

Browse files
committed
M: add output formatting about file-output
1 parent ee9cbb4 commit 8386252

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

main.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from factory import Instr_Factory
2-
from utils import separate, trans
1+
from tools.factory import Instr_Factory
2+
from tools.utils import separate, trans
33
import argparse
4+
from math import log10, ceil
45

56
'''
67
input: file_path
@@ -10,15 +11,18 @@ def main(mode: str, infile_path: str, outfile_path=None):
1011
assert not (mode == "FILE" and outfile_path is None)
1112

1213
fin = open(infile_path)
13-
lines = fin.readlines()
14-
lines = [int(line.strip('\n'), 16) for line in lines]
14+
origin_lines = fin.readlines()
15+
origin_lines = [int(line.strip('\n'), 16) for line in origin_lines]
1516

1617
if mode == "FILE":
1718
fout = open(outfile_path, "w", encoding="utf-8")
1819

19-
sepa_lst = [separate(line) for line in lines]
20+
# get instruction type
21+
sepa_lst = [separate(line) for line in origin_lines]
2022
trans_lst = [trans(line) for line in sepa_lst]
2123
instr_txt_lst = []
24+
25+
# process instructions input
2226
for line in trans_lst:
2327
instr = line[0]
2428
instr_fact = Instr_Factory(
@@ -30,29 +34,45 @@ def main(mode: str, infile_path: str, outfile_path=None):
3034
if mode == "FILE":
3135
# add '\n'
3236
instr_txt_lst = [instr_txt+'\n' for instr_txt in instr_txt_lst]
33-
fout.writelines(instr_txt_lst)
37+
38+
# thanks to https://stackoverflow.com/questions/11676864/how-can-i-format-an-integer-to-a-two-digit-hex/11677120
39+
# and https://note.nkmk.me/en/python-for-enumerate-zip/
40+
# and https://pyformat.info/ (about formats positional arguments)
41+
instr_num_lg = ceil(log10(len(instr_txt_lst)))
42+
line_head_len = max(instr_num_lg, len("idx"))
43+
fout.write(
44+
"{:{}{}}".format("idx", '>', line_head_len)+" "
45+
+"{:{}{}}".format("hex code", '>', 10)+" "
46+
+"{:{}}".format("mips-assembly", '<')+'\n'
47+
)
48+
# add breakline
49+
fout.write("-"*40+'\n')
50+
for idx, (origin_hex, instr_txt) in enumerate(zip(origin_lines, instr_txt_lst)):
51+
fout.write("{:{}{}}".format(idx, '>', line_head_len)+" "+"0x{:08x}".format(origin_hex)+" "+instr_txt)
52+
3453
elif mode == "STD":
35-
for instr_txt in instr_txt_lst:
54+
for origin_hex, instr_txt in zip(origin_lines, instr_txt_lst):
3655
print(instr_txt)
3756
else:
3857
pass
3958

40-
parser = argparse.ArgumentParser()
59+
if __name__ == "__main__":
60+
61+
parser = argparse.ArgumentParser()
4162

42-
choices = [
43-
{ "method": "-d", "vmethod": "--dsasmb", "help": "disassemble *.txt" },
44-
{ "method": "-o", "vmethod": "--output", "help": "output to *.txt" }
45-
]
63+
choices = [
64+
{ "method": "-d", "vmethod": "--dsasmb", "help": "disassemble *.txt" },
65+
{ "method": "-o", "vmethod": "--output", "help": "output to *.txt" }
66+
]
4667

47-
for choice in choices:
48-
parser.add_argument(
49-
choice["method"], choice["vmethod"], type=str,
50-
help=choice["help"]
51-
)
68+
for choice in choices:
69+
parser.add_argument(
70+
choice["method"], choice["vmethod"], type=str,
71+
help=choice["help"]
72+
)
5273

53-
args = parser.parse_args()
74+
args = parser.parse_args()
5475

55-
if __name__ == "__main__":
5676
infile_path = args.dsasmb
5777
outfile_path = args.output
5878

@@ -61,4 +81,4 @@ def main(mode: str, infile_path: str, outfile_path=None):
6181
if outfile_path is None:
6282
main("STD", infile_path)
6383
else:
64-
main("FILE", infile_path, outfile_path)
84+
main("FILE", infile_path, outfile_path)

0 commit comments

Comments
 (0)