Skip to content

Add Disassembler #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add byte offset to output to make seeing offsets easier
Offsets are in number of bytes (matches how 'GNU as' outputs listings)
  • Loading branch information
wnienhaus committed Jul 2, 2023
commit 40ea7e93163410b565a31f55f8a9939215d0beb8
22 changes: 11 additions & 11 deletions tools/disassemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,24 @@ def chunk_into_words(code, bytes_per_word, byteorder):
return words


def print_code_line(i, asm):
lineformat = '{0} {1}'
def print_code_line(byte_offset, i, asm):
lineformat = '{0:04x} {1} {2}'
hex = ubinascii.hexlify(i.to_bytes(4, 'little'))
print(lineformat.format(hex.decode('utf-8'), asm))
print(lineformat.format(byte_offset, hex.decode('utf-8'), asm))


def decode_instruction_and_print(i, verbose=False):
def decode_instruction_and_print(byte_offset, i, verbose=False):
try:
ins, name = decode_instruction(i)
except Exception as e:
print_code_line(i, e)
print_code_line(byte_offset, i, e)
return

print_code_line(i, name)
print_code_line(byte_offset, i, name)

if verbose:
for field, val, extra in get_instruction_fields(ins):
print(" {:10} = {:3}{}".format(field, val, extra))
print(" {:10} = {:3}{}".format(field, val, extra))


def disassemble_manually(byte_sequence_string, verbose=False):
Expand All @@ -190,10 +190,10 @@ def disassemble_manually(byte_sequence_string, verbose=False):
for i in range(0, len(sequence), chars_per_instruction)
]

for instruction in list:
for idx, instruction in enumerate(list):
byte_sequence = ubinascii.unhexlify(instruction.replace(' ',''))
i = int.from_bytes(byte_sequence, 'little')
decode_instruction_and_print(i, verbose)
decode_instruction_and_print(idx << 2, i, verbose)


def disassemble_file(filename, verbose=False):
Expand All @@ -203,8 +203,8 @@ def disassemble_file(filename, verbose=False):
code = data[12:] # text_offset (where code starts) is always 12 for ULP binaries
words = chunk_into_words(code, bytes_per_word=4, byteorder='little')

for i in words:
decode_instruction_and_print(i, verbose)
for idx, i in enumerate(words):
decode_instruction_and_print(idx << 2, i, verbose)


def print_help():
Expand Down