Nguyen Anh Quynh | 0b96545 | 2015-04-26 22:54:41 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2015 */ |
| 3 | |
| 4 | // This sample code demonstrates the option CS_OPT_MNEMONIC |
| 5 | // to customize instruction mnemonic. |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include "../myinttypes.h" |
| 10 | |
| 11 | #include <capstone/capstone.h> |
| 12 | |
| 13 | #define X86_CODE32 "\x75\x01" |
| 14 | |
| 15 | // Print out the input code in hexadecimal format |
| 16 | static void print_string_hex(unsigned char *str, size_t len) |
| 17 | { |
| 18 | unsigned char *c; |
| 19 | |
| 20 | for (c = str; c < str + len; c++) { |
| 21 | printf("%02x ", *c & 0xff); |
| 22 | } |
| 23 | printf("\t"); |
| 24 | } |
| 25 | |
| 26 | // Print one instruction |
| 27 | static void print_insn(csh handle) |
| 28 | { |
| 29 | cs_insn *insn; |
| 30 | size_t count; |
| 31 | |
| 32 | count = cs_disasm(handle, (const uint8_t *)X86_CODE32, sizeof(X86_CODE32) - 1, 0x1000, 1, &insn); |
| 33 | if (count) { |
Nguyen Anh Quynh | c0b1de3 | 2015-04-26 23:15:08 +0800 | [diff] [blame] | 34 | print_string_hex((unsigned char *)X86_CODE32, sizeof(X86_CODE32) - 1); |
Nguyen Anh Quynh | 0b96545 | 2015-04-26 22:54:41 +0800 | [diff] [blame] | 35 | printf("\t%s\t%s\n", insn[0].mnemonic, insn[0].op_str); |
| 36 | // Free memory allocated by cs_disasm() |
| 37 | cs_free(insn, count); |
| 38 | } else |
| 39 | printf("ERROR: Failed to disasm given code!\n"); |
| 40 | } |
| 41 | |
| 42 | static void test() |
| 43 | { |
| 44 | csh handle; |
| 45 | cs_err err; |
| 46 | // Customize mnemonic JNE to "jnz" |
| 47 | cs_opt_mnem my_mnem = { X86_INS_JNE, "jnz" }; |
| 48 | // Set .mnemonic to NULL to reset to default mnemonic |
| 49 | cs_opt_mnem default_mnem = { X86_INS_JNE, NULL }; |
| 50 | |
| 51 | err = cs_open(CS_ARCH_X86, CS_MODE_32, &handle); |
| 52 | if (err) { |
| 53 | printf("Failed on cs_open() with error returned: %u\n", err); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // 1. Print out the instruction in default setup. |
| 58 | printf("Disassemble X86 code with default instruction mnemonic\n"); |
Nguyen Anh Quynh | 0b96545 | 2015-04-26 22:54:41 +0800 | [diff] [blame] | 59 | print_insn(handle); |
| 60 | |
| 61 | // Customized mnemonic JNE to JNZ using CS_OPT_MNEMONIC option |
| 62 | printf("\nNow customize engine to change mnemonic from 'JNE' to 'JNZ'\n"); |
| 63 | cs_option(handle, CS_OPT_MNEMONIC, (size_t)&my_mnem); |
| 64 | |
| 65 | // 2. Now print out the instruction in newly customized setup. |
Nguyen Anh Quynh | 0b96545 | 2015-04-26 22:54:41 +0800 | [diff] [blame] | 66 | print_insn(handle); |
| 67 | |
| 68 | // Reset engine to use the default mnemonic of JNE |
| 69 | printf("\nReset engine to use the default mnemonic\n"); |
| 70 | cs_option(handle, CS_OPT_MNEMONIC, (size_t)&default_mnem); |
| 71 | |
| 72 | // 3. Now print out the instruction in default setup. |
Nguyen Anh Quynh | 0b96545 | 2015-04-26 22:54:41 +0800 | [diff] [blame] | 73 | print_insn(handle); |
| 74 | |
| 75 | // Done |
| 76 | cs_close(&handle); |
| 77 | } |
| 78 | |
| 79 | int main() |
| 80 | { |
| 81 | test(); |
| 82 | |
| 83 | return 0; |
| 84 | } |