File: test_customized_mnem.py

package info (click to toggle)
capstone 5.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 58,188 kB
  • sloc: ansic: 96,086; cpp: 67,489; cs: 29,510; python: 25,829; pascal: 24,412; java: 15,582; ml: 14,473; makefile: 1,275; sh: 479; ruby: 386
file content (40 lines) | stat: -rwxr-xr-x 976 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python

# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>

from __future__ import print_function
from capstone import *
from capstone.x86 import *
from xprint import to_hex


X86_CODE32 = b"\x75\x01"


def print_insn(md, code):
    print("%s\t" % to_hex(code, False), end="")

    for insn in md.disasm(code, 0x1000):
        print("\t%s\t%s\n" % (insn.mnemonic, insn.op_str))


def test():
    try:
        md = Cs(CS_ARCH_X86, CS_MODE_32)

        print("Disassemble X86 code with default instruction mnemonic")
        print_insn(md, X86_CODE32)

        print("Now customize engine to change mnemonic from 'JNE' to 'JNZ'")
        md.mnemonic_setup(X86_INS_JNE, "jnz")
        print_insn(md, X86_CODE32)

        print("Reset engine to use the default mnemonic")
        md.mnemonic_setup(X86_INS_JNE, None)
        print_insn(md, X86_CODE32)
    except CsError as e:
        print("ERROR: %s" % e)


if __name__ == '__main__':
    test()