1#!/usr/bin/env python
2
3# Capstone Python bindings, by Sebastian Macke <Sebastian Macke>
4from __future__ import print_function
5from capstone import *
6from capstone.mos65xx import *
7from xprint import to_hex, to_x
8
9MOS65XX_CODE = b"\x0d\x34\x12\x00\x81\x65\x6c\x01\x00\x85\xFF\x10\x00\x19\x42\x42\x00\x49\x42"
10
11address_modes=[
12    'No address mode',
13    'implied addressing (no addressing mode)',
14    'accumulator addressing',
15    'absolute addressing',
16    'zeropage addressing',
17    '8 Bit immediate value',
18    'indexed absolute addressing by the X index register',
19    'indexed absolute addressing by the Y index register',
20    'indexed indirect addressing by the X index register',
21    'indirect indexed addressing by the Y index register',
22    'indexed zeropage addressing by the X index register',
23    'indexed zeropage addressing by the Y index register',
24    'relative addressing used by branches',
25    'absolute indirect addressing'
26];
27
28
29def print_insn_detail(insn):
30    # print address, mnemonic and operands
31    print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
32
33    # "data" instruction generated by SKIPDATA option has no detail
34    if insn.id == 0:
35        return
36    print("\taddress mode: %s" % (address_modes[insn.am]))
37    print("\tmodifies flags: %s" % ('true' if insn.modifies_flags != 0 else 'false'))
38    if len(insn.operands) > 0:
39        print("\top_count: %u" % len(insn.operands))
40        c = -1
41        for i in insn.operands:
42            c += 1
43            if i.type == MOS65XX_OP_REG:
44                print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
45            if i.type == MOS65XX_OP_IMM:
46                print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x(i.imm)))
47            if i.type == MOS65XX_OP_MEM:
48                print("\t\toperands[%u].type: MEM = 0x%s" % (c, to_x(i.mem)))
49
50
51# ## Test class Cs
52def test_class():
53    print("*" * 16)
54    print("Platform: %s" % "MOS65XX")
55    print("Code: %s" % to_hex(MOS65XX_CODE))
56    print("Disasm:")
57
58    try:
59        md = Cs(CS_ARCH_MOS65XX, 0)
60        md.detail = True
61        for insn in md.disasm(MOS65XX_CODE, 0x1000):
62            print_insn_detail(insn)
63            print()
64
65        print("0x%x:\n" % (insn.address + insn.size))
66    except CsError as e:
67        print("ERROR: %s" % e)
68
69
70if __name__ == '__main__':
71    test_class()
72