-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm.c
507 lines (411 loc) · 11.7 KB
/
asm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Dwarf - A minimalist 16-bit RISC CPU
*
* asm.c: Crude, brutal and unforgiving assembly language translator
*
* Copyright (c) 2017, Helmut Sipos <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <sysexits.h>
#include <sys/stat.h>
#define OK 0
#define RESET_VECTOR 0x0000
char *save_ptr_token = NULL;
typedef struct {
const char *instr;
const uint16_t opcode;
const uint8_t form;
} tInstr;
const char *regs[] = { "r0" ,"r1" ,"r2" ,"r3" ,"r4" ,"r5" ,"r6" ,"r7"
,"r8" ,"r9" ,"r10" ,"r11" ,"r12" ,"r13" ,"r14" ,"r15" };
#define NR_REGS (sizeof (regs) / sizeof (char*))
static int
get_reg (char *reg, uint8_t *value)
{
int i;
int rc = !OK;
for (i = 0; i < NR_REGS; i++)
if (!strcmp (regs[i], reg)) {
*value = i;
rc = OK;
break;
}
return rc;
}
/* Instruction forms */
#define FRM_OP_NOTHING 0x0 /* "" */
#define FRM_OP_SO_RT 0x1 /* "%s" */
#define FRM_OP_SO_RT_RD 0x2 /* "%s %s" */
#define FRM_OP_SO_RT_IMM4 0x3 /* "%s %s" */
#define FRM_OP_RS_LIMM8 0x4 /* "%s %s" */
#define FRM_OP_RS_UIMM8 0x5 /* "%s %s" */
#define FRM_OP_RS_RT_RD 0x6 /* "%s %s %s" */
#define FRM_OP_IMM12 0x7 /* "%s" */
/* Instruction set */
#define RDM_OP_SO 0x0100 /* r[rd] = *(uint16*)r[rt]; */
#define WRM_OP_SO 0x0200 /* *(uint16*)r[rt] = r[rd]; */
#define MOV_OP_SO 0x0300 /* r[rd] = r[rt]; */
#define NOT_OP_SO 0x0400 /* r[rd] = ~r[rt]; */
#define SKS_OP_SO 0x0500 /* if (r[rt] & (1 << imm4)) pc_next += 4; */
#define SKC_OP_SO 0x0600 /* if (!(r[rt] & (1 << imm4))) pc_next += 4; */
#define SKZ_OP_SO 0x0700 /* if (r[rt] == 0) pc_next += 4; */
#define SKN_OP_SO 0x0800 /* if (r[rt] != 0) pc_next += 4; */
#define BRR_OP_SO 0x0900 /* pc_next = r[rt]; */
#define LDU_OP_SO 0x1000 /* r[rs] = (imm8 << 8) | 0x0; */
#define SHR_OP_SO 0x2000 /* r[rd] = r[rs] >> r[rt]; */
#define ROR_OP_SO 0x3000 /* r[rd] = r[rs] >>> r[rt]; */
#define SUB_OP_SO 0x4000 /* r[rd] = r[rs] - r[rt]; */
#define SUBI_OP_SO 0x5000 /* r[rs] = r[rs] - imm8; */
#define ADD_OP_SO 0x6000 /* r[rd] = r[rs] + r[rt]; */
#define ADDI_OP_SO 0x7000 /* r[rs] = r[rs] + imm8; */
#define MUL_OP_SO 0x8000 /* r[rd] = r[rs] * r[rt]; */
#define OR_OP_SO 0x9000 /* r[rd] = r[rs] | r[rt]; */
#define ORI_OP_SO 0xA000 /* r[rs] = r[rs] | imm8; */
#define XOR_OP_SO 0xB000 /* r[rd] = r[rs] | r[rt]; */
#define AND_OP_SO 0xC000 /* r[rd] = r[rs] & r[rt]; */
#define ANDI_OP_SO 0xD000 /* r[rs] = r[rs] & imm8; */
#define CMP_OP_SO 0xE000 /* r[rd] = r[rs] ? r[rt]; */
#define BRL_OP_SO 0xF000 /* r[15] = pc_next; pc_next = imm12; */
#define NOP_OP_SO 0x0000 /* nop */
const tInstr instrSet[] = {
{"rdm", RDM_OP_SO, FRM_OP_SO_RT_RD}
,{"wrm", WRM_OP_SO, FRM_OP_SO_RT_RD}
,{"mov", MOV_OP_SO, FRM_OP_SO_RT_RD}
,{"not", NOT_OP_SO, FRM_OP_SO_RT_RD}
,{"sks", SKS_OP_SO, FRM_OP_SO_RT_IMM4}
,{"skc", SKC_OP_SO, FRM_OP_SO_RT_IMM4}
,{"skz", SKZ_OP_SO, FRM_OP_SO_RT}
,{"skn", SKN_OP_SO, FRM_OP_SO_RT}
,{"brr", BRR_OP_SO, FRM_OP_SO_RT}
,{"ldu", LDU_OP_SO, FRM_OP_RS_UIMM8}
,{"shr", SHR_OP_SO, FRM_OP_RS_RT_RD}
,{"ror", ROR_OP_SO, FRM_OP_RS_RT_RD}
,{"sub", SUB_OP_SO, FRM_OP_RS_RT_RD}
,{"subi", SUBI_OP_SO, FRM_OP_RS_LIMM8}
,{"add", ADD_OP_SO, FRM_OP_RS_RT_RD}
,{"addi", ADDI_OP_SO, FRM_OP_RS_LIMM8}
,{"mul", MUL_OP_SO, FRM_OP_RS_RT_RD}
,{"or", OR_OP_SO, FRM_OP_RS_RT_RD}
,{"ori", ORI_OP_SO, FRM_OP_RS_LIMM8}
,{"xor", XOR_OP_SO, FRM_OP_RS_RT_RD}
,{"and", AND_OP_SO, FRM_OP_RS_RT_RD}
,{"andi", ANDI_OP_SO, FRM_OP_RS_LIMM8}
,{"cmp", CMP_OP_SO, FRM_OP_RS_RT_RD}
,{"brl", BRL_OP_SO, FRM_OP_IMM12}
,{"nop", NOP_OP_SO, FRM_OP_NOTHING}
};
#define NR_INSTR (sizeof (instrSet) / sizeof (tInstr))
static int
get_opcode (char *instr, uint16_t *opcode, uint8_t *form)
{
int i;
int rc = !OK;
for (i = 0; i < NR_INSTR; i++)
if (!strcmp (instrSet[i].instr, instr)) {
*opcode = instrSet[i].opcode;
*form = instrSet[i].form;
rc = OK;
break;
}
return rc;
}
typedef struct sSymbol {
char *name;
uint16_t value;
struct sSymbol *next;
} tSymbol;
tSymbol *symbols = NULL;
static tSymbol*
find_symbol (const char *name)
{
tSymbol *symb = NULL;
for (symb = symbols; symb; symb = symb->next)
if (!strcmp (symb->name, name))
break;
return symb;
}
static void
add_symbol (const char *name, uint16_t value)
{
tSymbol *tmp = NULL;
tSymbol *symb = (tSymbol*) malloc (sizeof (tSymbol));
symb->name = strdup (name);
symb->value = value;
symb->next = NULL;
if (!symbols)
symbols = symb;
else {
for (tmp = symbols; tmp->next; tmp = tmp->next)
;
tmp->next = symb;
}
}
static uint8_t
get_reg_param (void)
{
char *par = NULL;
uint8_t reg = 0xFF;
if ((par = strtok_r (NULL, ", \t\n", &save_ptr_token)) == NULL) {
printf("PANIC: missing register parameter\n");
exit (EX_DATAERR);
}
if (get_reg(par, ®) != OK)
{
printf("PANIC: unknown register '%s'\n", par);
exit (EX_DATAERR);
}
return reg;
}
static uint16_t
get_imm_param (int pass)
{
char *par = NULL;
char *endptr = NULL;
uint16_t imm = 0xDEAD;
if (pass != 0) {
if ((par = strtok_r (NULL, ", \t\n", &save_ptr_token)) == NULL) {
printf("PANIC: missing register parameter\n");
exit (EX_DATAERR);
}
imm = strtol(par, &endptr, 0);
if ((par == NULL) || (*endptr != '\0')) {
tSymbol *sym;
if (!(sym = find_symbol (par))) {
printf("PANIC: symbol '%s' not found\n", par);
exit (EX_DATAERR);
}
imm = sym->value;
}
}
return imm;
}
static void
usage (void)
{
printf ("usage: asm [-l] <-f in.s>\n");
exit (EX_USAGE);
}
int
main (int argc, char **argv)
{
struct stat st;
off_t src_len = 0;
FILE *fin = NULL;
char *file = NULL;
char *src = NULL;
char *save_ptr_line = NULL;
char *line = NULL;
char *ptr = NULL;
char *token = NULL;
char *orig_line = NULL;
uint16_t pc = RESET_VECTOR;
int line_nr = 0;
int pass = 0;
int list = 0;
int opt;
if (argc < 2)
usage ();
while ((opt = getopt (argc, argv, "lf:")) != -1)
switch (opt) {
case 'l':
list = 1;
break;
case 'f':
file = strdup(optarg);
break;
default:
abort ();
}
if (!file)
usage();
fin = fopen (file, "r");
fstat (fileno (fin), &st);
src_len = st.st_size;
src = (char *) malloc (src_len + 1);
memset (src, 0, src_len + 1);
fread (src, sizeof (char), src_len, fin);
fclose (fin);
for (pass = 0; pass < 2; pass++) {
pc = RESET_VECTOR;
line_nr = 0;
line = strtok_r (strdup (src), "\n", &save_ptr_line);
while (line) {
orig_line = strdup (line);
line_nr++;
if ((ptr = rindex (line, ';')))
*ptr = 0;
if ((token = strtok_r (line, ", \t\n", &save_ptr_token)) != NULL) {
if (token[strlen (token) - 1] == ':') {
*index(token, ':') = 0;
if (find_symbol (token)) {
if (pass == 0) {
printf ("PANIC: duplicated symbol: '%s' in line: %d\n", token, line_nr);
exit (EX_DATAERR);
}
}
else
add_symbol (token, pc);
if (pass && list)
printf ("\t\t%s:\n", token);
}
else if (token[0] == '.') {
char *endptr = NULL;
uint16_t val = 0xDEAD;
char *par = strtok_r (NULL, " ,\t\n", &save_ptr_token);
val = strtol(par, &endptr, 0);
if (*endptr != '\0') {
printf ("PANIC: can not translate value '%s'\n", par);
exit (EX_DATAERR);
}
if (find_symbol (&token[1])) {
if (pass == 0) {
printf ("PANIC: duplicated symbol: '%s' in line: %d\n", &token[1], line_nr);
exit (EX_DATAERR);
}
}
else
add_symbol (&token[1], val);
if (pass && list)
printf ("\t\t%s %s\n", &token[1], par);
}
else if (token[0] == '$') {
char *par = NULL;
int skip = 0;
if (list)
printf ("\n");
while ((par = strtok_r (NULL, ",\t\n", &save_ptr_token)) != NULL) {
for (skip = 0; par[skip] == ' '; skip++)
;
if (par[skip] == '"') {
int i;
for (i=skip + 1; par[i] != '"'; i++) {
if (pass && list)
printf ("%04X %04X\t\t'%c'\n", pc, (uint16_t)par[i], par[i]);
pc += 2;
}
}
else {
char *endptr = NULL;
uint16_t val = 0xDEAD;
val = strtol(&par[skip], &endptr, 0);
if (*endptr != '\0') {
printf ("PANIC: can not translate value '%s'\n", &par[skip]);
exit (EX_DATAERR);
}
if (pass && list)
printf ("%04X %04X\t\t%s\n", pc, val, &par[skip]);
pc += 2;
}
}
if (list)
printf ("\n");
}
else if (token[0] == '@') {
char *endptr = NULL;
uint16_t val = 0xDEAD;
val = strtol(&token[1], &endptr, 0);
if (*endptr != '\0') {
printf ("PANIC: can not translate given address '%s'\n", &token[1]);
exit (EX_DATAERR);
}
pc = val;
}
else {
uint16_t opcode = 0;
uint16_t val = 0;
uint8_t form = 0;
if (get_opcode (token, &opcode, &form) != OK) {
printf ("PANIC: unknown mnemonic: '%s' in line: %d\n", token, line_nr);
exit (EX_DATAERR);
}
switch (form) {
case FRM_OP_SO_RT:
val = get_reg_param ();
opcode |= val << 4;
break;
case FRM_OP_SO_RT_RD:
val = get_reg_param ();
opcode |= val << 4;
val = get_reg_param ();
opcode |= val;
break;
case FRM_OP_SO_RT_IMM4:
val = get_reg_param ();
opcode |= val << 4;
val = get_imm_param(pass);
opcode |= val & 0xF;
break;
case FRM_OP_RS_LIMM8:
val = get_reg_param ();
opcode |= val << 8;
val = get_imm_param(pass);
opcode |= val & 0xFF;
break;
case FRM_OP_RS_UIMM8:
val = get_reg_param ();
opcode |= val << 8;
val = get_imm_param(pass);
opcode |= (val >> 8) & 0xFF;
break;
case FRM_OP_RS_RT_RD:
val = get_reg_param ();
opcode |= val << 8;
val = get_reg_param ();
opcode |= val << 4;
val = get_reg_param ();
opcode |= val;
break;
case FRM_OP_IMM12:
val = get_imm_param(pass);
opcode |= (val >> 1) & 0xFFF;
break;
case FRM_OP_NOTHING:
break;
default:
printf ("PANIC: unknown form: '%02X' in line: %d\n", form, line_nr);
exit (EX_DATAERR);
break;
}
if (pass) {
if (list)
printf ("%04X:%04X\t%s\n",pc, opcode, orig_line);
else
printf ("%04X:%04X\n",pc, opcode);
}
pc += 2;
}
}
if (orig_line)
free (orig_line);
line = strtok_r (NULL, "\n", &save_ptr_line);
}
}
return EX_OK;
}