blob: 30d1d2d02f88b0ecb80f67b7d970d34ee7bbfe27 [file] [log] [blame]
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
4#include <stdio.h>
5#include <inttypes.h>
6
7#include <capstone.h>
8
9struct platform {
10 cs_arch arch;
11 cs_mode mode;
12 unsigned char *code;
13 size_t size;
14 char *comment;
15};
16
17static csh handle;
18
19static void print_string_hex(char *comment, unsigned char *str, int len)
20{
21 unsigned char *c;
22
23 printf("%s", comment);
24 for (c = str; c < str + len; c++) {
25 printf("0x%02x ", *c & 0xff);
26 }
27
28 printf("\n");
29}
30
31static void print_insn_detail(cs_insn *ins)
32{
33 cs_ppc *ppc = &(ins->detail->ppc);
34
35 if (ppc->op_count)
36 printf("\top_count: %u\n", ppc->op_count);
37
38 int i;
39 for (i = 0; i < ppc->op_count; i++) {
40 cs_ppc_op *op = &(ppc->operands[i]);
41 switch((int)op->type) {
42 default:
43 break;
44 case PPC_OP_REG:
45 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
46 break;
47 case PPC_OP_IMM:
48 printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
49 break;
50 case PPC_OP_MEM:
51 printf("\t\toperands[%u].type: MEM\n", i);
52 if (op->mem.base != X86_REG_INVALID)
53 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
54 i, cs_reg_name(handle, op->mem.base));
55 if (op->mem.disp != 0)
56 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
57
58 break;
59 }
60 }
61
62 if (ppc->bc != 0)
Nguyen Anh Quynhf122ae02014-01-05 21:45:30 +080063 printf("\tBranch code: %u\n", ppc->bc);
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080064
Nguyen Anh Quynh91e532d2014-01-05 09:15:42 +080065 if (ppc->bh != 0)
Nguyen Anh Quynhf122ae02014-01-05 21:45:30 +080066 printf("\tBranch hint: %u\n", ppc->bh);
Nguyen Anh Quynh91e532d2014-01-05 09:15:42 +080067
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080068 if (ppc->update_cr0)
69 printf("\tUpdate-CR0: True\n");
70
71 printf("\n");
72}
73
74static void test()
75{
76#define PPC_CODE "\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21"
77
78 struct platform platforms[] = {
79 {
80 .arch = CS_ARCH_PPC,
81 .mode = CS_MODE_BIG_ENDIAN,
82 .code = (unsigned char*)PPC_CODE,
83 .size = sizeof(PPC_CODE) - 1,
84 .comment = "PPC-64",
85 }
86 };
87
88 uint64_t address = 0x1000;
89 cs_insn *insn;
90 int i;
91
92 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
93 if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
94 return;
95
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +080096 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
97
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080098 size_t count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
99 if (count) {
100 printf("****************\n");
101 printf("Platform: %s\n", platforms[i].comment);
102 print_string_hex("Code:", platforms[i].code, platforms[i].size);
103 printf("Disasm:\n");
104
105 size_t j;
106 for (j = 0; j < count; j++) {
107 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
108 print_insn_detail(&insn[j]);
109 }
110 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
111
112 // free memory allocated by cs_disasm_ex()
113 cs_free(insn, count);
114 } else {
115 printf("****************\n");
116 printf("Platform: %s\n", platforms[i].comment);
117 print_string_hex("Code:", platforms[i].code, platforms[i].size);
118 printf("ERROR: Failed to disasm given code!\n");
119 }
120
121 printf("\n");
122
123 cs_close(handle);
124 }
125}
126
127int main()
128{
129 test();
130
131 return 0;
132}