Rizin
unix-like reverse engineering framework and cli tools
cstool_m680x.c
Go to the documentation of this file.
1 /* Capstone Disassembly Engine */
2 /* M680X Backend by Wolfgang Schwotzer <wolfgang.schwotzer@gmx.net> 2017 */
3 
4 #include <stdio.h>
5 #include <capstone/capstone.h>
6 
7 void print_string_hex(char *comment, unsigned char *str, size_t len);
8 
9 static const char *s_access[] = {
10  "UNCHANGED", "READ", "WRITE", "READ | WRITE",
11 };
12 
14 {
15  int i;
16 
17  if (detail->regs_read_count > 0) {
18  printf("\treading from regs: ");
19 
20  for (i = 0; i < detail->regs_read_count; ++i) {
21  if (i > 0)
22  printf(", ");
23 
24  printf("%s", cs_reg_name(handle, detail->regs_read[i]));
25  }
26 
27  printf("\n");
28  }
29 
30  if (detail->regs_write_count > 0) {
31  printf("\twriting to regs: ");
32 
33  for (i = 0; i < detail->regs_write_count; ++i) {
34  if (i > 0)
35  printf(", ");
36 
38  detail->regs_write[i]));
39  }
40 
41  printf("\n");
42  }
43 }
44 
45 void print_insn_detail_m680x(csh handle, cs_insn *insn)
46 {
47  cs_detail *detail = insn->detail;
48  cs_m680x *m680x = NULL;
49  int i;
50 
51  // detail can be NULL on "data" instruction if SKIPDATA option is
52  // turned ON
53  if (detail == NULL)
54  return;
55 
56  m680x = &detail->m680x;
57 
58  if (m680x->op_count)
59  printf("\top_count: %u\n", m680x->op_count);
60 
61  for (i = 0; i < m680x->op_count; i++) {
62  cs_m680x_op *op = &(m680x->operands[i]);
63  const char *comment;
64 
65  switch ((int)op->type) {
66  default:
67  break;
68 
69  case M680X_OP_REGISTER:
70  comment = "";
71 
72  if ((i == 0 && m680x->flags & M680X_FIRST_OP_IN_MNEM) ||
73  (i == 1 && m680x->flags &
75  comment = " (in mnemonic)";
76 
77  printf("\t\toperands[%u].type: REGISTER = %s%s\n", i,
78  cs_reg_name(handle, op->reg), comment);
79  break;
80 
81  case M680X_OP_CONSTANT:
82  printf("\t\toperands[%u].type: CONSTANT = %u\n", i,
83  op->const_val);
84  break;
85 
86  case M680X_OP_IMMEDIATE:
87  printf("\t\toperands[%u].type: IMMEDIATE = #%d\n", i,
88  op->imm);
89  break;
90 
91  case M680X_OP_DIRECT:
92  printf("\t\toperands[%u].type: DIRECT = 0x%02x\n", i,
93  op->direct_addr);
94  break;
95 
96  case M680X_OP_EXTENDED:
97  printf("\t\toperands[%u].type: EXTENDED %s = 0x%04x\n",
98  i, op->ext.indirect ? "INDIRECT" : "",
99  op->ext.address);
100  break;
101 
102  case M680X_OP_RELATIVE:
103  printf("\t\toperands[%u].type: RELATIVE = 0x%04x\n", i,
104  op->rel.address);
105  break;
106 
107  case M680X_OP_INDEXED:
108  printf("\t\toperands[%u].type: INDEXED%s\n", i,
109  (op->idx.flags & M680X_IDX_INDIRECT) ?
110  " INDIRECT" : "");
111 
112  if (op->idx.base_reg != M680X_REG_INVALID)
113  printf("\t\t\tbase register: %s\n",
114  cs_reg_name(handle, op->idx.base_reg));
115 
116  if (op->idx.offset_reg != M680X_REG_INVALID)
117  printf("\t\t\toffset register: %s\n",
118  cs_reg_name(handle, op->idx.offset_reg));
119 
120  if ((op->idx.offset_bits != 0) &&
121  (op->idx.offset_reg == M680X_REG_INVALID) &&
122  !op->idx.inc_dec) {
123  printf("\t\t\toffset: %d\n", op->idx.offset);
124 
125  if (op->idx.base_reg == M680X_REG_PC)
126  printf("\t\t\toffset address: 0x%x\n",
127  op->idx.offset_addr);
128 
129  printf("\t\t\toffset bits: %u\n",
130  op->idx.offset_bits);
131  }
132 
133  if (op->idx.inc_dec) {
134  const char *post_pre = op->idx.flags &
135  M680X_IDX_POST_INC_DEC ? "post" : "pre";
136  const char *inc_dec = (op->idx.inc_dec > 0) ?
137  "increment" : "decrement";
138 
139  printf("\t\t\t%s %s: %d\n", post_pre, inc_dec,
140  abs(op->idx.inc_dec));
141  }
142 
143  break;
144  }
145 
146  if (op->size != 0)
147  printf("\t\t\tsize: %u\n", op->size);
148 
149  if (op->access != CS_AC_INVALID)
150  printf("\t\t\taccess: %s\n", s_access[op->access]);
151  }
152 
154 }
155 
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static mcore_handle handle
Definition: asm_mcore.c:8
size_t csh
Definition: capstone.h:71
@ CS_AC_INVALID
Uninitialized/invalid access type.
Definition: capstone.h:203
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static const char * s_access[]
Definition: cstool_m680x.c:9
void print_string_hex(char *comment, unsigned char *str, size_t len)
void print_read_write_regs(csh handle, cs_detail *detail)
Definition: cstool_m680x.c:13
void print_insn_detail_m680x(csh handle, cs_insn *insn)
Definition: cstool_m680x.c:45
#define M680X_IDX_INDIRECT
Definition: m680x.h:76
#define M680X_FIRST_OP_IN_MNEM
Definition: m680x.h:159
@ M680X_REG_INVALID
Definition: m680x.h:21
@ M680X_REG_PC
M6800/1/2/3/9, M6301/9.
Definition: m680x.h:46
#define M680X_IDX_POST_INC_DEC
Definition: m680x.h:78
@ M680X_OP_EXTENDED
= Extended addressing operand.
Definition: m680x.h:60
@ M680X_OP_INDEXED
= Indexed addressing operand.
Definition: m680x.h:59
@ M680X_OP_CONSTANT
Used e.g. for a bit index or page number.
Definition: m680x.h:63
@ M680X_OP_IMMEDIATE
= Immediate operand.
Definition: m680x.h:58
@ M680X_OP_REGISTER
= Register operand.
Definition: m680x.h:57
@ M680X_OP_RELATIVE
= Relative addressing operand.
Definition: m680x.h:62
@ M680X_OP_DIRECT
= Direct addressing operand.
Definition: m680x.h:61
#define M680X_SECOND_OP_IN_MNEM
Definition: m680x.h:162
Instruction operand.
Definition: m680x.h:114
The M680X instruction and it's operands.
Definition: m680x.h:165
uint8_t flags
See: M680X instruction flags.
Definition: m680x.h:166
cs_m680x_op operands[M680X_OPERAND_COUNT]
operands for this insn.
Definition: m680x.h:168
uint8_t op_count
number of operands for the instruction or 0
Definition: m680x.h:167
Definition: dis.c:32