Rizin
unix-like reverse engineering framework and cli tools
test_m68k.c File Reference
#include <stdio.h>
#include <capstone/platform.h>
#include <capstone/capstone.h>

Go to the source code of this file.

Classes

struct  platform
 

Macros

#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"
 

Functions

static void print_string_hex (const char *comment, unsigned char *str, size_t len)
 
static void print_read_write_regs (cs_detail *detail)
 
static void print_insn_detail (cs_insn *ins)
 
static void test ()
 
int main ()
 

Variables

static csh handle
 
const char * s_addressing_modes []
 

Macro Definition Documentation

◆ M68K_CODE

#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"

Function Documentation

◆ main()

int main ( void  )

Definition at line 209 of file test_m68k.c.

210 {
211  test();
212 
213  return 0;
214 }
static void test()
Definition: test_m68k.c:151

References test().

◆ print_insn_detail()

static void print_insn_detail ( cs_insn *  ins)
static

Definition at line 80 of file test_m68k.c.

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 }
lzma_index ** i
Definition: index.h:629
#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
@ 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
Instruction operand.
Definition: m68k.h:160
The M68K instruction and it's operands.
Definition: m68k.h:210
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
Definition: dis.c:32

References cs_reg_name(), test_evm::detail, handle, i, M68K_OP_FP_DOUBLE, M68K_OP_FP_SINGLE, M68K_OP_IMM, M68K_OP_MEM, M68K_OP_REG, M68K_OP_REG_BITS, M68K_OP_REG_PAIR, M68K_REG_INVALID, NULL, print_read_write_regs(), printf(), and s_addressing_modes.

Referenced by test().

◆ print_read_write_regs()

static void print_read_write_regs ( cs_detail *  detail)
static

Definition at line 61 of file test_m68k.c.

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 }
unsigned short uint16_t
Definition: sftypes.h:30

References cs_reg_name(), handle, i, and printf().

Referenced by print_insn_detail().

◆ print_string_hex()

static void print_string_hex ( const char *  comment,
unsigned char *  str,
size_t  len 
)
static

Definition at line 19 of file test_m68k.c.

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 }
size_t len
Definition: 6502dis.c:15
#define c(i)
Definition: sha256.c:43

References c, len, printf(), and cmd_descs_generate::str.

Referenced by test().

◆ test()

static void test ( )
static

Definition at line 151 of file test_m68k.c.

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 }
static bool err
Definition: armass.c:435
@ 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
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
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 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
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
unsigned long uint64_t
Definition: sftypes.h:28
Definition: inftree9.h:24
#define PRIx64
Definition: sysdefs.h:94
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
mnemonic
Definition: z80asm.h:48

References arch, platform::comment, count, CS_ARCH_M68K, cs_close(), cs_disasm(), cs_free(), CS_MODE_BIG_ENDIAN, CS_MODE_M68K_040, cs_open(), CS_OPT_DETAIL, CS_OPT_ON, cs_option(), err, handle, i, M68K_CODE, platforms, print_insn_detail(), print_string_hex(), printf(), and PRIx64.

Referenced by main().

Variable Documentation

◆ handle

csh handle
static

Definition at line 17 of file test_m68k.c.

Referenced by print_insn_detail(), print_read_write_regs(), and test().

◆ s_addressing_modes

const char* s_addressing_modes[]
Initial value:
= {
"<invalid mode>",
"Register Direct - Data",
"Register Direct - Address",
"Register Indirect - Address",
"Register Indirect - Address with Postincrement",
"Register Indirect - Address with Predecrement",
"Register Indirect - Address with Displacement",
"Address Register Indirect With Index - 8-bit displacement",
"Address Register Indirect With Index - Base displacement",
"Memory indirect - Postindex",
"Memory indirect - Preindex",
"Program Counter Indirect - with Displacement",
"Program Counter Indirect with Index - with 8-Bit Displacement",
"Program Counter Indirect with Index - with Base Displacement",
"Program Counter Memory Indirect - Postindexed",
"Program Counter Memory Indirect - Preindexed",
"Absolute Data Addressing - Short",
"Absolute Data Addressing - Long",
"Immediate value",
}

Definition at line 31 of file test_m68k.c.

Referenced by print_insn_detail().