#!/usr/bin/env python3
"""Disassemble an arbitrary VA range from section_dump.txt: dis_range.py START END"""
import re, sys
from capstone import Cs, CS_ARCH_X86, CS_MODE_32

DUMP = r"C:\git\bt411\reference\decomp\section_dump.txt"
START = int(sys.argv[1], 16)
END = int(sys.argv[2], 16)

mem = {}
pat = re.compile(r'^\s*([0-9a-f]{6,8})\s((?:[0-9a-f]{2,8}\s?)+)')
with open(DUMP) as f:
    for line in f:
        m = pat.match(line)
        if not m: continue
        addr = int(m.group(1), 16)
        if addr + 16 < START or addr > END: continue
        b = bytes.fromhex(''.join(m.group(2).split()))
        for i, byte in enumerate(b):
            mem[addr+i] = byte

code = bytes(mem.get(a, 0xCC) for a in range(START, END))
md = Cs(CS_ARCH_X86, CS_MODE_32)
for ins in md.disasm(code, START):
    print(f"{ins.address:08x}: {ins.mnemonic:8s} {ins.op_str}")
