Debian Codegraph @

bindings/python/tests/test_xcore.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Capstone Python bindings, by Nguyen Anh Quynnh <[email protected]>
4
5 from __future__ import print_function
6 from capstone import *
7 from capstone.xcore import *
8
9
10 XCORE_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"
11 mask64 = lambda v: v & 0xFFFFFFFFFFFFFFFF
12
13 all_tests = (
14 (CS_ARCH_XCORE, 0, XCORE_CODE, "XCore"),
15 )
16
17
18 def 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%x" % (c, mask64(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%x" \
44 % (c, mask64(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
51 def test_class():
52
53 for (arch, mode, code, comment) in all_tests:
54 print("*" * 16)
55 print("Platform: %s" %comment)
56 print("Code: %s" % code.hex(' '))
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
70 if __name__ == '__main__':
71 test_class()