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)
Example #2
0
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)
Example #3
0
def differential_disassemble(data, addr):
    if data[0] in [0xCD, 0xED]:
        (x,y,z,p,q) = xyz(data[1])
    else:
        (x,y,z,p,q) = xyz(data[0])

    dc = z80.decode(data, addr)
    a = z80.decoded2str(dc)
    alen = dc.len
    [b, blen] = disasm_libopcodes(data, addr)
    [c, clen] = disasm_z80ex(data, addr)

    # comparison versions
    a_ = normalize(a)
    b_ = normalize(b)
    c_ = normalize(c)

    hexstr = ' '.join(map(lambda x: '%02X'%x, data))
    print('(%d,%d,%d) %04X: %s -' % \
        (alen, blen, clen, addr, hexstr), end='')

    # PASSED, we agree with one of the two oracles
    if a_ == b_ or a_ == c_:
        print(GREEN+a+NORMAL+'-    -'+b+'-    -'+c+'-')
    # PASS, we nop and z80ex errors
    elif a_ == 'nop' and c_ == 'ERROR':
        print(a+'-    -'+b+'-    -'+c+'-'+YELLOW+' PASS'+NORMAL)
    # FAIL
    else:
        print(RED+a+NORMAL+'-    -'+b+'-    -'+c+'-')
        print('comparator a: -%s-' % a_)
        print('comparator b: -%s-' % b_)
        print('comparator c: -%s-' % c_)
        xyz_print(data[0])
        sys.exit(-1)

    # COMPARE LENGTH
    if alen != clen:
        if data[0:2] in [b'\xDD\xCB', b'\xFD\xCB'] and alen == clen-1:
            print(YELLOW + 'length mismatch, our %d vs. z80ex\'s %d is OK for DDCB/FDCB' % (alen, clen) + NORMAL)
        else: 
            print(RED + 'length mismatch, our %d vs. z80ex\'s %d' % (alen, clen) + NORMAL)
            sys.exit(-1)
Example #4
0
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))
Example #5
0
#!/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))