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

Go to the source code of this file.

Functions

static int disassemble (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 
static bool _write_asm (RzAsmOp *op, int value, int n)
 
static int assemble (RzAsm *a, RzAsmOp *op, const char *buf)
 

Variables

RzAsmPlugin rz_asm_plugin_bf
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ _write_asm()

static bool _write_asm ( RzAsmOp op,
int  value,
int  n 
)
static

Definition at line 48 of file asm_bf.c.

48  {
49  ut8 *opbuf = malloc(n);
50  if (opbuf == NULL) {
51  return true;
52  }
53  memset(opbuf, value, n);
54  rz_strbuf_setbin(&op->buf, opbuf, n);
55  free(opbuf);
56  return false;
57 }
static int value
Definition: cmd_api.c:93
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
void * malloc(size_t size)
Definition: malloc.c:123
int n
Definition: mipsasm.c:19
RZ_API bool rz_strbuf_setbin(RzStrBuf *sb, const ut8 *s, size_t len)
Definition: strbuf.c:85
Definition: dis.c:32

References free(), malloc(), memset(), n, NULL, rz_strbuf_setbin(), and value.

Referenced by assemble().

◆ assemble()

static int assemble ( RzAsm a,
RzAsmOp op,
const char *  buf 
)
static

Definition at line 59 of file asm_bf.c.

59  {
60  int n = 0;
61  if (buf[0] && buf[1] == ' ') {
62  buf += 2;
63  }
64  const char *arg = strchr(buf, ',');
65  const char *ref = strchr(buf, '[');
66  bool write_err = false;
67  if (arg) {
68  n = atoi(arg + 1);
69  } else {
70  n = 1;
71  }
72  if (!strncmp(buf, "trap", 4)) {
73  write_err = _write_asm(op, 0xcc, n);
74  } else if (!strncmp(buf, "nop", 3)) {
75  write_err = _write_asm(op, 0x90, n);
76  } else if (!strncmp(buf, "inc", 3)) {
77  char ch = ref ? '+' : '>';
78  n = 1;
79  write_err = _write_asm(op, ch, n);
80  } else if (!strncmp(buf, "dec", 3)) {
81  char ch = ref ? '-' : '<';
82  n = 1;
83  write_err = _write_asm(op, ch, n);
84  } else if (!strncmp(buf, "sub", 3)) {
85  char ch = ref ? '-' : '<';
86  write_err = _write_asm(op, ch, n);
87  } else if (!strncmp(buf, "add", 3)) {
88  char ch = ref ? '+' : '>';
89  write_err = _write_asm(op, ch, n);
90  } else if (!strncmp(buf, "while", 5)) {
91  n = 1;
92  write_err = _write_asm(op, '[', 1);
93  } else if (!strncmp(buf, "loop", 4)) {
94  n = 1;
95  write_err = _write_asm(op, ']', 1);
96  } else if (!strncmp(buf, "in", 2)) {
97  write_err = _write_asm(op, ',', n);
98  } else if (!strncmp(buf, "out", 3)) {
99  write_err = _write_asm(op, '.', n);
100  } else {
101  n = 0;
102  }
103  if (write_err) {
104  return 0;
105  }
106  return n;
107 }
static bool _write_asm(RzAsmOp *op, int value, int n)
Definition: asm_bf.c:48
voidpf void * buf
Definition: ioapi.h:138

References _write_asm(), and n.

◆ disassemble()

static int disassemble ( RzAsm a,
RzAsmOp op,
const ut8 buf,
int  len 
)
static

Definition at line 7 of file asm_bf.c.

7  {
8  const char *buf_asm = "invalid";
9  switch (*buf) {
10  case '[':
11  buf_asm = "while [ptr]";
12  break;
13  case ']':
14  buf_asm = "loop";
15  break;
16  case '>':
17  buf_asm = "inc ptr";
18  break;
19  case '<':
20  buf_asm = "dec ptr";
21  break;
22  case '+':
23  buf_asm = "inc [ptr]";
24  break;
25  case '-':
26  buf_asm = "dec [ptr]";
27  break;
28  case ',':
29  buf_asm = "in [ptr]";
30  break;
31  case '.':
32  buf_asm = "out [ptr]";
33  break;
34  case 0xff:
35  case 0x00:
36  buf_asm = "trap";
37  break;
38  default:
39  buf_asm = "nop";
40  break;
41  }
42 
43  rz_strbuf_set(&op->buf_asm, buf_asm);
44  op->size = 1;
45  return op->size;
46 }
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153

References rz_strbuf_set().

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_ASM,
.data = &rz_asm_plugin_bf,
}
RzAsmPlugin rz_asm_plugin_bf
Definition: asm_bf.c:109
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_asm.h:133

Definition at line 123 of file asm_bf.c.

◆ rz_asm_plugin_bf

RzAsmPlugin rz_asm_plugin_bf
Initial value:
= {
.name = "bf",
.author = "pancake, nibble",
.version = "4.0.0",
.arch = "bf",
.license = "LGPL3",
.bits = 16 | 32 | 64,
.endian = RZ_SYS_ENDIAN_NONE,
.desc = "Brainfuck",
.disassemble = &disassemble,
.assemble = &assemble
}
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_bf.c:7
static int assemble(RzAsm *a, RzAsmOp *op, const char *buf)
Definition: asm_bf.c:59
#define RZ_SYS_ENDIAN_NONE
Definition: rz_types.h:525

Definition at line 109 of file asm_bf.c.