• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
4from __future__ import print_function
5from capstone import *
6from capstone.ppc import *
7from xprint import to_x, to_hex, to_x_32
8
9PPC_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
11all_tests = (
12        (CS_ARCH_PPC, CS_MODE_BIG_ENDIAN, PPC_CODE, "PPC-64"),
13        )
14
15
16def print_insn_detail(insn):
17    # print address, mnemonic and operands
18    print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str))
19
20    # "data" instruction generated by SKIPDATA option has no detail
21    if insn.id == 0:
22        return
23
24    if len(insn.operands) > 0:
25        print("\top_count: %u" % len(insn.operands))
26        c = 0
27        for i in insn.operands:
28            if i.type == PPC_OP_REG:
29                print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
30            if i.type == PPC_OP_IMM:
31                print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x_32(i.imm)))
32            if i.type == PPC_OP_MEM:
33                print("\t\toperands[%u].type: MEM" % c)
34                if i.mem.base != 0:
35                    print("\t\t\toperands[%u].mem.base: REG = %s" \
36                        % (c, insn.reg_name(i.mem.base)))
37                if i.mem.disp != 0:
38                    print("\t\t\toperands[%u].mem.disp: 0x%s" \
39                        % (c, to_x_32(i.mem.disp)))
40            if i.type == PPC_OP_CRX:
41                print("\t\toperands[%u].type: CRX" % c)
42                print("\t\t\toperands[%u].crx.scale: = %u" \
43                        % (c, i.crx.scale))
44                if i.crx.reg != 0:
45                    print("\t\t\toperands[%u].crx.reg: REG = %s" \
46                        % (c, insn.reg_name(i.crx.reg)))
47                if i.crx.cond != 0:
48                    print("\t\t\toperands[%u].crx.cond: 0x%x" \
49                        % (c, i.crx.cond))
50            c += 1
51
52    if insn.bc:
53        print("\tBranch code: %u" % insn.bc)
54    if insn.bh:
55        print("\tBranch hint: %u" % insn.bh)
56    if insn.update_cr0:
57        print("\tUpdate-CR0: True")
58
59
60# ## Test class Cs
61def test_class():
62
63    for (arch, mode, code, comment) in all_tests:
64        print("*" * 16)
65        print("Platform: %s" % comment)
66        print("Code: %s" % to_hex(code))
67        print("Disasm:")
68
69        try:
70            md = Cs(arch, mode)
71            md.detail = True
72            for insn in md.disasm(code, 0x1000):
73                print_insn_detail(insn)
74                print ()
75            print("0x%x:\n" % (insn.address + insn.size))
76        except CsError as e:
77            print("ERROR: %s" % e)
78
79
80if __name__ == '__main__':
81    test_class()
82