blob: e0b4689cd3991c94c648c70cbf3deecd616f975a [file] [log] [blame]
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
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_sparc *sparc = &(ins->detail->sparc);
34
35 if (sparc->op_count)
36 printf("\top_count: %u\n", sparc->op_count);
37
38 int i;
39 for (i = 0; i < sparc->op_count; i++) {
40 cs_sparc_op *op = &(sparc->operands[i]);
41 switch((int)op->type) {
42 default:
43 break;
44 case SPARC_OP_REG:
45 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
46 break;
47 case SPARC_OP_IMM:
48 printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
49 break;
50 case SPARC_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.index != X86_REG_INVALID)
56 printf("\t\t\toperands[%u].mem.index: REG = %s\n",
57 i, cs_reg_name(handle, op->mem.index));
58 if (op->mem.disp != 0)
59 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
60
61 break;
62 }
63 }
64
65 if (sparc->cc != 0)
66 printf("\tCode condition: %u\n", sparc->cc);
67
68 if (sparc->hint != 0)
69 printf("\tHint code: %u\n", sparc->hint);
70
71 printf("\n");
72}
73
74static void test()
75{
76#define SPARC_CODE "\x80\xa0\x40\x02\x85\xc2\x60\x08\x85\xe8\x20\x01\x81\xe8\x00\x00\x90\x10\x20\x01\xd5\xf6\x10\x16\x21\x00\x00\x0a\x86\x00\x40\x02\x01\x00\x00\x00\x12\xbf\xff\xff\x10\xbf\xff\xff\xa0\x02\x00\x09\x0d\xbf\xff\xff\xd4\x20\x60\x00\xd4\x4e\x00\x16\x2a\xc2\x80\x03"
77
Nguyen Anh Quynhea9f4b12014-03-10 20:38:01 +080078#define SPARCV9_CODE "\x81\xa8\x0a\x24\x89\xa0\x10\x20\x89\xa0\x1a\x60\x89\xa0\x00\xe0"
79
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080080 struct platform platforms[] = {
81 {
82 .arch = CS_ARCH_SPARC,
83 .mode = CS_MODE_BIG_ENDIAN,
84 .code = (unsigned char*)SPARC_CODE,
85 .size = sizeof(SPARC_CODE) - 1,
86 .comment = "Sparc",
Nguyen Anh Quynhea9f4b12014-03-10 20:38:01 +080087 },
88 {
89 .arch = CS_ARCH_SPARC,
90 .mode = CS_MODE_BIG_ENDIAN + CS_MODE_V9,
91 .code = (unsigned char*)SPARCV9_CODE,
92 .size = sizeof(SPARCV9_CODE) - 1,
93 .comment = "SparcV9"
94 },
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080095 };
96
97 uint64_t address = 0x1000;
98 cs_insn *insn;
99 int i;
100
101 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
102 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
103 if (err) {
104 printf("Failed on cs_open() with error returned: %u\n", err);
105 continue;
106 }
107
108 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
109
110 size_t count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
111 if (count) {
112 printf("****************\n");
113 printf("Platform: %s\n", platforms[i].comment);
114 print_string_hex("Code:", platforms[i].code, platforms[i].size);
115 printf("Disasm:\n");
116
117 size_t j;
118 for (j = 0; j < count; j++) {
119 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
120 print_insn_detail(&insn[j]);
121 }
122 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
123
124 // free memory allocated by cs_disasm_ex()
125 cs_free(insn, count);
126 } else {
127 printf("****************\n");
128 printf("Platform: %s\n", platforms[i].comment);
129 print_string_hex("Code:", platforms[i].code, platforms[i].size);
130 printf("ERROR: Failed to disasm given code!\n");
131 }
132
133 printf("\n");
134
135 cs_close(&handle);
136 }
137}
138
139int main()
140{
141 test();
142
143 return 0;
144}