Rizin
unix-like reverse engineering framework and cli tools
test_ppc.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 from __future__ import print_function
5 from capstone import *
6 from capstone.ppc import *
7 from xprint import to_hex, to_x_32
8 
9 PPC_CODE = b"\x43\x20\x0c\x07\x41\x56\xff\x17\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21\x40\x82\x00\x14"
10 PPC_CODE2 = b"\x10\x60\x2a\x10\x10\x64\x28\x88\x7c\x4a\x5d\x0f"
11 
12 all_tests = (
13  (CS_ARCH_PPC, CS_MODE_BIG_ENDIAN, PPC_CODE, "PPC-64"),
14  (CS_ARCH_PPC, CS_MODE_BIG_ENDIAN + CS_MODE_QPX, PPC_CODE2, "PPC-64 + QPX"),
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 == PPC_OP_REG:
31  print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
32  if i.type == PPC_OP_IMM:
33  print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x_32(i.imm)))
34  if i.type == PPC_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.disp != 0:
40  print("\t\t\toperands[%u].mem.disp: 0x%s" \
41  % (c, to_x_32(i.mem.disp)))
42  if i.type == PPC_OP_CRX:
43  print("\t\toperands[%u].type: CRX" % c)
44  print("\t\t\toperands[%u].crx.scale: = %u" \
45  % (c, i.crx.scale))
46  if i.crx.reg != 0:
47  print("\t\t\toperands[%u].crx.reg: REG = %s" \
48  % (c, insn.reg_name(i.crx.reg)))
49  if i.crx.cond != 0:
50  print("\t\t\toperands[%u].crx.cond: 0x%x" \
51  % (c, i.crx.cond))
52  c += 1
53 
54  if insn.bc:
55  print("\tBranch code: %u" % insn.bc)
56  if insn.bh:
57  print("\tBranch hint: %u" % insn.bh)
58  if insn.update_cr0:
59  print("\tUpdate-CR0: True")
60 
61 
62 # ## Test class Cs
63 def test_class():
64 
65  for (arch, mode, code, comment) in all_tests:
66  print("*" * 16)
67  print("Platform: %s" % comment)
68  print("Code: %s" % to_hex(code))
69  print("Disasm:")
70 
71  try:
72  md = Cs(arch, mode)
73  md.detail = True
74  for insn in md.disasm(code, 0x1000):
75  print_insn_detail(insn)
76  print ()
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()
size_t len
Definition: 6502dis.c:15
def test_class()
Definition: test_ppc.py:63
def print_insn_detail(insn)
Definition: test_ppc.py:18
def to_hex(s, prefix_0x=True)
Definition: xprint.py:9
def to_x_32(s)
Definition: xprint.py:36