blob: 9a0fc7f30bc3250b5e1819ea6b4e4a99743cba4d [file] [log] [blame]
YUHANG TANGbe3f8672016-10-27 12:12:59 +08001//
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
12void print_string_hex(char *comment, unsigned char *str, size_t len);
13
YUHANG TANG9bc14c12016-10-28 15:32:50 +080014static const char* s_addressing_modes[] = {
YUHANG TANGbe3f8672016-10-27 12:12:59 +080015 "<invalid mode>",
YUHANG TANG9bc14c12016-10-28 15:32:50 +080016
YUHANG TANGbe3f8672016-10-27 12:12:59 +080017 "Register Direct - Data",
18 "Register Direct - Address",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080019
YUHANG TANGbe3f8672016-10-27 12:12:59 +080020 "Register Indirect - Address",
21 "Register Indirect - Address with Postincrement",
22 "Register Indirect - Address with Predecrement",
23 "Register Indirect - Address with Displacement",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080024
YUHANG TANGbe3f8672016-10-27 12:12:59 +080025 "Address Register Indirect With Index - 8-bit displacement",
26 "Address Register Indirect With Index - Base displacement",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080027
YUHANG TANGbe3f8672016-10-27 12:12:59 +080028 "Memory indirect - Postindex",
29 "Memory indirect - Preindex",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080030
YUHANG TANGbe3f8672016-10-27 12:12:59 +080031 "Program Counter Indirect - with Displacement",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080032
YUHANG TANGbe3f8672016-10-27 12:12:59 +080033 "Program Counter Indirect with Index - with 8-Bit Displacement",
34 "Program Counter Indirect with Index - with Base Displacement",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080035
YUHANG TANGbe3f8672016-10-27 12:12:59 +080036 "Program Counter Memory Indirect - Postindexed",
37 "Program Counter Memory Indirect - Preindexed",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080038
YUHANG TANGbe3f8672016-10-27 12:12:59 +080039 "Absolute Data Addressing - Short",
40 "Absolute Data Addressing - Long",
41 "Immediate value",
42};
43
YUHANG TANG9bc14c12016-10-28 15:32:50 +080044static void print_read_write_regs(cs_detail* detail, csh handle)
YUHANG TANGbe3f8672016-10-27 12:12:59 +080045{
46 int i;
YUHANG TANG9bc14c12016-10-28 15:32:50 +080047
48 for (i = 0; i < detail->regs_read_count; ++i) {
YUHANG TANGbe3f8672016-10-27 12:12:59 +080049 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 }
YUHANG TANG9bc14c12016-10-28 15:32:50 +080053
54 for (i = 0; i < detail->regs_write_count; ++i) {
YUHANG TANGbe3f8672016-10-27 12:12:59 +080055 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
61void print_insn_detail_m68k(csh handle, cs_insn *ins)
62{
63 cs_m68k* m68k;
64 cs_detail* detail;
65 int i;
YUHANG TANG9bc14c12016-10-28 15:32:50 +080066
YUHANG TANGbe3f8672016-10-27 12:12:59 +080067 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
68 if (ins->detail == NULL)
69 return;
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080070
YUHANG TANGbe3f8672016-10-27 12:12:59 +080071 detail = ins->detail;
72 m68k = &detail->m68k;
73 if (m68k->op_count)
74 printf("\top_count: %u\n", m68k->op_count);
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080075
YUHANG TANGbe3f8672016-10-27 12:12:59 +080076 print_read_write_regs(detail, handle);
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080077
YUHANG TANGbe3f8672016-10-27 12:12:59 +080078 printf("\tgroups_count: %u\n", detail->groups_count);
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080079
YUHANG TANGbe3f8672016-10-27 12:12:59 +080080 for (i = 0; i < m68k->op_count; i++) {
81 cs_m68k_op* op = &(m68k->operands[i]);
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080082
YUHANG TANGbe3f8672016-10-27 12:12:59 +080083 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",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080096 i, cs_reg_name(handle, op->mem.base_reg));
YUHANG TANGbe3f8672016-10-27 12:12:59 +080097 if (op->mem.index_reg != M68K_REG_INVALID) {
98 printf("\t\t\toperands[%u].mem.index: REG = %s\n",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +080099 i, cs_reg_name(handle, op->mem.index_reg));
YUHANG TANGbe3f8672016-10-27 12:12:59 +0800100 printf("\t\t\toperands[%u].mem.index: size = %c\n",
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +0800101 i, op->mem.index_size ? 'l' : 'w');
YUHANG TANGbe3f8672016-10-27 12:12:59 +0800102 }
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);
Nguyen Anh Quynhdf6f9cc2016-10-28 16:12:05 +0800107
YUHANG TANGbe3f8672016-10-27 12:12:59 +0800108 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 }
YUHANG TANGbe3f8672016-10-27 12:12:59 +0800120}
121