Skip to content

Commit 2a06a54

Browse files
committed
Add support for disassembling a provided file
The original "manual disassembling" now requires the "-m" option, followed by the sequence of hex digits representing the instructions. The sequence of hex digits does not need to be quoted. All parameters after -m will be joined together into a sequence of hex digits.
1 parent 6720584 commit 2a06a54

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

tools/disassemble.py

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ def get_instruction_fields(ins):
151151
return field_details
152152

153153

154+
def chunk_into_words(code, bytes_per_word, byteorder):
155+
chunks = [
156+
ubinascii.hexlify(code[i:i + bytes_per_word])
157+
for i in range(0, len(code), bytes_per_word)
158+
]
159+
160+
words = [int.from_bytes(ubinascii.unhexlify(i), byteorder) for i in chunks]
161+
162+
return words
163+
164+
154165
def decode_instruction_and_print(i):
155166
print(ubinascii.hexlify(i.to_bytes(4, 'little')))
156167

@@ -180,28 +191,66 @@ def disassemble_manually(byte_sequence_string):
180191
decode_instruction_and_print(i)
181192

182193

194+
def disassemble_file(filename):
195+
with open(filename, 'rb') as f:
196+
data = f.read()
197+
198+
code = data[12:] # text_offset (where code starts) is always 12 for ULP binaries
199+
words = chunk_into_words(code, bytes_per_word=4, byteorder='little')
200+
201+
for i in words:
202+
decode_instruction_and_print(i)
203+
204+
183205
def print_help():
184-
print('Usage: disassemble.py [<options>] <byte_sequence>')
206+
print('Usage: disassemble.py [<options>] [-m <byte_sequence> | <filename>]')
185207
print('')
186208
print('Options:')
187-
print(' -h Show this help text')
188-
print(' <byte_sequence> Sequence of hex bytes (8 per instruction)')
209+
print(' -h Show this help text')
210+
print(' -m <byte_sequence> Sequence of hex bytes (8 per instruction)')
211+
print(' <filename> Path to ULP binary')
189212
pass
190213

191214

192215
def handle_cmdline(params):
193-
byte_sequence = ''
216+
filename = None
217+
byte_sequence = None
194218

195219
while params:
196220
if params[0] == '-h':
197221
print_help()
198222
sys.exit(0)
223+
elif params[0] == '-m':
224+
if len(params) == 1:
225+
print_help()
226+
sys.exit(1)
227+
params = params[1:] # remove -m from list
228+
229+
sequence_len = len(params)
230+
for i in range(0, len(params)):
231+
if params[i][0] == '-': # start of a next option
232+
sequence_len = i-1
233+
break
234+
235+
if sequence_len < 0:
236+
print_help()
237+
sys.exit(1)
238+
239+
byte_sequence = "".join(params[:sequence_len+1])
240+
params = params[sequence_len:]
241+
elif params[0][0] == '-':
242+
# ignore unknown options for now
243+
pass
199244
else:
200-
byte_sequence += params[0]
245+
if not filename:
246+
filename = params[0]
201247

202248
params = params[1:] # remove first param from list
203249

204-
disassemble_manually(byte_sequence)
250+
if byte_sequence:
251+
disassemble_manually(byte_sequence)
252+
elif filename:
253+
disassemble_file(filename)
205254

206255

207256
if sys.argv: # if run from cmdline

0 commit comments

Comments
 (0)