Rizin
unix-like reverse engineering framework and cli tools
test_xcore.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
4 
5 from __future__ import print_function
6 from capstone import *
7 from capstone.xcore import *
8 from xprint import to_x, to_hex
9 
10 
11 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"
12 
13 all_tests = (
14  (CS_ARCH_XCORE, 0, XCORE_CODE, "XCore"),
15 )
16 
17 
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
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" % 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 
70 if __name__ == '__main__':
71  test_class()
size_t len
Definition: 6502dis.c:15
def print_insn_detail(insn)
Definition: test_xcore.py:18
def test_class()
Definition: test_xcore.py:51
def to_hex(s, prefix_0x=True)
Definition: xprint.py:9
def to_x(s)
Definition: xprint.py:29