test_m68k.c
Go to the documentation of this file.
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
3 
4 #include <stdio.h>
5 #include <assert.h>
6 
7 #include <capstone/platform.h>
8 #include <capstone/capstone.h>
9 
10 struct platform {
11  cs_arch arch;
12  cs_mode mode;
13  unsigned char* code;
14  size_t size;
15  const char* comment;
16 };
17 
18 static csh handle;
19 
20 static void print_string_hex(const char* comment, unsigned char* str, size_t len)
21 {
22  unsigned char *c;
23 
24  printf("%s", comment);
25  for (c = str; c < str + len; c++) {
26  printf("0x%02x ", *c & 0xff);
27  }
28 
29  printf("\n");
30 }
31 
32 const char* s_addressing_modes[] = {
33  "<invalid mode>",
34 
35  "Register Direct - Data",
36  "Register Direct - Address",
37 
38  "Register Indirect - Address",
39  "Register Indirect - Address with Postincrement",
40  "Register Indirect - Address with Predecrement",
41  "Register Indirect - Address with Displacement",
42 
43  "Address Register Indirect With Index - 8-bit displacement",
44  "Address Register Indirect With Index - Base displacement",
45 
46  "Memory indirect - Postindex",
47  "Memory indirect - Preindex",
48 
49  "Program Counter Indirect - with Displacement",
50 
51  "Program Counter Indirect with Index - with 8-Bit Displacement",
52  "Program Counter Indirect with Index - with Base Displacement",
53 
54  "Program Counter Memory Indirect - Postindexed",
55  "Program Counter Memory Indirect - Preindexed",
56 
57  "Absolute Data Addressing - Short",
58  "Absolute Data Addressing - Long",
59  "Immediate value",
60 };
61 
62 static void print_read_write_regs(cs_detail* detail)
63 {
64  int i;
65 
66  for (i = 0; i < detail->regs_read_count; ++i)
67  {
68  uint16_t reg_id = detail->regs_read[i];
69  const char* reg_name = cs_reg_name(handle, reg_id);
70  printf("\treading from reg: %s\n", reg_name);
71  }
72 
73  for (i = 0; i < detail->regs_write_count; ++i)
74  {
75  uint16_t reg_id = detail->regs_write[i];
76  const char* reg_name = cs_reg_name(handle, reg_id);
77  printf("\twriting to reg: %s\n", reg_name);
78  }
79 }
80 
81 static void print_insn_detail(cs_insn *ins)
82 {
83  cs_m68k* m68k;
84  cs_detail* detail;
85  int i;
86 
87  // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
88  if (ins->detail == NULL)
89  return;
90 
91  detail = ins->detail;
92  m68k = &detail->m68k;
93  if (m68k->op_count)
94  printf("\top_count: %u\n", m68k->op_count);
95 
97 
98  printf("\tgroups_count: %u\n", detail->groups_count);
99 
100  for (i = 0; i < m68k->op_count; i++) {
101  cs_m68k_op* op = &(m68k->operands[i]);
102 
103  switch((int)op->type) {
104  default:
105  break;
106  case M68K_OP_REG:
107  printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
108  break;
109  case M68K_OP_IMM:
110  printf("\t\toperands[%u].type: IMM = 0x%x\n", i, (int)op->imm);
111  break;
112  case M68K_OP_MEM:
113  printf("\t\toperands[%u].type: MEM\n", i);
114  if (op->mem.base_reg != M68K_REG_INVALID)
115  printf("\t\t\toperands[%u].mem.base: REG = %s\n",
116  i, cs_reg_name(handle, op->mem.base_reg));
117  if (op->mem.index_reg != M68K_REG_INVALID) {
118  printf("\t\t\toperands[%u].mem.index: REG = %s\n",
119  i, cs_reg_name(handle, op->mem.index_reg));
120  printf("\t\t\toperands[%u].mem.index: size = %c\n",
121  i, op->mem.index_size ? 'l' : 'w');
122  }
123  if (op->mem.disp != 0)
124  printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
125  if (op->mem.scale != 0)
126  printf("\t\t\toperands[%u].mem.scale: %d\n", i, op->mem.scale);
127 
128  printf("\t\taddress mode: %s\n", s_addressing_modes[op->address_mode]);
129  break;
130  case M68K_OP_FP_SINGLE:
131  printf("\t\toperands[%u].type: FP_SINGLE\n", i);
132  printf("\t\t\toperands[%u].simm: %f\n", i, op->simm);
133  break;
134  case M68K_OP_FP_DOUBLE:
135  printf("\t\toperands[%u].type: FP_DOUBLE\n", i);
136  printf("\t\t\toperands[%u].dimm: %lf\n", i, op->dimm);
137  break;
138  case M68K_OP_REG_BITS:
139  printf("\t\toperands[%u].type: REG_BITS = $%x\n", i, op->register_bits);
140 
141  }
142  }
143 
144  printf("\n");
145 }
146 
147 static void test()
148 {
149 #define M68K_CODE "\xf0\x10\xf0\x00\x48\xaf\xff\xff\x7f\xff\x11\xb0\x01\x37\x7f\xff\xff\xff\x12\x34\x56\x78\x01\x33\x10\x10\x10\x10\x32\x32\x32\x32\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"
150  struct platform platforms[] = {
151  {
152  CS_ARCH_M68K,
154  (unsigned char*)M68K_CODE,
155  sizeof(M68K_CODE) - 1,
156  "M68K",
157  },
158  };
159 
160  uint64_t address = 0x01000;
161  cs_insn *insn;
162  int i;
163  size_t count;
164 
165  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
166  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
167  if (err) {
168  printf("Failed on cs_open() with error returned: %u\n", err);
169  abort();
170  }
171 
173 
174  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
175  if (count) {
176  size_t j;
177 
178  printf("****************\n");
179  printf("Platform: %s\n", platforms[i].comment);
181  printf("Disasm:\n");
182 
183  for (j = 0; j < count; j++) {
184  assert(address == insn[j].address && "this means the size of the previous instruction was incorrect");
185  address += insn[j].size;
186  printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
187  print_insn_detail(&insn[j]);
188  }
189  printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
190 
191  // free memory allocated by cs_disasm()
192  cs_free(insn, count);
193  } else {
194  printf("****************\n");
195  printf("Platform: %s\n", platforms[i].comment);
197  printf("ERROR: Failed to disasm given code!\n");
198  abort();
199  }
200 
201  printf("\n");
202 
203  cs_close(&handle);
204  }
205 }
206 
207 int main()
208 {
209  test();
210 
211  return 0;
212 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
M68K_OP_REG_BITS
@ M68K_OP_REG_BITS
Register bits move.
Definition: m68k.h:119
cs_close
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:522
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
CS_ARCH_M68K
@ CS_ARCH_M68K
68K architecture
Definition: capstone.h:83
platform::code
unsigned char * code
Definition: test_arm_regression.c:21
cs_disasm
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:822
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
print_read_write_regs
static void print_read_write_regs(cs_detail *detail)
Definition: test_m68k.c:62
error_ref_leak.err
err
Definition: error_ref_leak.py:35
cs_open
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:474
platform::mode
cs_mode mode
Definition: test_arm_regression.c:20
cs_arch
cs_arch
Architecture type.
Definition: capstone.h:74
M68K_OP_FP_DOUBLE
@ M68K_OP_FP_DOUBLE
double precision Floating-Point operand
Definition: m68k.h:118
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
M68K_OP_FP_SINGLE
@ M68K_OP_FP_SINGLE
single precision Floating-Point operand
Definition: m68k.h:117
CS_OPT_DETAIL
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:172
detail
Definition: test_winkernel.cpp:39
print_insn_detail
static void print_insn_detail(cs_insn *ins)
Definition: test_m68k.c:81
cs_option
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:670
cs_mode
cs_mode
Mode type.
Definition: capstone.h:103
capstone.h
handle
static csh handle
Definition: test_m68k.c:18
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
cs_m68k
The M68K instruction and it's operands.
Definition: m68k.h:207
cs_reg_name
const CAPSTONE_EXPORT char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1176
platform.h
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
CS_OPT_ON
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:184
CS_MODE_M68K_040
@ CS_MODE_M68K_040
M68K 68040 mode.
Definition: capstone.h:122
platform::comment
char * comment
Definition: test_arm_regression.c:23
m68k
Definition: test_winkernel.cpp:67
CS_MODE_BIG_ENDIAN
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:124
main
int main()
Definition: test_m68k.c:207
arch
cs_arch arch
Definition: cstool.c:13
platform::arch
cs_arch arch
Definition: test_arm_regression.c:19
M68K_OP_IMM
@ M68K_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: m68k.h:115
s_addressing_modes
const char * s_addressing_modes[]
Definition: test_m68k.c:32
csh
size_t csh
Definition: capstone.h:71
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
M68K_CODE
#define M68K_CODE
M68K_REG_INVALID
@ M68K_REG_INVALID
Definition: m68k.h:21
M68K_OP_REG
@ M68K_OP_REG
= CS_OP_REG (Register operand).
Definition: m68k.h:114
print_string_hex
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_m68k.c:20
cs_free
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1039
code
Definition: bloaty/third_party/zlib/contrib/infback9/inftree9.h:24
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
platforms
struct platform platforms[]
Definition: fuzz_diff.c:18
cs_m68k_op
Instruction operand.
Definition: m68k.h:154
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
test
static void test()
Definition: test_m68k.c:147
M68K_OP_MEM
@ M68K_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: m68k.h:116
op
static grpc_op * op
Definition: test/core/fling/client.cc:47
platform
Definition: test_arm_regression.c:18
platform::size
size_t size
Definition: test_arm_regression.c:22
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
test_evm.detail
detail
Definition: test_evm.py:9


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:31