-
Notifications
You must be signed in to change notification settings - Fork 8
/
assembler.c
733 lines (619 loc) · 17.9 KB
/
assembler.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
* assembler.c
* ------
* Reads a 8bit cpu assembler file and writes a compiled executable
* for our architecture.
*
* Compile project via: clang assembler.c -o assembler
*
* Usage: ./assembler program.asm program.out
*/
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VERSION "0.2"
#define PROGRAM_MEMORY_SIZE 256
#define MAX_JUMP_COUNT 64
#define MAX_LABEL_COUNT 32
#define MAX_LABEL_LENGTH 32
#define MAX_LINE_LENGTH 128
#define OPCODE_COUNT 33
/*
* Struct: label
* ------
* Placeholder for occurring label and jump declaration, mapping
* the address in the program memory to the label name.
*/
struct label
{
int memory_addr;
char label[MAX_LABEL_LENGTH];
};
/*
* Struct: opcode
* ------
* Holds data about opcode mnemonic and its microcode addresses
* for the regarding addressing modes.
*/
typedef struct
{
char mnemonic[3]; // Mnemonic of opcode
int a; // Implicit / single-byte instruction (no operand)
int a_abs; // Absolute $nn
int a_imm; // Immediate #$nn
int a_idx; // Indexed $nn,a
int a_idx_ind; // Indexed Indirect ($nn,a)
int a_ind; // Indirect ($nn)
int a_ind_idx; // Indirect Indexed ($nn),a
int a_label; // Labelled (internal)
} opcode;
/*
* Table: OPCODES[]
* ------
* Lists all given opcodes of our architecture defining
* their microcode addresses in the control unit (CU).
*/
static const opcode OPCODES[] =
{
{ "adc", 0x00, 0x5a, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "and", 0x00, 0x70, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "asl", 0x8b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "bcc", 0x00, 0x00, 0x98, 0x00, 0x00, 0x00, 0x00, 0x98 },
{ "bcs", 0x00, 0x00, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x9a },
{ "beq", 0x00, 0x00, 0x9e, 0x00, 0x00, 0x00, 0x00, 0x9e },
{ "bmi", 0x00, 0x00, 0x96, 0x00, 0x00, 0x00, 0x00, 0x96 },
{ "bne", 0x00, 0x00, 0x9c, 0x00, 0x00, 0x00, 0x00, 0x9c },
{ "bpl", 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00, 0x94 },
{ "cib", 0xd7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "clc", 0xa2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "cmp", 0x00, 0xa6, 0xa4, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "dec", 0x6a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "eor", 0x00, 0x80, 0x7d, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "inc", 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "jmp", 0x00, 0xba, 0xb8, 0x00, 0x00, 0x00, 0x00, 0xb8 },
{ "jsr", 0x00, 0xc8, 0xbe, 0x00, 0x00, 0x00, 0x00, 0xbe },
{ "lda", 0x00, 0x08, 0x06, 0x00, 0x00, 0x0c, 0x00, 0x00 },
{ "ldb", 0x00, 0x14, 0x12, 0xd9, 0x25, 0x18, 0x1e, 0x00 },
{ "lsl", 0x85, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "lsr", 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "ora", 0x00, 0x78, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "pha", 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "pop", 0xb2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "rol", 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "ror", 0x91, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "rts", 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "sbc", 0x00, 0x62, 0x5f, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "sec", 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "sta", 0x00, 0x2c, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00 },
{ "stb", 0x00, 0x3b, 0x00, 0x36, 0x4c, 0x3f, 0x45, 0x00 },
{ "tab", 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ "tba", 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};
/*
* Function: print_syntax_error
* ------
* Prints a formatted error message for the user.
*
* message: Error message string
* token: Related artefact
* line_index: Line number in code (-1 when not given)
*/
void print_syntax_error (char *message, char *token, int line_index)
{
if (line_index > -1)
{
printf(
"Found syntax error in '%s' @ line %i (%s)!\n",
token,
line_index,
message
);
}
else
{
printf(
"Found syntax error in '%s' (%s)!\n",
token,
message
);
}
}
/*
* Function: remove_comments
* ------
* Removes all assembler-style comments (;) from a string
*
* str: The processed string
*/
void remove_comments (char *str)
{
int i;
for (i = 0; str[i] != 00; i++)
{
if (str[i] == ';')
{
str[i] = 00;
break;
}
}
}
/*
* Function: is_hex_str
* ------
* Checks if string holds only symbols describing a hexadecimal number
*
* str: The processed string
*
* returns: 1 if string represents a hexadecimal number, otherwise 0
*/
bool is_hex_str (char *str)
{
int i;
bool result = 1;
for (i = 0; str[i] != 00; i++)
{
if (!isxdigit(str[i]))
{
result = 0;
break;
}
}
return result;
}
/*
* Function: is_abs, is_idx, is_imm, is_idx_ind, is_ind, is_ind_idx
* ------
* Checks if address string uses an specific addressing mode
*
* token: The string to check against
*
* returns: 1 if string uses the adressing mode, otherwise 0
*/
bool is_abs (char *token)
{
if (
token[0] == '$' &&
token[strlen(token) - 2] != ','
)
{
return 1;
}
return 0;
}
bool is_idx (char *token)
{
if (
token[0] == '$' &&
token[strlen(token) - 2] == ',' &&
token[strlen(token) - 1] == 'a'
)
{
return 1;
}
return 0;
}
bool is_imm (char *token)
{
if (token[0] == '#')
{
return 1;
}
return 0;
}
bool is_idx_ind (char *token)
{
if (
token[0] == '(' &&
token[1] == '$' &&
token[strlen(token) - 3] == ',' &&
token[strlen(token) - 2] == 'a' &&
token[strlen(token) - 1] == ')'
)
{
return 1;
}
return 0;
}
bool is_ind (char *token)
{
if (
token[0] == '(' &&
token[1] == '$' &&
token[strlen(token) - 3] != ',' &&
token[strlen(token) - 2] != 'a' &&
token[strlen(token) - 1] == ')'
)
{
return 1;
}
return 0;
}
bool is_ind_idx (char *token)
{
if (
token[0] == '(' &&
token[1] == '$' &&
token[strlen(token) - 3] != ')' &&
token[strlen(token) - 2] != ',' &&
token[strlen(token) - 1] == 'a'
)
{
return 1;
}
return 0;
}
/*
* Function: is_valid_address
* ------
* Checks if token is a correct address mode
*
* opcode_index: Used opcode
* str: The processed string
*
* returns: 1 if string is a valid address, otherwise 0
*/
bool is_valid_address (int opcode_index, char *str)
{
if (
(OPCODES[opcode_index].a_abs && is_abs(str)) ||
(OPCODES[opcode_index].a_ind && is_ind(str)) ||
(OPCODES[opcode_index].a_imm && is_imm(str)) ||
(OPCODES[opcode_index].a_ind_idx && is_ind_idx(str)) ||
(OPCODES[opcode_index].a_idx_ind && is_idx_ind(str)) ||
(OPCODES[opcode_index].a_idx && is_idx(str))
)
{
return 1;
}
return 0;
}
/*
* Function: find_opcode_index
* ------
* Returns the index of the opcode lookup table
*
* mnemonic: The mnemonic of the opcode
*
* returns: index of opcode in lookup table
*/
int find_opcode_index (char *mnemonic)
{
int i;
int found_index = -1;
for (i = 0; i < OPCODE_COUNT; i++)
{
if (strncmp(OPCODES[i].mnemonic, mnemonic, 3) == 0)
{
found_index = i;
break;
}
}
return found_index;
}
/*
* Function: handle_opcode
* ------
* Adds the single opcode (1byte) microcode instruction to the program.
*
* program: The program
* index: Index of the current line in program
* op: Index of the opcode in lookup-table
*/
void handle_opcode (int *program, int index, int op)
{
program[index] = OPCODES[op].a;
program[index + 1] = 0x00;
}
/*
* Function: handle_opcode_and_operand
* ------
* Adds the opcode + operand (2byte) microcode instruction to the program.
*
* program: The program
* index: Index of the current line in program
* op: Index of the opcode in lookup-table
* addr: Operand / address
*/
void handle_opcode_and_operand (int *program, int index, int op, char *addr)
{
char* stripped_addr;
if (is_imm(addr))
{
// Immediate adressing mode
program[index] = OPCODES[op].a_imm;
stripped_addr = addr + 2;
}
else if (is_idx(addr))
{
// Indexed adressing mode
program[index] = OPCODES[op].a_idx;
stripped_addr = addr + 1;
stripped_addr[strlen(stripped_addr) - 2] = 0;
}
else if (is_abs(addr))
{
// Absolute adressing mode
program[index] = OPCODES[op].a_abs;
stripped_addr = addr + 1;
}
else if (is_ind(addr))
{
// Indirect adressing mode
program[index] = OPCODES[op].a_ind;
stripped_addr = addr + 2;
stripped_addr[strlen(stripped_addr) - 1] = 0;
}
else if (is_ind_idx(addr))
{
// Indirect indexed adressing mode
program[index] = OPCODES[op].a_ind_idx;
stripped_addr = addr + 2;
stripped_addr[strlen(stripped_addr) - 3] = 0;
}
else if (is_idx_ind(addr))
{
// Indexed indirect adressing mode
program[index] = OPCODES[op].a_idx_ind;
stripped_addr = addr + 2;
stripped_addr[strlen(stripped_addr) - 3] = 0;
}
if (!is_hex_str(stripped_addr))
{
print_syntax_error("Invalid address format", addr, 0);
exit(EXIT_FAILURE);
}
int hex_int = (int) strtol(stripped_addr, NULL, 16);
if (hex_int < 0 || hex_int > PROGRAM_MEMORY_SIZE - 1)
{
print_syntax_error("Invalid address range", addr, 0);
exit(EXIT_FAILURE);
}
program[index + 1] = hex_int;
}
/*
* Function: print_and_save_program
* ------
* Writes the compiled program to a file and
* prints the result to the screen.
*
* file: Write file
* program: The compiled program
* size: Length of the program (in bytes)
*/
void print_and_save_program (FILE *file, int *program, int size)
{
int i;
// Header for Logisim
fprintf(file, "v2.0 raw\n");
for (i = 0; i < size; i++)
{
fprintf(file, "%02x", program[i]);
printf("%02x", program[i]);
// Pretty print for screen
if (i % 16 == 15)
{
fprintf(file, "\n");
printf("\n");
}
else
{
fprintf(file, " ");
printf(" ");
}
}
}
/*
* Function: main
* ------
* Reads a 8bit cpu assembler file and writes a compiled executable
* for our architecture.
*
* Usage: ./assembler program.asm program.out
*/
int main (int argc, char **argv)
{
printf("===============================================\n");
printf(" 8bit cpu assembler v%s\n", VERSION);
printf("===============================================\n");
// Get input file
char *file_path = argv[1];
FILE *file;
file = fopen(file_path, "r");
if (file == 0)
{
printf("Error: Please specify a valid file path.\n");
exit(EXIT_FAILURE);
}
// Check output file
char *write_file_path = argv[2];
if (!write_file_path)
{
printf("Error: Please specify a valid out file path.\n");
exit(EXIT_FAILURE);
}
// Variables for stream reading
char line[MAX_LINE_LENGTH];
char *token = NULL;
// Variables for parsing and error output
int opcode_index = -1;
int current_line_index = 1;
// The compiled program variables
int program[PROGRAM_MEMORY_SIZE];
int program_adr = 0;
// Lookup table for labels
struct label label_table[MAX_LABEL_COUNT];
int label_table_count = 0;
// Lookup table for labelled jump instructions
struct label jump_table[MAX_JUMP_COUNT];
int jump_table_count = 0;
// Read file line by line
while (fgets(line, sizeof line, file) != NULL)
{
remove_comments(line);
token = strtok(line, "\n\t\r ");
while (token)
{
// Expects operand (address or label) of two-byte instruction
if (opcode_index > -1)
{
if (is_valid_address(opcode_index, token))
{
// Found an address
handle_opcode_and_operand(
program,
program_adr,
opcode_index,
token
);
}
else if (OPCODES[opcode_index].a_label)
{
// Found a label
if (jump_table_count > MAX_JUMP_COUNT)
{
print_syntax_error(
"Exceeded jump count",
token,
current_line_index
);
exit(EXIT_FAILURE);
}
else if (strlen(token) > MAX_LABEL_LENGTH)
{
print_syntax_error(
"Label name is too long",
token,
current_line_index
);
exit(EXIT_FAILURE);
}
// Write instruction to program with placeholder
program[program_adr] = OPCODES[opcode_index].a_label;
program[program_adr + 1] = 0x00;
// Store position for final processing
jump_table[jump_table_count].memory_addr = program_adr + 1;
strcpy(jump_table[jump_table_count].label, token);
jump_table_count += 1;
}
else
{
print_syntax_error(
"Invalid or missing operand",
token,
current_line_index
);
exit(EXIT_FAILURE);
}
opcode_index = -1;
program_adr += 2;
}
else
{
// Handle label or opcode
int op = find_opcode_index(token);
// Is token an opcode?
if (op > -1)
{
// Will an operand be expected in next token?
if (!OPCODES[op].a)
{
// Prepare two-byte instruction
opcode_index = op;
}
else
{
// Handle single byte instruction
handle_opcode(
program,
program_adr,
op
);
program_adr += 2;
}
}
else if (token[(strlen(token) - 1)] == ':')
{
// Token is a label, strip the last character (:)
token[strlen(token) - 1] = 0;
if (label_table_count > MAX_LABEL_COUNT)
{
print_syntax_error(
"Exceeded label count",
token,
current_line_index
);
exit(EXIT_FAILURE);
}
else if (strlen(token) > MAX_LABEL_LENGTH)
{
print_syntax_error(
"Label name is too long",
token,
current_line_index
);
exit(EXIT_FAILURE);
}
// Store it in table for later processing
label_table[label_table_count].memory_addr = program_adr;
strcpy(label_table[label_table_count].label, token);
label_table_count += 1;
}
else
{
print_syntax_error(
"Unkown opcode",
token,
current_line_index
);
exit(EXIT_FAILURE);
}
}
if (program_adr > PROGRAM_MEMORY_SIZE)
{
print_syntax_error(
"Program exceeds memory size",
token,
current_line_index
);
exit(EXIT_FAILURE);
}
token = strtok(NULL, "\n\t\r ");
}
current_line_index += 1;
}
fclose(file);
// Finally replace labels with memory addresses
int i, n;
bool found_label;
for (i = 0; i < jump_table_count; i++)
{
found_label = 0;
for (n = 0; n < label_table_count; n++)
{
if (strcmp(jump_table[i].label, label_table[n].label) == 0)
{
// Replace label with actual memory location in program
program[jump_table[i].memory_addr] = label_table[n].memory_addr;
found_label = 1;
break;
}
}
if (!found_label)
{
print_syntax_error("Could not find label", jump_table[i].label, -1);
exit(EXIT_FAILURE);
}
}
// Save binary to file and print result
printf(
"Successfully compiled program (%i bytes):\n\n",
program_adr
);
FILE * write_file = fopen(write_file_path, "w+");
print_and_save_program(write_file, program, program_adr);
fclose(write_file);
printf("\n\nWrite output to '%s'.\n", write_file_path);
return 0;
}