| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import print_function |
| 6 |
from capstone import * |
| 7 |
from capstone.riscv import * |
| 8 |
|
| 9 |
RISCV_CODE32 = b"\x37\x34\x00\x00\x97\x82\x00\x00\xef\x00\x80\x00\xef\xf0\x1f\xff\xe7\x00\x45\x00\xe7\x00\xc0\xff\x63\x05\x41\x00\xe3\x9d\x61\xfe\x63\xca\x93\x00\x63\x53\xb5\x00\x63\x65\xd6\x00\x63\x76\xf7\x00\x03\x88\x18\x00\x03\x99\x49\x00\x03\xaa\x6a\x00\x03\xcb\x2b\x01\x03\xdc\x8c\x01\x23\x86\xad\x03\x23\x9a\xce\x03\x23\x8f\xef\x01\x93\x00\xe0\x00\x13\xa1\x01\x01\x13\xb2\x02\x7d\x13\xc3\x03\xdd\x13\xe4\xc4\x12\x13\xf5\x85\x0c\x13\x96\xe6\x01\x13\xd7\x97\x01\x13\xd8\xf8\x40\x33\x89\x49\x01\xb3\x0a\x7b\x41\x33\xac\xac\x01\xb3\x3d\xde\x01\x33\xd2\x62\x40\xb3\x43\x94\x00\x33\xe5\xc5\x00\xb3\x76\xf7\x00\xb3\x54\x39\x01\xb3\x50\x31\x00\x33\x9f\x0f\x00" |
| 10 |
RISCV_CODE64 = b"\x13\x04\xa8\x7a" |
| 11 |
mask64 = lambda v: v & 0xFFFFFFFFFFFFFFFF |
| 12 |
all_tests = ( |
| 13 |
(CS_ARCH_RISCV, CS_MODE_RISCV32, RISCV_CODE32, "riscv32"), |
| 14 |
(CS_ARCH_RISCV, CS_MODE_RISCV64, RISCV_CODE64, "riscv64"), |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def print_insn_detail(insn): |
| 19 |
|
| 20 |
print("0x%x:\t%s\t%s" % (insn.address, insn.mnemonic, insn.op_str)) |
| 21 |
|
| 22 |
|
| 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 == RISCV_OP_REG: |
| 31 |
print("\t\toperands[%u].type: REG = %s" % (c, insn.reg_name(i.reg))) |
| 32 |
if i.type == RISCV_OP_IMM: |
| 33 |
print("\t\toperands[%u].type: IMM = 0x%x" % (c, mask64(i.imm))) |
| 34 |
if i.type == RISCV_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%x" \ |
| 41 |
% (c, mask64(i.mem.disp))) |
| 42 |
c += 1 |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
def test_class(): |
| 47 |
|
| 48 |
for (arch, mode, code, comment) in all_tests: |
| 49 |
print("*" * 16) |
| 50 |
print("Platform: %s" %comment) |
| 51 |
print("Code: %s" % code.hex(' ')) |
| 52 |
print("Disasm:") |
| 53 |
|
| 54 |
try: |
| 55 |
md = Cs(arch, mode) |
| 56 |
md.detail = True |
| 57 |
for insn in md.disasm(code, 0x1000): |
| 58 |
print_insn_detail(insn) |
| 59 |
print () |
| 60 |
print("0x%x:\n" % (insn.address + insn.size)) |
| 61 |
except CsError as e: |
| 62 |
print("ERROR: %s" %e) |
| 63 |
|
| 64 |
|
| 65 |
if __name__ == '__main__': |
| 66 |
test_class() |