| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import print_function |
| 5 |
from capstone import * |
| 6 |
from capstone.mos65xx import * |
| 7 |
|
| 8 |
MOS65XX_CODE = b"\x0d\x34\x12\x00\x81\x65\x6c\x01\x00\x85\xFF\x10\x00\x19\x42\x42\x00\x49\x42" |
| 9 |
mask64 = lambda v: v & 0xFFFFFFFFFFFFFFFF |
| 10 |
|
| 11 |
address_modes=[ |
| 12 |
"No address mode", |
| 13 |
"implied", |
| 14 |
"accumulator", |
| 15 |
"immediate value", |
| 16 |
"relative", |
| 17 |
"interrupt signature", |
| 18 |
"block move", |
| 19 |
"zero page", |
| 20 |
"zero page indexed with x", |
| 21 |
"zero page indexed with y", |
| 22 |
"relative bit branch", |
| 23 |
"zero page indirect", |
| 24 |
"zero page indexed with x indirect", |
| 25 |
"zero page indirect indexed with y", |
| 26 |
"zero page indirect long", |
| 27 |
"zero page indirect long indexed with y", |
| 28 |
"absolute", |
| 29 |
"absolute indexed with x", |
| 30 |
"absolute indexed with y", |
| 31 |
"absolute indirect", |
| 32 |
"absolute indexed with x indirect", |
| 33 |
"absolute indirect long", |
| 34 |
"absolute long", |
| 35 |
"absolute long indexed with x", |
| 36 |
"stack relative", |
| 37 |
"stack relative indirect indexed with y", |
| 38 |
]; |
| 39 |
|
| 40 |
|
| 41 |
def print_insn_detail(insn): |
| 42 |
|
| 43 |
print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) |
| 44 |
|
| 45 |
|
| 46 |
if insn.id == 0: |
| 47 |
return |
| 48 |
print("\taddress mode: %s" % (address_modes[insn.am])) |
| 49 |
print("\tmodifies flags: %s" % ('true' if insn.modifies_flags != 0 else 'false')) |
| 50 |
if len(insn.operands) > 0: |
| 51 |
print("\top_count: %u" % len(insn.operands)) |
| 52 |
c = -1 |
| 53 |
for i in insn.operands: |
| 54 |
c += 1 |
| 55 |
if i.type == MOS65XX_OP_REG: |
| 56 |
print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg))) |
| 57 |
if i.type == MOS65XX_OP_IMM: |
| 58 |
print("\t\toperands[%u].type: IMM = 0x%x" % (c, mask64(i.imm))) |
| 59 |
if i.type == MOS65XX_OP_MEM: |
| 60 |
print("\t\toperands[%u].type: MEM = 0x%x" % (c, mask64(i.mem))) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
def test_class(): |
| 65 |
print("*" * 16) |
| 66 |
print("Platform: %s" % "MOS65XX") |
| 67 |
print("Code: %s" % MOS65XX_CODE.hex(' ')) |
| 68 |
print("Disasm:") |
| 69 |
|
| 70 |
try: |
| 71 |
md = Cs(CS_ARCH_MOS65XX, 0) |
| 72 |
md.detail = True |
| 73 |
for insn in md.disasm(MOS65XX_CODE, 0x1000): |
| 74 |
print_insn_detail(insn) |
| 75 |
print() |
| 76 |
|
| 77 |
print("0x%x:\n" % (insn.address + insn.size)) |
| 78 |
except CsError as e: |
| 79 |
print("ERROR: %s" % e) |
| 80 |
|
| 81 |
|
| 82 |
if __name__ == '__main__': |
| 83 |
test_class() |