54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
import sys
|
|
import re
|
|
|
|
|
|
JUMP_INSTRUCTIONS = [
|
|
"jc",
|
|
"jhs",
|
|
"jeq",
|
|
"jz",
|
|
"jge",
|
|
"jlt",
|
|
"jmp",
|
|
"jn",
|
|
"jnc",
|
|
"jlo",
|
|
"jl",
|
|
"jne",
|
|
"jnz",
|
|
]
|
|
|
|
def make_jump_line_absolute(line: str, parts: list[str]) -> str:
|
|
line_addr = int(parts[0].replace(":", ""), 16)
|
|
addr_offset = int(parts[3].replace("$", ""), 16)
|
|
abs_target_addr = line_addr + addr_offset
|
|
line = line.replace(parts[4], '') # remove relative to functione addr
|
|
line = line.replace(parts[3], f'${hex(abs_target_addr)[2:]}') # replace relative with abs addr
|
|
return line
|
|
|
|
|
|
def replace_relative_addresses(file_name: str) -> None:
|
|
with open(file_name, 'r') as f:
|
|
lines = f.readlines()
|
|
|
|
for i in range(len(lines)):
|
|
line = lines[i]
|
|
parts = re.split(r'\s+', line.strip())
|
|
if len(parts) == 5 and parts[2] in JUMP_INSTRUCTIONS:
|
|
line = make_jump_line_absolute(line, parts)
|
|
lines[i] = line
|
|
|
|
if 'call' in line:
|
|
lines[i] = line.replace('#0x', '$')
|
|
|
|
with open(file_name.replace('.asm', '_with_abs_addr.txt'), 'w') as f:
|
|
f.writelines(lines)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Takes the copy and based assembly code from the web interface and
|
|
# replaces the relative address with absolute ones for easier navigation.
|
|
file_name = sys.argv[1]
|
|
replace_relative_addresses(file_name)
|
|
|