def disasm(data, addr): if not data: return arch_dis = get_arch_dis() #if 'binaryninja' in sys.modules: # return utils.disasm(data, addr, arch_dis) if arch == 'z80': from z80dis import z80 lines = [] while data: decoded = z80.decode(data, addr) lines.append(z80.disasm(decoded)) data = data[decoded.len:] addr += decoded.len return '\n'.join(lines) else: import capstone offset = 0 lines = [] if arch_dis == 'x86_64': md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64) elif arch_dis == 'x86': md = capstone.Cs(capstone.CS_ARCH_X86, 0) for i in md.disasm(data, addr): addrstr = '%s%016X%s' % (GREEN, i.address, NORMAL) bytestr = hexlify(data[offset:offset+i.size]).decode('utf-8').ljust(16) asmstr = i.mnemonic + ' ' + i.op_str line = '%s: %s %s' % (addrstr, bytestr, asmstr) lines.append(line) offset += i.size return '\n'.join(lines)
def disasm1(data, addr): if not data: return arch_dis = get_arch_dis() #if 'binaryninja' in sys.modules: # return utils.disasm1(data, addr, arch_dis) if arch == 'z80': from z80dis import z80 decoded = z80.decode(data, addr) return (z80.disasm(decoded), decoded.len) else: import capstone if arch_dis == 'x86_64': md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64) elif arch_dis == 'x86': md = capstone.Cs(capstone.CS_ARCH_X86, 0) gen = md.disasm(data, addr) insn = next(gen) return ('%s %s' % (insn.mnemonic, insn.op_str), insn.size)
def doit(data): decoded = z80.decode(data, ADDR) hexstr = hexlify(data[0:decoded.len]).decode('utf-8') disasm = z80.disasm(decoded) print('%04X: %s %s' % (ADDR, hexstr, disasm))
#!/usr/bin/env python # "what can JP instructions look like?" # ./enum65536.py | grep jp | sort | unique # jp (hl); jp (ix); jp (iy); jp <hex>; jp <cc>,<hex> # "what can JR instructions look like? # ./enum65536.py | grep jp | sort | unique # jr <hex>; jr <cc>,<hex> from z80dis import z80 from struct import pack from binascii import hexlify ADDR = 0xDEAD for i in range(65536): data = pack('>H', i) + b'\xAB\xCD\xEF\x00' decoded = z80.decode(data, ADDR) hexstr = hexlify(data[0:decoded.len]).decode('utf-8') disasm = z80.disasm(decoded) print('%s %04X: %s' % (disasm.ljust(16), ADDR, hexstr))