• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
4
5from __future__ import print_function
6from capstone import *
7from capstone.xcore import *
8from xprint import to_x, to_hex
9
10
11XCORE_CODE = b"\xfe\x0f\xfe\x17\x13\x17\xc6\xfe\xec\x17\x97\xf8\xec\x4f\x1f\xfd\xec\x37\x07\xf2\x45\x5b\xf9\xfa\x02\x06\x1b\x10\x09\xfd\xec\xa7"
12
13all_tests = (
14        (CS_ARCH_XCORE, 0, XCORE_CODE, "XCore"),
15)
16
17
18def print_insn_detail(insn):
19    # print address, mnemonic and operands
20    print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
21
22    # "data" instruction generated by SKIPDATA option has no detail
23    if insn.id == 0:
24        return
25
26    if len(insn.operands) > 0:
27        print("\top_count: %u" % len(insn.operands))
28        c = 0
29        for i in insn.operands:
30            if i.type == XCORE_OP_REG:
31                print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
32            if i.type == XCORE_OP_IMM:
33                print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x(i.imm)))
34            if i.type == XCORE_OP_MEM:
35                print("\t\toperands[%u].type: MEM" % c)
36                if i.mem.base != 0:
37                    print("\t\t\toperands[%u].mem.base: REG = %s" \
38                        % (c, insn.reg_name(i.mem.base)))
39                if i.mem.index != 0:
40                    print("\t\t\toperands[%u].mem.index: REG = %s" \
41                        % (c, insn.reg_name(i.mem.index)))
42                if i.mem.disp != 0:
43                    print("\t\t\toperands[%u].mem.disp: 0x%s" \
44                        % (c, to_x(i.mem.disp)))
45                if i.mem.direct != 1:
46                    print("\t\t\toperands[%u].mem.direct: -1" % c)
47            c += 1
48
49
50# ## Test class Cs
51def test_class():
52
53    for (arch, mode, code, comment) in all_tests:
54        print("*" * 16)
55        print("Platform: %s" %comment)
56        print("Code: %s" % to_hex(code))
57        print("Disasm:")
58
59        try:
60            md = Cs(arch, mode)
61            md.detail = True
62            for insn in md.disasm(code, 0x1000):
63                print_insn_detail(insn)
64                print ()
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