Rizin
unix-like reverse engineering framework and cli tools
test_m68k.c
Go to the documentation of this file.
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3 
4 #include <stdio.h>
5 
6 #include <capstone/platform.h>
7 #include <capstone/capstone.h>
8 
9 struct platform {
10  cs_arch arch;
11  cs_mode mode;
12  unsigned char* code;
13  size_t size;
14  const char* comment;
15 };
16 
17 static csh handle;
18 
19 static void print_string_hex(const char* comment, unsigned char* str, size_t len)
20 {
21  unsigned char *c;
22 
23  printf("%s", comment);
24  for (c = str; c < str + len; c++) {
25  printf("0x%02x ", *c & 0xff);
26  }
27 
28  printf("\n");
29 }
30 
31 const char* s_addressing_modes[] = {
32  "<invalid mode>",
33 
34  "Register Direct - Data",
35  "Register Direct - Address",
36 
37  "Register Indirect - Address",
38  "Register Indirect - Address with Postincrement",
39  "Register Indirect - Address with Predecrement",
40  "Register Indirect - Address with Displacement",
41 
42  "Address Register Indirect With Index - 8-bit displacement",
43  "Address Register Indirect With Index - Base displacement",
44 
45  "Memory indirect - Postindex",
46  "Memory indirect - Preindex",
47 
48  "Program Counter Indirect - with Displacement",
49 
50  "Program Counter Indirect with Index - with 8-Bit Displacement",
51  "Program Counter Indirect with Index - with Base Displacement",
52 
53  "Program Counter Memory Indirect - Postindexed",
54  "Program Counter Memory Indirect - Preindexed",
55 
56  "Absolute Data Addressing - Short",
57  "Absolute Data Addressing - Long",
58  "Immediate value",
59 };
60 
61 static void print_read_write_regs(cs_detail* detail)
62 {
63  int i;
64 
65  for (i = 0; i < detail->regs_read_count; ++i)
66  {
67  uint16_t reg_id = detail->regs_read[i];
68  const char* reg_name = cs_reg_name(handle, reg_id);
69  printf("\treading from reg: %s\n", reg_name);
70  }
71 
72  for (i = 0; i < detail->regs_write_count; ++i)
73  {
74  uint16_t reg_id = detail->regs_write[i];
75  const char* reg_name = cs_reg_name(handle, reg_id);
76  printf("\twriting to reg: %s\n", reg_name);
77  }
78 }
79 
80 static void print_insn_detail(cs_insn *ins)
81 {
82  cs_m68k* m68k;
83  cs_detail* detail;
84  int i;
85 
86  // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
87  if (ins->detail == NULL)
88  return;
89 
90  detail = ins->detail;
91  m68k = &detail->m68k;
92  if (m68k->op_count)
93  printf("\top_count: %u\n", m68k->op_count);
94 
96 
97  printf("\tgroups_count: %u\n", detail->groups_count);
98 
99  for (i = 0; i < m68k->op_count; i++) {
100  cs_m68k_op* op = &(m68k->operands[i]);
101 
102  switch((int)op->type) {
103  default:
104  break;
105  case M68K_OP_REG:
106  printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
107  break;
108  case M68K_OP_IMM:
109  printf("\t\toperands[%u].type: IMM = 0x%x\n", i, (int)op->imm);
110  break;
111  case M68K_OP_MEM:
112  printf("\t\toperands[%u].type: MEM\n", i);
113  if (op->mem.base_reg != M68K_REG_INVALID)
114  printf("\t\t\toperands[%u].mem.base: REG = %s\n",
115  i, cs_reg_name(handle, op->mem.base_reg));
116  if (op->mem.index_reg != M68K_REG_INVALID) {
117  printf("\t\t\toperands[%u].mem.index: REG = %s\n",
118  i, cs_reg_name(handle, op->mem.index_reg));
119  printf("\t\t\toperands[%u].mem.index: size = %c\n",
120  i, op->mem.index_size ? 'l' : 'w');
121  }
122  if (op->mem.disp != 0)
123  printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
124  if (op->mem.scale != 0)
125  printf("\t\t\toperands[%u].mem.scale: %d\n", i, op->mem.scale);
126 
127  printf("\t\taddress mode: %s\n", s_addressing_modes[op->address_mode]);
128  break;
129  case M68K_OP_FP_SINGLE:
130  printf("\t\toperands[%u].type: FP_SINGLE\n", i);
131  printf("\t\t\toperands[%u].simm: %f\n", i, op->simm);
132  break;
133  case M68K_OP_FP_DOUBLE:
134  printf("\t\toperands[%u].type: FP_DOUBLE\n", i);
135  printf("\t\t\toperands[%u].dimm: %lf\n", i, op->dimm);
136  break;
137  case M68K_OP_REG_BITS:
138  printf("\t\toperands[%u].type: REG_BITS = $%x\n", i, op->register_bits);
139  break;
140  case M68K_OP_REG_PAIR:
141  printf("\t\toperands[%u].type: REG_PAIR = (%s, %s)\n", i,
142  cs_reg_name(handle, op->reg_pair.reg_0),
143  cs_reg_name(handle, op->reg_pair.reg_1));
144  break;
145  }
146  }
147 
148  printf("\n");
149 }
150 
151 static void test()
152 {
153 #define M68K_CODE "\x4C\x00\x54\x04\x48\xe7\xe0\x30\x4C\xDF\x0C\x07\xd4\x40\x87\x5a\x4e\x71\x02\xb4\xc0\xde\xc0\xde\x5c\x00\x1d\x80\x71\x12\x01\x23\xf2\x3c\x44\x22\x40\x49\x0e\x56\x54\xc5\xf2\x3c\x44\x00\x44\x7a\x00\x00\xf2\x00\x0a\x28\x4E\xB9\x00\x00\x00\x12\x4E\x75"
154  struct platform platforms[] = {
155  {
156  CS_ARCH_M68K,
158  (unsigned char*)M68K_CODE,
159  sizeof(M68K_CODE) - 1,
160  "M68K",
161  },
162  };
163 
164  uint64_t address = 0x01000;
165  cs_insn *insn;
166  int i;
167  size_t count;
168 
169  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
170  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
171  if (err) {
172  printf("Failed on cs_open() with error returned: %u\n", err);
173  abort();
174  }
175 
177 
178  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
179  if (count) {
180  size_t j;
181 
182  printf("****************\n");
183  printf("Platform: %s\n", platforms[i].comment);
185  printf("Disasm:\n");
186 
187  for (j = 0; j < count; j++) {
188  printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
189  print_insn_detail(&insn[j]);
190  }
191  printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
192 
193  // free memory allocated by cs_disasm()
194  cs_free(insn, count);
195  } else {
196  printf("****************\n");
197  printf("Platform: %s\n", platforms[i].comment);
199  printf("ERROR: Failed to disasm given code!\n");
200  abort();
201  }
202 
203  printf("\n");
204 
205  cs_close(&handle);
206  }
207 }
208 
209 int main()
210 {
211  test();
212 
213  return 0;
214 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
cs_arch
Architecture type.
Definition: capstone.h:74
@ CS_ARCH_M68K
68K architecture
Definition: capstone.h:83
cs_mode
Mode type.
Definition: capstone.h:102
@ CS_MODE_M68K_040
M68K 68040 mode.
Definition: capstone.h:121
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:798
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:646
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
cs_arch arch
Definition: cstool.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
struct platform platforms[]
Definition: fuzz_diff.c:18
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
@ M68K_OP_REG_PAIR
Register pair in the same op (upper 4 bits for first reg, lower for second)
Definition: m68k.h:120
@ M68K_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: m68k.h:115
@ M68K_OP_FP_SINGLE
single precision Floating-Point operand
Definition: m68k.h:117
@ M68K_OP_FP_DOUBLE
double precision Floating-Point operand
Definition: m68k.h:118
@ M68K_OP_REG_BITS
Register bits move.
Definition: m68k.h:119
@ M68K_OP_REG
= CS_OP_REG (Register operand).
Definition: m68k.h:114
@ M68K_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: m68k.h:116
@ M68K_REG_INVALID
Definition: m68k.h:21
unsigned short uint16_t
Definition: sftypes.h:30
unsigned long uint64_t
Definition: sftypes.h:28
#define c(i)
Definition: sha256.c:43
Definition: inftree9.h:24
Instruction operand.
Definition: m68k.h:160
The M68K instruction and it's operands.
Definition: m68k.h:210
unsigned char * code
#define PRIx64
Definition: sysdefs.h:94
static csh handle
Definition: test_m68k.c:17
static void print_read_write_regs(cs_detail *detail)
Definition: test_m68k.c:61
const char * s_addressing_modes[]
Definition: test_m68k.c:31
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_m68k.c:19
#define M68K_CODE
static void print_insn_detail(cs_insn *ins)
Definition: test_m68k.c:80
static void test()
Definition: test_m68k.c:151
int main()
Definition: test_m68k.c:209
Definition: dis.c:32
mnemonic
Definition: z80asm.h:48