blob: 0a925ca77505675443222448065d6e77738add6f [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)
63 printf("\tBranch code: %u\n", ins->detail->ppc.bc);
64
65 if (ppc->update_cr0)
66 printf("\tUpdate-CR0: True\n");
67
68 printf("\n");
69}
70
71static void test()
72{
73#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"
74
75 struct platform platforms[] = {
76 {
77 .arch = CS_ARCH_PPC,
78 .mode = CS_MODE_BIG_ENDIAN,
79 .code = (unsigned char*)PPC_CODE,
80 .size = sizeof(PPC_CODE) - 1,
81 .comment = "PPC-64",
82 }
83 };
84
85 uint64_t address = 0x1000;
86 cs_insn *insn;
87 int i;
88
89 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
90 if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
91 return;
92
93 size_t count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
94 if (count) {
95 printf("****************\n");
96 printf("Platform: %s\n", platforms[i].comment);
97 print_string_hex("Code:", platforms[i].code, platforms[i].size);
98 printf("Disasm:\n");
99
100 size_t j;
101 for (j = 0; j < count; j++) {
102 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
103 print_insn_detail(&insn[j]);
104 }
105 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
106
107 // free memory allocated by cs_disasm_ex()
108 cs_free(insn, count);
109 } else {
110 printf("****************\n");
111 printf("Platform: %s\n", platforms[i].comment);
112 print_string_hex("Code:", platforms[i].code, platforms[i].size);
113 printf("ERROR: Failed to disasm given code!\n");
114 }
115
116 printf("\n");
117
118 cs_close(handle);
119 }
120}
121
122int main()
123{
124 test();
125
126 return 0;
127}