YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 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 | |
YUHANG TANG | 9bc14c1 | 2016-10-28 15:32:50 +0800 | [diff] [blame] | 14 | static const char* s_addressing_modes[] = { |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 15 | "<invalid mode>", |
YUHANG TANG | 9bc14c1 | 2016-10-28 15:32:50 +0800 | [diff] [blame] | 16 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 17 | "Register Direct - Data", |
| 18 | "Register Direct - Address", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 19 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 20 | "Register Indirect - Address", |
| 21 | "Register Indirect - Address with Postincrement", |
| 22 | "Register Indirect - Address with Predecrement", |
| 23 | "Register Indirect - Address with Displacement", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 24 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 25 | "Address Register Indirect With Index - 8-bit displacement", |
| 26 | "Address Register Indirect With Index - Base displacement", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 27 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 28 | "Memory indirect - Postindex", |
| 29 | "Memory indirect - Preindex", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 30 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 31 | "Program Counter Indirect - with Displacement", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 32 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 33 | "Program Counter Indirect with Index - with 8-Bit Displacement", |
| 34 | "Program Counter Indirect with Index - with Base Displacement", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 35 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 36 | "Program Counter Memory Indirect - Postindexed", |
| 37 | "Program Counter Memory Indirect - Preindexed", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 38 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 39 | "Absolute Data Addressing - Short", |
| 40 | "Absolute Data Addressing - Long", |
| 41 | "Immediate value", |
| 42 | }; |
| 43 | |
YUHANG TANG | 9bc14c1 | 2016-10-28 15:32:50 +0800 | [diff] [blame] | 44 | static void print_read_write_regs(cs_detail* detail, csh handle) |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 45 | { |
| 46 | int i; |
YUHANG TANG | 9bc14c1 | 2016-10-28 15:32:50 +0800 | [diff] [blame] | 47 | |
| 48 | for (i = 0; i < detail->regs_read_count; ++i) { |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 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 | } |
YUHANG TANG | 9bc14c1 | 2016-10-28 15:32:50 +0800 | [diff] [blame] | 53 | |
| 54 | for (i = 0; i < detail->regs_write_count; ++i) { |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 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 | |
| 61 | void print_insn_detail_m68k(csh handle, cs_insn *ins) |
| 62 | { |
| 63 | cs_m68k* m68k; |
| 64 | cs_detail* detail; |
| 65 | int i; |
YUHANG TANG | 9bc14c1 | 2016-10-28 15:32:50 +0800 | [diff] [blame] | 66 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 67 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 68 | if (ins->detail == NULL) |
| 69 | return; |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 70 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 71 | detail = ins->detail; |
| 72 | m68k = &detail->m68k; |
| 73 | if (m68k->op_count) |
| 74 | printf("\top_count: %u\n", m68k->op_count); |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 75 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 76 | print_read_write_regs(detail, handle); |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 77 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 78 | printf("\tgroups_count: %u\n", detail->groups_count); |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 79 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 80 | for (i = 0; i < m68k->op_count; i++) { |
| 81 | cs_m68k_op* op = &(m68k->operands[i]); |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 82 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 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", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 96 | i, cs_reg_name(handle, op->mem.base_reg)); |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 97 | if (op->mem.index_reg != M68K_REG_INVALID) { |
| 98 | printf("\t\t\toperands[%u].mem.index: REG = %s\n", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 99 | i, cs_reg_name(handle, op->mem.index_reg)); |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 100 | printf("\t\t\toperands[%u].mem.index: size = %c\n", |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 101 | i, op->mem.index_size ? 'l' : 'w'); |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 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); |
Nguyen Anh Quynh | df6f9cc | 2016-10-28 16:12:05 +0800 | [diff] [blame] | 107 | |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 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 | } |
YUHANG TANG | be3f867 | 2016-10-27 12:12:59 +0800 | [diff] [blame] | 120 | } |
| 121 | |