1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
3
4 #include <stdio.h>
5 #include <assert.h>
6
7 #include <capstone/platform.h>
8 #include <capstone/capstone.h>
9
10 struct platform {
11 cs_arch arch;
12 cs_mode mode;
13 unsigned char* code;
14 size_t size;
15 const char* comment;
16 };
17
18 static csh handle;
19
print_string_hex(const char * comment,unsigned char * str,size_t len)20 static void print_string_hex(const char* comment, unsigned char* str, size_t len)
21 {
22 unsigned char *c;
23
24 printf("%s", comment);
25 for (c = str; c < str + len; c++) {
26 printf("0x%02x ", *c & 0xff);
27 }
28
29 printf("\n");
30 }
31
32 const char* s_addressing_modes[] = {
33 "<invalid mode>",
34
35 "Register Direct - Data",
36 "Register Direct - Address",
37
38 "Register Indirect - Address",
39 "Register Indirect - Address with Postincrement",
40 "Register Indirect - Address with Predecrement",
41 "Register Indirect - Address with Displacement",
42
43 "Address Register Indirect With Index - 8-bit displacement",
44 "Address Register Indirect With Index - Base displacement",
45
46 "Memory indirect - Postindex",
47 "Memory indirect - Preindex",
48
49 "Program Counter Indirect - with Displacement",
50
51 "Program Counter Indirect with Index - with 8-Bit Displacement",
52 "Program Counter Indirect with Index - with Base Displacement",
53
54 "Program Counter Memory Indirect - Postindexed",
55 "Program Counter Memory Indirect - Preindexed",
56
57 "Absolute Data Addressing - Short",
58 "Absolute Data Addressing - Long",
59 "Immediate value",
60 };
61
print_read_write_regs(cs_detail * detail)62 static void print_read_write_regs(cs_detail* detail)
63 {
64 int i;
65
66 for (i = 0; i < detail->regs_read_count; ++i)
67 {
68 uint16_t reg_id = detail->regs_read[i];
69 const char* reg_name = cs_reg_name(handle, reg_id);
70 printf("\treading from reg: %s\n", reg_name);
71 }
72
73 for (i = 0; i < detail->regs_write_count; ++i)
74 {
75 uint16_t reg_id = detail->regs_write[i];
76 const char* reg_name = cs_reg_name(handle, reg_id);
77 printf("\twriting to reg: %s\n", reg_name);
78 }
79 }
80
print_insn_detail(cs_insn * ins)81 static void print_insn_detail(cs_insn *ins)
82 {
83 cs_m68k* m68k;
84 cs_detail* detail;
85 int i;
86
87 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
88 if (ins->detail == NULL)
89 return;
90
91 detail = ins->detail;
92 m68k = &detail->m68k;
93 if (m68k->op_count)
94 printf("\top_count: %u\n", m68k->op_count);
95
96 print_read_write_regs(detail);
97
98 printf("\tgroups_count: %u\n", detail->groups_count);
99
100 for (i = 0; i < m68k->op_count; i++) {
101 cs_m68k_op* op = &(m68k->operands[i]);
102
103 switch((int)op->type) {
104 default:
105 break;
106 case M68K_OP_REG:
107 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
108 break;
109 case M68K_OP_IMM:
110 printf("\t\toperands[%u].type: IMM = 0x%x\n", i, (int)op->imm);
111 break;
112 case M68K_OP_MEM:
113 printf("\t\toperands[%u].type: MEM\n", i);
114 if (op->mem.base_reg != M68K_REG_INVALID)
115 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
116 i, cs_reg_name(handle, op->mem.base_reg));
117 if (op->mem.index_reg != M68K_REG_INVALID) {
118 printf("\t\t\toperands[%u].mem.index: REG = %s\n",
119 i, cs_reg_name(handle, op->mem.index_reg));
120 printf("\t\t\toperands[%u].mem.index: size = %c\n",
121 i, op->mem.index_size ? 'l' : 'w');
122 }
123 if (op->mem.disp != 0)
124 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
125 if (op->mem.scale != 0)
126 printf("\t\t\toperands[%u].mem.scale: %d\n", i, op->mem.scale);
127
128 printf("\t\taddress mode: %s\n", s_addressing_modes[op->address_mode]);
129 break;
130 case M68K_OP_FP_SINGLE:
131 printf("\t\toperands[%u].type: FP_SINGLE\n", i);
132 printf("\t\t\toperands[%u].simm: %f\n", i, op->simm);
133 break;
134 case M68K_OP_FP_DOUBLE:
135 printf("\t\toperands[%u].type: FP_DOUBLE\n", i);
136 printf("\t\t\toperands[%u].dimm: %lf\n", i, op->dimm);
137 break;
138 case M68K_OP_REG_BITS:
139 printf("\t\toperands[%u].type: REG_BITS = $%x\n", i, op->register_bits);
140
141 }
142 }
143
144 printf("\n");
145 }
146
test()147 static void test()
148 {
149 #define M68K_CODE "\xf0\x10\xf0\x00\x48\xaf\xff\xff\x7f\xff\x11\xb0\x01\x37\x7f\xff\xff\xff\x12\x34\x56\x78\x01\x33\x10\x10\x10\x10\x32\x32\x32\x32\x4C\x00\x54\x04\x48\xe7\xe0\x30\x4C\xDF\x0C\x07\xd4\x40\x87\x5a\x4e\x71\x02\xb4\xc0\xde\xc0\xde\x5c\x00\x1d\x80\x71\x12\x01\x23\xf2\x3c\x44\x22\x40\x49\x0e\x56\x54\xc5\xf2\x3c\x44\x00\x44\x7a\x00\x00\xf2\x00\x0a\x28\x4E\xB9\x00\x00\x00\x12\x4E\x75"
150 struct platform platforms[] = {
151 {
152 CS_ARCH_M68K,
153 (cs_mode)(CS_MODE_BIG_ENDIAN | CS_MODE_M68K_040),
154 (unsigned char*)M68K_CODE,
155 sizeof(M68K_CODE) - 1,
156 "M68K",
157 },
158 };
159
160 uint64_t address = 0x01000;
161 cs_insn *insn;
162 int i;
163 size_t count;
164
165 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
166 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
167 if (err) {
168 printf("Failed on cs_open() with error returned: %u\n", err);
169 abort();
170 }
171
172 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
173
174 count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
175 if (count) {
176 size_t j;
177
178 printf("****************\n");
179 printf("Platform: %s\n", platforms[i].comment);
180 print_string_hex("Code: ", platforms[i].code, platforms[i].size);
181 printf("Disasm:\n");
182
183 for (j = 0; j < count; j++) {
184 assert(address == insn[j].address && "this means the size of the previous instruction was incorrect");
185 address += insn[j].size;
186 printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
187 print_insn_detail(&insn[j]);
188 }
189 printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
190
191 // free memory allocated by cs_disasm()
192 cs_free(insn, count);
193 } else {
194 printf("****************\n");
195 printf("Platform: %s\n", platforms[i].comment);
196 print_string_hex("Code:", platforms[i].code, platforms[i].size);
197 printf("ERROR: Failed to disasm given code!\n");
198 abort();
199 }
200
201 printf("\n");
202
203 cs_close(&handle);
204 }
205 }
206
main()207 int main()
208 {
209 test();
210
211 return 0;
212 }
213