| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import print_function |
| 5 |
from capstone import * |
| 6 |
from capstone.m68k import * |
| 7 |
|
| 8 |
M68K_CODE = b"\x4c\x00\x54\x04\x48\xe7\xe0\x30\x4c\xdf\x0c\x07\xd4\x40\x87\x5a\x4e\x71\x02\xb4\xc0\xde\xc0\xde\x5c\x00\x1d\x80\x71\x12\x01\x23\xf2\x3c\x44\x22\x40\x49\x0e\x56\x54\xc5\xf2\x3c\x44\x00\x44\x7a\x00\x00\xf2\x00\x0a\x28\x4e\xb9\x00\x00\x00\x12\x4e\x75" |
| 9 |
|
| 10 |
all_tests = ( |
| 11 |
(CS_ARCH_M68K, CS_MODE_BIG_ENDIAN | CS_MODE_M68K_040, M68K_CODE, "M68K"), |
| 12 |
) |
| 13 |
|
| 14 |
s_addressing_modes = { |
| 15 |
0: "<invalid mode>", |
| 16 |
|
| 17 |
1: "Register Direct - Data", |
| 18 |
2: "Register Direct - Address", |
| 19 |
|
| 20 |
3: "Register Indirect - Address", |
| 21 |
4: "Register Indirect - Address with Postincrement", |
| 22 |
5: "Register Indirect - Address with Predecrement", |
| 23 |
6: "Register Indirect - Address with Displacement", |
| 24 |
|
| 25 |
7: "Address Register Indirect With Index - 8-bit displacement", |
| 26 |
8: "Address Register Indirect With Index - Base displacement", |
| 27 |
|
| 28 |
9: "Memory indirect - Postindex", |
| 29 |
10: "Memory indirect - Preindex", |
| 30 |
|
| 31 |
11: "Program Counter Indirect - with Displacement", |
| 32 |
|
| 33 |
12: "Program Counter Indirect with Index - with 8-Bit Displacement", |
| 34 |
13: "Program Counter Indirect with Index - with Base Displacement", |
| 35 |
|
| 36 |
14: "Program Counter Memory Indirect - Postindexed", |
| 37 |
15: "Program Counter Memory Indirect - Preindexed", |
| 38 |
|
| 39 |
16: "Absolute Data Addressing - Short", |
| 40 |
17: "Absolute Data Addressing - Long", |
| 41 |
18: "Immediate value", |
| 42 |
|
| 43 |
19: "Branch Displacement", |
| 44 |
} |
| 45 |
|
| 46 |
def print_read_write_regs(insn): |
| 47 |
for m in insn.regs_read: |
| 48 |
print("\treading from reg: %s" % insn.reg_name(m)) |
| 49 |
|
| 50 |
for m in insn.regs_write: |
| 51 |
print("\twriting to reg: %s" % insn.reg_name(m)) |
| 52 |
|
| 53 |
def print_insn_detail(insn): |
| 54 |
if len(insn.operands) > 0: |
| 55 |
print("\top_count: %u" % (len(insn.operands))) |
| 56 |
print("\tgroups_count: %u" % len(insn.groups)) |
| 57 |
|
| 58 |
print_read_write_regs(insn) |
| 59 |
|
| 60 |
for i, op in enumerate(insn.operands): |
| 61 |
if op.type == M68K_OP_REG: |
| 62 |
print("\t\toperands[%u].type: REG = %s" % (i, insn.reg_name(op.reg))) |
| 63 |
elif op.type == M68K_OP_IMM: |
| 64 |
print("\t\toperands[%u].type: IMM = 0x%x" % (i, op.imm & 0xffffffff)) |
| 65 |
elif op.type == M68K_OP_MEM: |
| 66 |
print("\t\toperands[%u].type: MEM" % (i)) |
| 67 |
if op.mem.base_reg != M68K_REG_INVALID: |
| 68 |
print("\t\t\toperands[%u].mem.base: REG = %s" % (i, insn.reg_name(op.mem.base_reg))) |
| 69 |
if op.mem.index_reg != M68K_REG_INVALID: |
| 70 |
print("\t\t\toperands[%u].mem.index: REG = %s" % (i, insn.reg_name(op.mem.index_reg))) |
| 71 |
mem_index_str = "w" |
| 72 |
if op.mem.index_size > 0: |
| 73 |
mem_index_str = "l" |
| 74 |
print("\t\t\toperands[%u].mem.index: size = %s" % (i, mem_index_str)) |
| 75 |
if op.mem.disp != 0: |
| 76 |
print("\t\t\toperands[%u].mem.disp: 0x%x" % (i, op.mem.disp)) |
| 77 |
if op.mem.scale != 0: |
| 78 |
print("\t\t\toperands[%u].mem.scale: %d" % (i, op.mem.scale)) |
| 79 |
print("\t\taddress mode: %s" % (s_addressing_modes[op.address_mode])) |
| 80 |
elif op.type == M68K_OP_FP_SINGLE: |
| 81 |
print("\t\toperands[%u].type: FP_SINGLE" % i) |
| 82 |
print("\t\toperands[%u].simm: %f", i, op.simm) |
| 83 |
elif op.type == M68K_OP_FP_DOUBLE: |
| 84 |
print("\t\toperands[%u].type: FP_DOUBLE" % i) |
| 85 |
print("\t\toperands[%u].dimm: %lf", i, op.dimm) |
| 86 |
elif op.type == M68K_OP_BR_DISP: |
| 87 |
print("\t\toperands[%u].br_disp.disp: 0x%x" % (i, op.br_disp.disp)) |
| 88 |
print("\t\toperands[%u].br_disp.disp_size: %d" % (i, op.br_disp.disp_size)) |
| 89 |
print() |
| 90 |
|
| 91 |
|
| 92 |
def test_class(): |
| 93 |
address = 0x01000 |
| 94 |
for (arch, mode, code, comment) in all_tests: |
| 95 |
print("*" * 16) |
| 96 |
print("Platform: %s" % comment) |
| 97 |
print("Code: %s " % code.hex(' ')) |
| 98 |
print("Disasm:") |
| 99 |
|
| 100 |
try: |
| 101 |
md = Cs(arch, mode) |
| 102 |
md.detail = True |
| 103 |
last_address = 0 |
| 104 |
for insn in md.disasm(code, address): |
| 105 |
last_address = insn.address + insn.size |
| 106 |
print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) |
| 107 |
print_insn_detail(insn) |
| 108 |
print("0x%x:\n" % (last_address)) |
| 109 |
|
| 110 |
except CsError as e: |
| 111 |
print("ERROR: %s" % e.__str__()) |
| 112 |
|
| 113 |
if __name__ == '__main__': |
| 114 |
test_class() |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|