• 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.systemz import *
8from xprint import to_x, to_hex, to_x_32
9
10
11SYSZ_CODE = b"\xed\x00\x00\x00\x00\x1a\x5a\x0f\x1f\xff\xc2\x09\x80\x00\x00\x00\x07\xf7\xeb\x2a\xff\xff\x7f\x57\xe3\x01\xff\xff\x7f\x57\xeb\x00\xf0\x00\x00\x24\xb2\x4f\x00\x78\xec\x18\x00\x00\xc1\x7f"
12
13all_tests = (
14        (CS_ARCH_SYSZ, 0, SYSZ_CODE, "SystemZ"),
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 == SYSZ_OP_REG:
31                print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg)))
32            if i.type == SYSZ_OP_ACREG:
33                print("\t\toperands[%u].type: ACREG = %u" % (c, i.reg))
34            if i.type == SYSZ_OP_IMM:
35                print("\t\toperands[%u].type: IMM = 0x%s" % (c, to_x(i.imm)))
36            if i.type == SYSZ_OP_MEM:
37                print("\t\toperands[%u].type: MEM" % c)
38                if i.mem.base != 0:
39                    print("\t\t\toperands[%u].mem.base: REG = %s" \
40                        % (c, insn.reg_name(i.mem.base)))
41                if i.mem.index != 0:
42                    print("\t\t\toperands[%u].mem.index: REG = %s" \
43                        % (c, insn.reg_name(i.mem.index)))
44                if i.mem.length != 0:
45                    print("\t\t\toperands[%u].mem.length: 0x%s" \
46                        % (c, to_x(i.mem.length)))
47                if i.mem.disp != 0:
48                    print("\t\t\toperands[%u].mem.disp: 0x%s" \
49                        % (c, to_x(i.mem.disp)))
50            c += 1
51
52    if insn.cc:
53        print("\tConditional code: %u" % insn.cc)
54
55
56# ## Test class Cs
57def test_class():
58
59    for (arch, mode, code, comment) in all_tests:
60        print("*" * 16)
61        print("Platform: %s" %comment)
62        print("Code: %s" % to_hex(code))
63        print("Disasm:")
64
65        try:
66            md = Cs(arch, mode)
67            md.detail = True
68            for insn in md.disasm(code, 0x1000):
69                print_insn_detail(insn)
70                print ()
71            print("0x%x:\n" % (insn.address + insn.size))
72        except CsError as e:
73            print("ERROR: %s" %e)
74
75
76if __name__ == '__main__':
77    test_class()
78