| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import print_function |
| 6 |
from capstone import * |
| 7 |
from capstone.sparc import * |
| 8 |
|
| 9 |
|
| 10 |
SPARC_CODE = b"\x80\xa0\x40\x02\x85\xc2\x60\x08\x85\xe8\x20\x01\x81\xe8\x00\x00\x90\x10\x20\x01\xd5\xf6\x10\x16\x21\x00\x00\x0a\x86\x00\x40\x02\x01\x00\x00\x00\x12\xbf\xff\xff\x10\xbf\xff\xff\xa0\x02\x00\x09\x0d\xbf\xff\xff\xd4\x20\x60\x00\xd4\x4e\x00\x16\x2a\xc2\x80\x03" |
| 11 |
SPARCV9_CODE = b"\x81\xa8\x0a\x24\x89\xa0\x10\x20\x89\xa0\x1a\x60\x89\xa0\x00\xe0" |
| 12 |
|
| 13 |
all_tests = ( |
| 14 |
(CS_ARCH_SPARC, CS_MODE_BIG_ENDIAN, SPARC_CODE, "Sparc"), |
| 15 |
(CS_ARCH_SPARC, CS_MODE_BIG_ENDIAN+CS_MODE_V9, SPARCV9_CODE, "SparcV9"), |
| 16 |
) |
| 17 |
mask32 = lambda v: v & 0xFFFFFFFF |
| 18 |
|
| 19 |
def print_insn_detail(insn): |
| 20 |
|
| 21 |
print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) |
| 22 |
|
| 23 |
|
| 24 |
if insn.id == 0: |
| 25 |
return |
| 26 |
|
| 27 |
if len(insn.operands) > 0: |
| 28 |
print("\top_count: %u" % len(insn.operands)) |
| 29 |
c = 0 |
| 30 |
for i in insn.operands: |
| 31 |
if i.type == SPARC_OP_REG: |
| 32 |
print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg))) |
| 33 |
if i.type == SPARC_OP_IMM: |
| 34 |
print("\t\toperands[%u].type: IMM = 0x%x" % (c, mask32(i.imm))) |
| 35 |
if i.type == SPARC_OP_MEM: |
| 36 |
print("\t\toperands[%u].type: MEM" % c) |
| 37 |
if i.mem.base != 0: |
| 38 |
print("\t\t\toperands[%u].mem.base: REG = %s" \ |
| 39 |
% (c, insn.reg_name(i.mem.base))) |
| 40 |
if i.mem.index != 0: |
| 41 |
print("\t\t\toperands[%u].mem.index: REG = %s" \ |
| 42 |
% (c, insn.reg_name(i.mem.index))) |
| 43 |
if i.mem.disp != 0: |
| 44 |
print("\t\t\toperands[%u].mem.disp: 0x%x" \ |
| 45 |
% (c, mask32(i.mem.disp))) |
| 46 |
c += 1 |
| 47 |
|
| 48 |
if insn.cc: |
| 49 |
print("\tCode condition: %u" % insn.cc) |
| 50 |
if insn.hint: |
| 51 |
print("\tHint code: %u" % insn.hint) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
def test_class(): |
| 56 |
for (arch, mode, code, comment) in all_tests: |
| 57 |
print("*" * 16) |
| 58 |
print("Platform: %s" % comment) |
| 59 |
print("Code: %s" % code.hex(' ')) |
| 60 |
print("Disasm:") |
| 61 |
|
| 62 |
try: |
| 63 |
md = Cs(arch, mode) |
| 64 |
md.detail = True |
| 65 |
for insn in md.disasm(code, 0x1000): |
| 66 |
print_insn_detail(insn) |
| 67 |
print () |
| 68 |
print("0x%x:\n" % (insn.address + insn.size)) |
| 69 |
except CsError as e: |
| 70 |
print("ERROR: %s" %e) |
| 71 |
|
| 72 |
|
| 73 |
if __name__ == '__main__': |
| 74 |
test_class() |