1 //
2 // cstool_m68k.c
3 //
4 //
5 // Created by YUHANG TANG on 26/10/16.
6 //
7 //
8
9 #include <stdio.h>
10 #include <capstone/capstone.h>
11
12 void print_string_hex(char *comment, unsigned char *str, size_t len);
13
14 static const char* s_addressing_modes[] = {
15 "<invalid mode>",
16
17 "Register Direct - Data",
18 "Register Direct - Address",
19
20 "Register Indirect - Address",
21 "Register Indirect - Address with Postincrement",
22 "Register Indirect - Address with Predecrement",
23 "Register Indirect - Address with Displacement",
24
25 "Address Register Indirect With Index - 8-bit displacement",
26 "Address Register Indirect With Index - Base displacement",
27
28 "Memory indirect - Postindex",
29 "Memory indirect - Preindex",
30
31 "Program Counter Indirect - with Displacement",
32
33 "Program Counter Indirect with Index - with 8-Bit Displacement",
34 "Program Counter Indirect with Index - with Base Displacement",
35
36 "Program Counter Memory Indirect - Postindexed",
37 "Program Counter Memory Indirect - Preindexed",
38
39 "Absolute Data Addressing - Short",
40 "Absolute Data Addressing - Long",
41 "Immediate value",
42 };
43
print_read_write_regs(cs_detail * detail,csh handle)44 static void print_read_write_regs(cs_detail* detail, csh handle)
45 {
46 int i;
47
48 for (i = 0; i < detail->regs_read_count; ++i) {
49 uint16_t reg_id = detail->regs_read[i];
50 const char* reg_name = cs_reg_name(handle, reg_id);
51 printf("\treading from reg: %s\n", reg_name);
52 }
53
54 for (i = 0; i < detail->regs_write_count; ++i) {
55 uint16_t reg_id = detail->regs_write[i];
56 const char* reg_name = cs_reg_name(handle, reg_id);
57 printf("\twriting to reg: %s\n", reg_name);
58 }
59 }
60
print_insn_detail_m68k(csh handle,cs_insn * ins)61 void print_insn_detail_m68k(csh handle, cs_insn *ins)
62 {
63 cs_m68k* m68k;
64 cs_detail* detail;
65 int i;
66
67 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
68 if (ins->detail == NULL)
69 return;
70
71 detail = ins->detail;
72 m68k = &detail->m68k;
73 if (m68k->op_count)
74 printf("\top_count: %u\n", m68k->op_count);
75
76 print_read_write_regs(detail, handle);
77
78 printf("\tgroups_count: %u\n", detail->groups_count);
79
80 for (i = 0; i < m68k->op_count; i++) {
81 cs_m68k_op* op = &(m68k->operands[i]);
82
83 switch((int)op->type) {
84 default:
85 break;
86 case M68K_OP_REG:
87 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
88 break;
89 case M68K_OP_IMM:
90 printf("\t\toperands[%u].type: IMM = 0x%x\n", i, (int)op->imm);
91 break;
92 case M68K_OP_MEM:
93 printf("\t\toperands[%u].type: MEM\n", i);
94 if (op->mem.base_reg != M68K_REG_INVALID)
95 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
96 i, cs_reg_name(handle, op->mem.base_reg));
97 if (op->mem.index_reg != M68K_REG_INVALID) {
98 printf("\t\t\toperands[%u].mem.index: REG = %s\n",
99 i, cs_reg_name(handle, op->mem.index_reg));
100 printf("\t\t\toperands[%u].mem.index: size = %c\n",
101 i, op->mem.index_size ? 'l' : 'w');
102 }
103 if (op->mem.disp != 0)
104 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
105 if (op->mem.scale != 0)
106 printf("\t\t\toperands[%u].mem.scale: %d\n", i, op->mem.scale);
107
108 printf("\t\taddress mode: %s\n", s_addressing_modes[op->address_mode]);
109 break;
110 case M68K_OP_FP_SINGLE:
111 printf("\t\toperands[%u].type: FP_SINGLE\n", i);
112 printf("\t\t\toperands[%u].simm: %f\n", i, op->simm);
113 break;
114 case M68K_OP_FP_DOUBLE:
115 printf("\t\toperands[%u].type: FP_DOUBLE\n", i);
116 printf("\t\t\toperands[%u].dimm: %lf\n", i, op->dimm);
117 break;
118 }
119 }
120 }
121
122