| 1 | /* Capstone Disassembly Engine */ |
| 2 | /* M680X Backend by Wolfgang Schwotzer <[email protected]> 2017 */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <capstone/capstone.h> |
| 6 | #include "cstool.h" |
| 7 | |
| 8 | static const char *s_access[] = { |
| 9 | "UNCHANGED", "READ", "WRITE", "READ | WRITE", |
| 10 | }; |
| 11 | |
| 12 | static void print_read_write_regs(csh handle, cs_detail *detail) |
| 13 | { |
| 14 | int i; |
| 15 | |
| 16 | if (detail->regs_read_count > 0) { |
| 17 | printf("\treading from regs: "); |
| 18 | |
| 19 | for (i = 0; i < detail->regs_read_count; ++i) { |
| 20 | if (i > 0) |
| 21 | printf(", "); |
| 22 | |
| 23 | printf("%s", cs_reg_name(handle, detail->regs_read[i])); |
| 24 | } |
| 25 | |
| 26 | printf("\n"); |
| 27 | } |
| 28 | |
| 29 | if (detail->regs_write_count > 0) { |
| 30 | printf("\twriting to regs: "); |
| 31 | |
| 32 | for (i = 0; i < detail->regs_write_count; ++i) { |
| 33 | if (i > 0) |
| 34 | printf(", "); |
| 35 | |
| 36 | printf("%s", cs_reg_name(handle, |
| 37 | detail->regs_write[i])); |
| 38 | } |
| 39 | |
| 40 | printf("\n"); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void print_insn_detail_m680x(csh handle, cs_insn *insn) |
| 45 | { |
| 46 | cs_detail *detail = insn->detail; |
| 47 | cs_m680x *m680x = NULL; |
| 48 | int i; |
| 49 | |
| 50 | // detail can be NULL on "data" instruction if SKIPDATA option is |
| 51 | // turned ON |
| 52 | if (detail == NULL) |
| 53 | return; |
| 54 | |
| 55 | m680x = &detail->m680x; |
| 56 | |
| 57 | if (m680x->op_count) |
| 58 | printf("\top_count: %u\n", m680x->op_count); |
| 59 | |
| 60 | for (i = 0; i < m680x->op_count; i++) { |
| 61 | cs_m680x_op *op = &(m680x->operands[i]); |
| 62 | const char *comment; |
| 63 | |
| 64 | switch ((int)op->type) { |
| 65 | default: |
| 66 | break; |
| 67 | |
| 68 | case M680X_OP_REGISTER: |
| 69 | comment = ""; |
| 70 | |
| 71 | if ((i == 0 && m680x->flags & M680X_FIRST_OP_IN_MNEM) || |
| 72 | (i == 1 && m680x->flags & |
| 73 | M680X_SECOND_OP_IN_MNEM)) |
| 74 | comment = " (in mnemonic)"; |
| 75 | |
| 76 | printf("\t\toperands[%u].type: REGISTER = %s%s\n", i, |
| 77 | cs_reg_name(handle, op->reg), comment); |
| 78 | break; |
| 79 | |
| 80 | case M680X_OP_CONSTANT: |
| 81 | printf("\t\toperands[%u].type: CONSTANT = %u\n", i, |
| 82 | op->const_val); |
| 83 | break; |
| 84 | |
| 85 | case M680X_OP_IMMEDIATE: |
| 86 | printf("\t\toperands[%u].type: IMMEDIATE = #%d\n", i, |
| 87 | op->imm); |
| 88 | break; |
| 89 | |
| 90 | case M680X_OP_DIRECT: |
| 91 | printf("\t\toperands[%u].type: DIRECT = 0x%02x\n", i, |
| 92 | op->direct_addr); |
| 93 | break; |
| 94 | |
| 95 | case M680X_OP_EXTENDED: |
| 96 | printf("\t\toperands[%u].type: EXTENDED %s = 0x%04x\n", |
| 97 | i, op->ext.indirect ? "INDIRECT" : "", |
| 98 | op->ext.address); |
| 99 | break; |
| 100 | |
| 101 | case M680X_OP_RELATIVE: |
| 102 | printf("\t\toperands[%u].type: RELATIVE = 0x%04x\n", i, |
| 103 | op->rel.address); |
| 104 | break; |
| 105 | |
| 106 | case M680X_OP_INDEXED: |
| 107 | printf("\t\toperands[%u].type: INDEXED%s\n", i, |
| 108 | (op->idx.flags & M680X_IDX_INDIRECT) ? |
| 109 | " INDIRECT" : ""); |
| 110 | |
| 111 | if (op->idx.base_reg != M680X_REG_INVALID) |
| 112 | printf("\t\t\tbase register: %s\n", |
| 113 | cs_reg_name(handle, op->idx.base_reg)); |
| 114 | |
| 115 | if (op->idx.offset_reg != M680X_REG_INVALID) |
| 116 | printf("\t\t\toffset register: %s\n", |
| 117 | cs_reg_name(handle, op->idx.offset_reg)); |
| 118 | |
| 119 | if ((op->idx.offset_bits != 0) && |
| 120 | (op->idx.offset_reg == M680X_REG_INVALID) && |
| 121 | !op->idx.inc_dec) { |
| 122 | printf("\t\t\toffset: %d\n", op->idx.offset); |
| 123 | |
| 124 | if (op->idx.base_reg == M680X_REG_PC) |
| 125 | printf("\t\t\toffset address: 0x%x\n", |
| 126 | op->idx.offset_addr); |
| 127 | |
| 128 | printf("\t\t\toffset bits: %u\n", |
| 129 | op->idx.offset_bits); |
| 130 | } |
| 131 | |
| 132 | if (op->idx.inc_dec) { |
| 133 | const char *post_pre = op->idx.flags & |
| 134 | M680X_IDX_POST_INC_DEC ? "post" : "pre"; |
| 135 | const char *inc_dec = (op->idx.inc_dec > 0) ? |
| 136 | "increment" : "decrement"; |
| 137 | |
| 138 | printf("\t\t\t%s %s: %d\n", post_pre, inc_dec, |
| 139 | abs(op->idx.inc_dec)); |
| 140 | } |
| 141 | |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | if (op->size != 0) |
| 146 | printf("\t\t\tsize: %u\n", op->size); |
| 147 | |
| 148 | if (op->access != CS_AC_INVALID) |
| 149 | printf("\t\t\taccess: %s\n", s_access[op->access]); |
| 150 | } |
| 151 | |
| 152 | print_read_write_regs(handle, detail); |
| 153 | } |
| 154 |