Rizin
unix-like reverse engineering framework and cli tools
pic_pic18.h File Reference
#include <rz_asm.h>

Go to the source code of this file.

Functions

int pic_pic18_disassemble (RzAsmOp *op, char *opbuf, const ut8 *b, int l)
 

Function Documentation

◆ pic_pic18_disassemble()

int pic_pic18_disassemble ( RzAsmOp op,
char *  opbuf,
const ut8 b,
int  l 
)

Definition at line 114 of file pic_pic18.c.

114  {
115  int i;
116  if (blen < 2) { // well noone loves reading bitstream of size zero or 1 !!
117  strcpy(opbuf, "invalid");
118  op->size = blen;
119  return -1;
120  }
121  ut16 instr = rz_read_le16(b); // instruction
122  // if still redundan code is reported think of this of instr=0x2
123  const char *buf_asm = "invalid";
124  strcpy(opbuf, buf_asm);
125 
126  for (i = 0; ops[i].opmin != (ops[i].opmin & instr) || ops[i].opmax != (ops[i].opmax | instr); i++) {
127  ;
128  }
129  if (ops[i].opmin == 0 && ops[i].opmax == 0xffff) {
130  strcpy(opbuf, ops[i].name);
131  op->size = 2;
132  return -1;
133  }
134  op->size = 2;
135  switch (ops[i].optype) {
136  case NO_ARG:
137  buf_asm = ops[i].name;
138  break;
139  case N_T:
140  case K_T:
141  buf_asm = sdb_fmt("%s 0x%x", ops[i].name, instr & 0xff);
142  break;
143  case DAF_T:
144  buf_asm = sdb_fmt("%s 0x%x, %d, %d", ops[i].name, instr & 0xff, (instr >> 9) & 1, (instr >> 8) & 1);
145  break;
146  case AF_T:
147  buf_asm = sdb_fmt("%s 0x%x, %d", ops[i].name, instr & 0xff, (instr >> 8) & 1);
148  break;
149  case BAF_T:
150  buf_asm = sdb_fmt("%s 0x%x, %d, %d", ops[i].name, instr & 0xff, (instr >> 9) & 0x7, (instr >> 8) & 0x1);
151  break;
152  case NEX_T:
153  buf_asm = sdb_fmt("%s 0x%x", ops[i].name, instr & 0x7ff);
154  break;
155  case CALL_T:
156  if (blen < 4) {
157  return -1;
158  }
159  op->size = 4;
160  {
161  ut32 dword_instr = rz_read_le32(b);
162  // I dont even know how the bits are arranged but it works !!!
163  //`the wierdness of little endianess`
164  if (dword_instr >> 28 != 0xf) {
165  return -1;
166  }
167  buf_asm = sdb_fmt("%s 0x%x, %d", ops[i].name,
168  (dword_instr & 0xff) | (dword_instr >> 8 & 0xfff00), (dword_instr >> 8) & 0x1);
169  }
170  break;
171  case GOTO_T:
172  if (blen < 4) {
173  return -1;
174  }
175  {
176  op->size = 4;
177  ut32 dword_instr = rz_read_le32(b);
178  if (dword_instr >> 28 != 0xf) {
179  return -1;
180  }
181  buf_asm = sdb_fmt("%s 0x%x", ops[i].name,
182  ((dword_instr & 0xff) | ((dword_instr & 0xfff0000) >> 8)) * 2);
183  }
184  break;
185  case F32_T:
186  if (blen < 4) {
187  return -1;
188  }
189  op->size = 4;
190  {
191  ut32 dword_instr = rz_read_le32(b);
192  if (dword_instr >> 28 != 0xf) {
193  return -1;
194  }
195  buf_asm = sdb_fmt("%s 0x%x, 0x%x", ops[i].name,
196  dword_instr & 0xfff, (dword_instr >> 16) & 0xfff);
197  }
198  break;
199  case SHK_T:
200  buf_asm = sdb_fmt("%s 0x%x", ops[i].name, instr & 0xf);
201  break;
202  case S_T:
203  buf_asm = sdb_fmt("%s %d", ops[i].name, instr & 0x1);
204  break;
205  case LFSR_T: {
206  if (blen < 4) {
207  return -1;
208  }
209  op->size = 4;
210  ut32 dword_instr = rz_read_le32(b);
211  if (dword_instr >> 28 != 0xf) {
212  return -1;
213  }
214  ut8 reg_n = (dword_instr >> 4) & 0x3;
215  buf_asm = sdb_fmt("%s %s, %d", ops[i].name, fsr[reg_n],
216  (dword_instr & 0xf) << 8 | ((dword_instr >> 16) & 0xff));
217  break;
218  }
219  default:
220  buf_asm = "unknown args";
221  };
222  strcpy(opbuf, buf_asm);
223  return op->size;
224 }
lzma_index ** i
Definition: index.h:629
uint16_t ut16
uint32_t ut32
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
uint8_t ut8
Definition: lh5801.h:11
#define BAF_T
Definition: pic_pic18.c:15
#define GOTO_T
Definition: pic_pic18.c:21
#define NO_ARG
Definition: pic_pic18.c:12
#define K_T
Definition: pic_pic18.c:16
ut16 opmin
Definition: pic_pic18.c:29
ut16 opmax
Definition: pic_pic18.c:30
#define SHK_T
Definition: pic_pic18.c:22
#define S_T
Definition: pic_pic18.c:23
#define DAF_T
Definition: pic_pic18.c:13
static char * fsr[]
Definition: pic_pic18.c:26
#define CALL_T
Definition: pic_pic18.c:18
static struct @106 ops[]
#define N_T
Definition: pic_pic18.c:17
#define AF_T
Definition: pic_pic18.c:20
#define F32_T
Definition: pic_pic18.c:14
#define NEX_T
Definition: pic_pic18.c:19
#define LFSR_T
Definition: pic_pic18.c:24
static ut16 rz_read_le16(const void *src)
Definition: rz_endian.h:206
static ut32 rz_read_le32(const void *src)
Definition: rz_endian.h:239
#define b(i)
Definition: sha256.c:42
Definition: z80asm.h:102
Definition: op.c:222
Definition: dis.c:32

References AF_T, b, BAF_T, CALL_T, DAF_T, F32_T, fsr, GOTO_T, i, K_T, LFSR_T, N_T, NEX_T, NO_ARG, opmax, opmin, ops, rz_read_le16(), rz_read_le32(), S_T, sdb_fmt(), and SHK_T.

Referenced by asm_pic_disassemble().