-
Notifications
You must be signed in to change notification settings - Fork 11
/
asm.c
601 lines (560 loc) · 15.1 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
// Input file.
static FILE* fi;
// Output file.
static FILE* fo;
// Name of assembly file.
static char* assem;
// Name of hex file.
static char* hexid;
// Failure flag goes high if anything went wrong during assembly.
static bool failure;
// Cleans up files. Uses failure flag to remove output hex file.
static void fshutdown()
{
if(fi) fclose(fi);
if(fo) fclose(fo);
if(failure)
remove(hexid);
}
// Inits files.
static void finit(char* argv[])
{
assem = argv[1];
hexid = argv[2];
// Input
fi = fopen(argv[1], "r");
if(fi == NULL)
{
fprintf(stderr, "error: %s does not exist\n", assem);
exit(1);
}
// Output
fo = fopen(argv[2], "w");
if(fo == NULL)
{
fprintf(stderr, "error: %s cannot be made\n", hexid);
exit(1);
}
atexit(fshutdown);
}
// Duplicates a string.
static char* dup(char* s)
{
int len = strlen(s) + 1;
char* p = (char*) malloc(len);
return p ? (char*) memcpy(p, s, len) : NULL;
}
// Rewinds both the input and output files.
static void frewind()
{
rewind(fi);
rewind(fo);
}
// Binary tree node.
// Keeps track of label names and their addresses.
struct node
{
char* name;
unsigned address;
struct node* l;
struct node* r;
};
// New binary tree node.
static struct node* build(char* name, unsigned address)
{
struct node* node = (struct node*) malloc(sizeof(*node));
node->name = dup(name);
node->address = address;
node->l = NULL;
node->r = NULL;
return node;
}
// Inserts a new node into a binary tree.
static struct node* insert(struct node* nodes, struct node* node)
{
if(nodes == NULL)
return node;
int difference = strcmp(node->name, nodes->name);
if(difference == 0)
{
free(node->name);
free(node);
failure = true;
}
else if(difference < 0)
nodes->l = insert(nodes->l, node);
else
nodes->r = insert(nodes->r, node);
return nodes;
}
// Gets a node from a binary tree. Returns NULL if node was not found.
static struct node* get(struct node* nodes, const char* name)
{
if(nodes == NULL)
return NULL;
int difference = strcmp(name, nodes->name);
if(difference == 0)
return nodes;
else if(difference < 0)
return get(nodes->l, name);
else
return get(nodes->r, name);
}
// Cleans up the binary tree.
static void burn(struct node* nodes)
{
if(nodes == NULL)
return;
burn(nodes->l);
burn(nodes->r);
free(nodes->name);
free(nodes);
}
static int add(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// ADD Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%c4\n", a[1], b[1]);
// ADD I, Vx.
else
if(strlen(a) == 1 && a[0] == 'I' &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "F%c1E\n", b[1]);
// ADD Vx, byte.
else
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 4 && strncmp(b, "0x", 2) == 0 &&
isxdigit(b[2]) &&
isxdigit(b[3]))
fprintf(fo, "7%c%c%c\n", a[1], b[2], b[3]);
else
return 1;
return 0;
}
static int _and(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// AND Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%c2\n", a[1], b[1]);
else
return 1;
return 0;
}
static int call(char* operand, struct node* labels)
{
char* a = strtok(operand, "\t ");
struct node* found = get(labels, a);
// CALL address.
if(found)
fprintf(fo, "2%03X\n", found->address);
else
return 2;
return 0;
}
static int cls(char* operand, struct node* labels)
{
(void )operand, (void) labels;
// CLS.
fprintf(fo, "00E0\n");
return 0;
}
static int db(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ");
// DB (Define Byte).
if(strlen(a) == 4 && strncmp(a, "0x", 2) == 0 &&
isxdigit(a[2]) &&
isxdigit(a[3]))
fprintf(fo, "%c%c\n", a[2], a[3]);
else
return 1;
return 0;
}
static int drw(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ,");
char* c = strtok(NULL, "\t ");
// DRW Vx, Vy, nibble.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]) &&
strlen(c) == 3 && strncmp(c, "0x", 2) == 0 && isxdigit(c[2]))
fprintf(fo, "D%c%c%c\n", a[1], b[1], c[2]);
else
return 1;
return 0;
}
static int jp(char* operand, struct node* labels)
{
struct node* found;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// JP V0, address.
if(strlen(a) == 2 && a[0] == 'V' && a[1] == '0' &&
(found = get(labels, b)))
fprintf(fo, "B%03X\n", found->address);
// JP address.
else
if((found = get(labels, a)))
fprintf(fo, "1%03X\n", found->address);
else
return 2;
return 0;
}
static int ld(char* operand, struct node* labels)
{
struct node* found;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// LD DT, Vx.
if(strlen(a) == 2 && a[0] == 'D' && a[1] == 'T' &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "F%c15\n", b[1]);
// LD ST, Vx.
else
if(strlen(a) == 2 && a[0] == 'S' && a[1] == 'T' &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "F%c18\n", b[1]);
// LD F, Vx.
else
if(strlen(a) == 1 && a[0] == 'F' &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "F%c29\n", b[1]);
// LD B, Vx.
else
if(strlen(a) == 1 && a[0] == 'B' &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "F%c33\n", b[1]);
// LD Vx, DT.
else
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'D' && b[1] == 'T')
fprintf(fo, "F%c07\n", a[1]);
// LD Vx, [I].
else
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 3 && b[0] == '[' && b[1] == 'I' && b[2] == ']')
fprintf(fo, "F%c65\n", a[1]);
// LD Vx, K.
else
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 1 && b[0] == 'K')
fprintf(fo, "F%c0A\n", a[1]);
// LD Vx, Vy.
else
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%c0\n", a[1], b[1]);
// LD Vx, byte.
else
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
(strlen(b) == 4 && strncmp(b, "0x", 2)) == 0 &&
isxdigit(b[2]) &&
isxdigit(b[3]))
fprintf(fo, "6%c%c%c\n", a[1], b[2], b[3]);
// LD [I], Vx.
else
if(strlen(a) == 3 && a[0] == '[' && a[1] == 'I' && a[2] == ']' &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "F%c55\n", b[1]);
// LD I, address.
else
if(strlen(a) == 1 && a[0] == 'I')
{
if((found = get(labels, b)))
fprintf(fo, "A%03X\n", found->address);
else
return 2;
}
else
return 1;
return 0;
}
static int _or(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// OR Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%c1\n", a[1], b[1]);
else
return 1;
return 0;
}
static int ret(char* operand, struct node* labels)
{
(void) operand, (void) labels;
// RET.
fprintf(fo, "00EE\n");
return 0;
}
static int rnd(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// RND Vx, byte.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 4 && strncmp(b, "0x", 2) == 0 &&
isxdigit(b[2]) &&
isxdigit(b[3]))
fprintf(fo, "C%c%c%c\n", a[1], b[2], b[3]);
else
return 1;
return 0;
}
static int se(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// SE Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "5%c%c0\n", a[1], b[1]);
// SE Vx, byte.
else
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 4 && strncmp(b, "0x", 2) == 0 &&
isxdigit(b[2]) &&
isxdigit(b[3]))
fprintf(fo, "3%c%c%c\n", a[1], b[2], b[3]);
else
return 1;
return 0;
}
static int shl(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// SHL Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%cE\n", a[1], b[1]);
else
return 1;
return 0;
}
static int shr(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// SHR Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%c6\n", a[1], b[1]);
else
return 1;
return 0;
}
static int sknp(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ");
// SKNP Vx.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]))
fprintf(fo, "E%cA1\n", a[1]);
else
return 1;
return 0;
}
static int skp(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ");
// SKP Vx.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]))
fprintf(fo, "E%c9E\n", a[1]);
else
return 1;
return 0;
}
static int sne(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// SNE Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "9%c%c0\n", a[1], b[1]);
// SNE Vx, byte.
else
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 4 && strncmp(b, "0x", 2) == 0 &&
isxdigit(b[2]) &&
isxdigit(b[3]))
fprintf(fo, "4%c%c%c\n", a[1], b[2], b[3]);
else
return 1;
return 0;
}
static int sub(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// SUB Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%c5\n", a[1], b[1]);
else
return 1;
return 0;
}
static int subn(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// SUBN Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%c7\n", a[1], b[1]);
else
return 1;
return 0;
}
static int _xor(char* operand, struct node* labels)
{
(void) labels;
char* a = strtok(operand, "\t ,");
char* b = strtok(NULL, "\t ");
// XOR Vx, Vy.
if(strlen(a) == 2 && a[0] == 'V' && isxdigit(a[1]) &&
strlen(b) == 2 && b[0] == 'V' && isxdigit(b[1]))
fprintf(fo, "8%c%c3\n", a[1], b[1]);
else
return 1;
return 0;
}
static int (*functions[])(char* operand, struct node* labels) = {
add, _and, call, cls, db, drw, jp, ld, _or, ret, rnd, se,
shl, shr, sknp, skp, sne, sub, subn, _xor
};
static const char* mnemonics[] = {
"ADD","AND","CALL","CLS","DB","DRW","JP","LD","OR","RET","RND","SE",
"SHL","SHR","SKNP","SKP","SNE","SUB","SUBN","XOR"
};
static int compare(const void* a, const void* b)
{
return strcmp((char*)a, *(char**)b);
}
static int assemble(char* mnemonic, char* operand, struct node* labels)
{
const char** found = (const char**) bsearch(
mnemonic,
mnemonics,
sizeof(mnemonics) / sizeof(*mnemonics),
sizeof(*mnemonics),
compare);
if(!found)
return 3;
return (*functions[found - mnemonics])(operand, labels);
}
static struct node* scan(struct node* labels)
{
unsigned address = 0x0202;
const bool growing = labels == NULL;
char line[320];
for(unsigned linenumber = 1; fgets(line, sizeof(line), fi); linenumber++)
{
char* label;
char* mnemonic;
char* operand;
char* newline;
char* semicolon;
char* colon;
int error = 0;
newline = strchr(line, '\n');
if(newline)
*newline = '\0';
semicolon = strchr(line, ';');
if(semicolon)
*semicolon = '\0';
colon = strchr(line, ':');
if(colon)
{
label = strtok(line, "\t :");
if(growing)
{
labels = insert(labels, build(label, address));
if(failure)
{
fprintf(stderr, "error: line %d: %s already defined\n", linenumber, label);
exit(1);
}
}
}
mnemonic = strtok(colon ? NULL : line, "\t ");
operand = strtok(NULL, "");
if(mnemonic)
{
if(growing)
address += strcmp(mnemonic, "DB") == 0 ? 0x0001 : 0x0002;
else
error = assemble(mnemonic, operand, labels);
}
if(error)
{
const char* types[] = {
/* 0 */ "no error",
/* 1 */ "operand formatting",
/* 2 */ "label not found",
/* 3 */ "unsupported chip8 mnemonic"
};
failure = true;
fprintf(stderr, "error: line %d: %s\n", linenumber, types[error]);
exit(1);
}
}
return labels;
}
// Generates the reset vector for the entry label.
static void rvec(struct node* labels, const char* entry)
{
struct node* reset = get(labels, entry);
if(!reset)
{
fprintf(stderr, "error: entry point %s not found\n", entry);
exit(1);
}
fprintf(fo, "1%03X\n", reset->address);
}
int main(int argc, char* argv[])
{
if(argc != 3)
{
fprintf(stderr, "expected input and output arguments");
exit(1);
}
finit(argv);
// First pass.
struct node* labels = NULL;
labels = scan(labels);
frewind();
// Second pass.
rvec(labels, "main");
scan(labels);
burn(labels);
exit(0);
}