capstone/bindings/python/test_m68k.py
2015-10-02 20:47:00 +02:00

74 lines
2.3 KiB
Python
Executable file

#!/usr/bin/env python
# Capstone Python bindings, by Nicolas PLANEL <nplanel@gmail.com>
from __future__ import print_function
from capstone import *
from capstone.m68k import *
from xprint import to_hex, to_x
M68000_CODE = b"\x04\x40\x00\x40"
#"\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56"
#b"\x04\x40\x00\x40"
all_tests = (
(CS_ARCH_M68K, CS_MODE_BIG_ENDIAN, M68000_CODE, "M68000-32 (Big-endian)"),
)
def mnemonic_type_name(t):
if t == 1:
return "CPU"
if t == 2:
return "FPU"
return "Invalid"
def print_insn_detail(insn):
# print address, mnemonic and operands
print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
# "data" instruction generated by SKIPDATA option has no detail
if insn.id == 0:
return
if len(insn.operands) > 0:
print("\top_count: %u" % len(insn.operands))
print("\top_size: type %s size %u" \
% (mnemonic_type_name(insn.op_size.type), insn.op_size.size))
for c,i in enumerate(insn.operands):
if i.type == M68K_OP_REG:
print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
if i.type == M68K_OP_IMM:
print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x(i.imm)))
if i.type == M68K_OP_MEM:
print("\t\toperands[%u].type: MEM" % c)
if i.mem.base_reg != 0:
print("\t\t\toperands[%u].mem.base_reg: REG = %s" \
% (c, insn.reg_name(i.mem.base_reg)))
if i.mem.disp != 0:
print("\t\t\toperands[%u].mem.disp: 0x%s" \
% (c, to_x(i.mem.disp)))
# ## Test class Cs
def test_class():
for (arch, mode, code, comment) in all_tests:
print("*" * 16)
print("Platform: %s" % comment)
print("Code: %s" % to_hex(code))
print("Disasm:")
try:
md = Cs(arch, mode)
md.detail = True
for insn in md.disasm(code, 0x0000):
print_insn_detail(insn)
print()
print("0x%x:\n" % (insn.address + insn.size))
except CsError as e:
print("ERROR: %s" % e)
if __name__ == '__main__':
test_class()