Debian Codegraph @

cstool/cstool_m68k.c

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