-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_parser.c
860 lines (679 loc) · 22.2 KB
/
file_parser.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
*Author:
*Author:
*This file contains code to parse contents of inout file(program.asm)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
#include <unistd.h>
#include "file_parser.h"
#include "tokenizer.h"
/*
* The structs below map a character to an integer.
* They are used in order to map a specific instruciton/register to its binary format in ASCII
*/
// Struct that stores registers and their respective binary reference
struct {
const char *name;
char *address;
} registerMap[] = {
{ "zero", "00000" },
{ "at", "00001" },
{ "v0", "00010" },
{ "v1", "00011" },
{ "a0", "00100" },
{ "a1", "00101" },
{ "a2", "00110" },
{ "a3", "00111" },
{ "t0", "01000" },
{ "t1", "01001" },
{ "t2", "01010" },
{ "t3", "01011" },
{ "t4", "01100" },
{ "t5", "01101" },
{ "t6", "01110" },
{ "t7", "01111" },
{ "s0", "10000" },
{ "s1", "10001" },
{ "s2", "10010" },
{ "s3", "10011" },
{ "s4", "10100" },
{ "s5", "10101" },
{ "s6", "10110" },
{ "s7", "10111" },
{ "t8", "11000" },
{ "t9", "11001" },
{ NULL, 0 } };
// Struct for R-Type instructions mapping for the 'function' field in the instruction
struct {
const char *name;
char *function;
} rMap[] = {
{ "add", "100000" },
{ "sub", "100001" },
{ "and", "100100" },
{ "or", "100101" },
{ "sll", "000000" },
{ "slt", "101010" },
{ "srl", "000010" },
{ "jr", "001000" },
{ NULL, 0 } };
// Struct for I-Type instructions
struct {
const char *name;
char *address;
} iMap[] = {
{ "lw", "100011" },
{ "sw", "101011" },
{ "andi", "001100" },
{ "ori", "001101" },
{ "lui", "001111" },
{ "beq", "000100" },
{ "bne", "001010" },
{ "addi", "001000" },
{ NULL, 0 } };
// Struct for J-Type instructions
struct {
const char *name;
char *address;
} jMap[] = {
{ "j", "000010" },
{ "jal", "000011" },
{ NULL, 0 } };
void parse_file(FILE *fptr, int pass, char *instructions[], size_t inst_len, hash_table_t *hash_table, FILE *Out) {
char line[MAX_LINE_LENGTH + 1];
char *tok_ptr, *ret, *token = NULL;
int32_t line_num = 1;
int32_t instruction_count = 0x00000000;
int data_reached = 0;
//FILE *fptr;
fptr = fopen(program.asm, "r");
if (fptr == NULL) {
fprintf(Out, "unable to open file %s. aborting ...\n", program.asm);
exit(-1);
}
while (1) {
if ((ret = fgets(line, MAX_LINE_LENGTH, fptr)) == NULL)
break;
line[MAX_LINE_LENGTH] = 0;
tok_ptr = line;
if (strlen(line) == MAX_LINE_LENGTH) {
fprintf(Out,
"line %d: line is too long. ignoring line ...\n", line_num);
line_num++;
continue;
}
/* parse the tokens within a line */
while (1) {
token = parse_token(tok_ptr, " \n\t$,", &tok_ptr, NULL);
/* blank line or comment begins here. go to the next line */
if (token == NULL || *token == '#') {
line_num++;
free(token);
break;
}
printf("token: %s\n", token);
/*
* If token is "la", increment by 8, otherwise if it exists in instructions[],
* increment by 4.
*/
int x = search(token);
//int x = (binarySearch(instructions, 0, inst_len, token));
if (x >= 0) {
if (strcmp(token, "la") == 0)
instruction_count = instruction_count + 8;
else
instruction_count = instruction_count + 4;
}
// If token is ".data", reset instruction to .data starting address
else if (strcmp(token, ".data") == 0) {
instruction_count = 0x00002000;
data_reached = 1;
}
// If first pass, then add labels to hash table
if (pass == 1) {
printf("First pass\n");
// if token has ':', then it is a label so add it to hash table
if (strstr(token, ":") && data_reached == 0) {
printf("Label\n");
// Strip out ':'
//printf("Label: %s at %d with address %d: \n", token, line_num, instruction_count);
size_t token_len = strlen(token);
token[token_len - 1] = '\0';
// Insert variable to hash table
uint32_t *inst_count;
inst_count = (uint32_t *)malloc(sizeof(uint32_t));
*inst_count = instruction_count;
int32_t insert = hash_insert(hash_table, token, strlen(token)+1, inst_count);
if (insert != 1) {
fprintf(Out, "Error inserting into hash table\n");
exit(1);
}
}
// If .data has been reached, increment instruction count accordingly
// and store to hash table
else {
char *var_tok = NULL;
char *var_tok_ptr = tok_ptr;
// If variable is .word
if (strstr(tok_ptr, ".word")) {
printf(".word\n");
// Variable is array
if (strstr(var_tok_ptr, ":")) {
printf("array\n");
// Store the number in var_tok and the occurance in var_tok_ptr
var_tok = parse_token(var_tok_ptr, ":", &var_tok_ptr, NULL);
// Convert char* to int
int freq = atoi(var_tok_ptr);
int num;
sscanf(var_tok, "%*s %d", &num);
// Increment instruction count by freq
instruction_count = instruction_count + (freq * 4);
// Strip out ':' from token
size_t token_len = strlen(token);
token[token_len - 1] = '\0';
//printf("Key: '%s', len: %zd\n", token, strlen(token));
// Insert variable to hash table
uint32_t *inst_count;
inst_count = (uint32_t *)malloc(sizeof(uint32_t));
*inst_count = instruction_count;
int32_t insert = hash_insert(hash_table, token, strlen(token)+1, inst_count);
if (insert == 0) {
fprintf(Out, "Error in hash table insertion\n");
exit(1);
}
printf("End array\n");
}
// Variable is a single variable
else {
instruction_count = instruction_count + 4;
// Strip out ':' from token
size_t token_len = strlen(token);
token[token_len - 1] = '\0';
// Insert variable to hash table
uint32_t *inst_count;
inst_count = (uint32_t *)malloc(sizeof(uint32_t));
*inst_count = instruction_count;
int32_t insert = hash_insert(hash_table, token, strlen(token)+1, inst_count);
if (insert == 0) {
fprintf(Out, "Error in hash table insertion\n");
exit(1);
}
printf("end singe var\n");
}
}
// Variable is a string
else if (strstr(tok_ptr, ".asciiz")) {
// Store the ascii in var_tok
var_tok_ptr+= 8;
var_tok = parse_token(var_tok_ptr, "\"", &var_tok_ptr, NULL);
// Increment instruction count by string length
size_t str_byte_len = strlen(var_tok);
instruction_count = instruction_count + str_byte_len;
// Strip out ':' from token
size_t token_len = strlen(token);
token[token_len - 1] = '\0';
// Insert variable to hash table
uint32_t *inst_count;
inst_count = (uint32_t *)malloc(sizeof(uint32_t));
*inst_count = instruction_count;
int32_t insert = hash_insert(hash_table, token, strlen(token)+1, inst_count);
if (insert == 0) {
fprintf(Out, "Error in hash table insertion\n");
exit(1);
}
}
}
}
// If second pass, then interpret
else if (pass == 2) {
printf("############ Pass 2 ##############\n");
// start interpreting here
// if j loop --> then instruction is: 000010 then immediate is insturction count in 26 bits??
// If in .text section
if (data_reached == 0) {
// Check instruction type
int instruction_supported = search(token);
char inst_type;
// If instruction is supported
if (instruction_supported != -1) {
// token contains the instruction
// tok_ptr points to the rest of the line
// Determine instruction type
inst_type = instruction_type(token);
if (inst_type == 'r') {
// R-Type with $rd, $rs, $rt format
if (strcmp(token, "add") == 0 || strcmp(token, "sub") == 0
|| strcmp(token, "and") == 0
|| strcmp(token, "or") == 0 || strcmp(token, "slt") == 0) {
// Parse the instructio - get rd, rs, rt registers
char *inst_ptr = tok_ptr;
char *reg = NULL;
// Create an array of char* that stores rd, rs, rt respectively
char **reg_store;
reg_store = malloc(3 * sizeof(char*));
if (reg_store == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
for (int i = 0; i < 3; i++) {
reg_store[i] = malloc(2 * sizeof(char));
if (reg_store[i] == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
}
// Keeps a reference to which register has been parsed for storage
int count = 0;
while (1) {
reg = parse_token(inst_ptr, " $,\n\t", &inst_ptr, NULL);
if (reg == NULL || *reg == '#') {
break;
}
strcpy(reg_store[count], reg);
count++;
free(reg);
}
// Send reg_store for output
// rd is in position 0, rs is in position 1 and rt is in position 2
rtype_instruction(token, reg_store[1], reg_store[2], reg_store[0], 0, Out);
// Dealloc reg_store
for (int i = 0; i < 3; i++) {
free(reg_store[i]);
}
free(reg_store);
}
// R-Type with $rd, $rs, shamt format
else if (strcmp(token, "sll") == 0 || strcmp(token, "srl") == 0) {
// Parse the instructio - get rd, rs, rt registers
char *inst_ptr = tok_ptr;
char *reg = NULL;
// Create an array of char* that stores rd, rs and shamt
char **reg_store;
reg_store = malloc(3 * sizeof(char*));
if (reg_store == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
for (int i = 0; i < 3; i++) {
reg_store[i] = malloc(2 * sizeof(char));
if (reg_store[i] == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
}
// Keeps a reference to which register has been parsed for storage
int count = 0;
while (1) {
reg = parse_token(inst_ptr, " $,\n\t", &inst_ptr, NULL);
if (reg == NULL || *reg == '#') {
break;
}
strcpy(reg_store[count], reg);
count++;
free(reg);
}
// Send reg_store for output
// rd is in position 0, rs is in position 1 and shamt is in position 2
rtype_instruction(token, "00000", reg_store[1], reg_store[0], atoi(reg_store[2]), Out);
// Dealloc reg_store
for (int i = 0; i < 3; i++) {
free(reg_store[i]);
}
free(reg_store);
}
else if (strcmp(token, "jr") == 0) {
// Parse the instruction - rs is in tok_ptr
char *inst_ptr = tok_ptr;
char *reg = NULL;
reg = parse_token(inst_ptr, " $,\n\t", &inst_ptr, NULL);
rtype_instruction(token, reg, "00000", "00000", 0, Out);
}
}
// I-Type
else if (inst_type == 'i') {
// la is pseudo instruction for lui and ori
// Convert to lui and ori and pass those instructions
if (strcmp(token, "la") == 0) {
// Parse the instruction - get register & immediate
char *inst_ptr = tok_ptr;
char *reg = NULL;
// Create an array of char* that stores rd, rs and shamt
char **reg_store;
reg_store = malloc(2 * sizeof(char*));
if (reg_store == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
for (int i = 0; i < 2; i++) {
reg_store[i] = malloc(2 * sizeof(char));
if (reg_store[i] == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
}
// Keeps a reference to which register has been parsed for storage
int count = 0;
while (1) {
reg = parse_token(inst_ptr, " $,\n\t", &inst_ptr, NULL);
if (reg == NULL || *reg == '#') {
break;
}
strcpy(reg_store[count], reg);
count++;
free(reg);
}
// Interpret la instruction.
// The register is at reg_store[0] and the variable is at reg_store[1]
// Find address of label in hash table
int *address = hash_find(hash_table, reg_store[1], strlen(reg_store[1])+1);
// Convert address to binary in char*
char addressBinary[33];
getBin(*address, addressBinary, 32);
// Get upper and lower bits of address
char upperBits[16];
char lowerBits[16];
for (int i = 0; i < 32; i++) {
if (i < 16)
lowerBits[i] = addressBinary[i];
else
upperBits[i-16] = addressBinary[i];
}
// Call the lui instruction with: lui $reg, upperBits
// Convert upperBits binary to int
int immediate = getDec(upperBits);
itype_instruction("lui", "00000", reg_store[0], immediate, Out);
// Call the ori instruction with: ori $reg, $reg, lowerBits
// Convert lowerBits binary to int
immediate = getDec(lowerBits);
itype_instruction("ori", reg_store[0], reg_store[0], immediate, Out);
// Dealloc reg_store
for (int i = 0; i < 2; i++) {
free(reg_store[i]);
}
free(reg_store);
}
// I-Type $rt, i($rs)
else if (strcmp(token, "lw") == 0 || strcmp(token, "sw") == 0) {
// Parse the instructio - rt, immediate and rs
char *inst_ptr = tok_ptr;
char *reg = NULL;
//
// Create an array of char* that stores rd, rs, rt respectively
char **reg_store;
reg_store = malloc(3 * sizeof(char*));
if (reg_store == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
for (int i = 0; i < 3; i++) {
reg_store[i] = malloc(2 * sizeof(char));
if (reg_store[i] == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
}
// Keeps a reference to which register has been parsed for storage
int count = 0;
while (1) {
reg = parse_token(inst_ptr, " $,\n\t()", &inst_ptr, NULL);
if (reg == NULL || *reg == '#') {
break;
}
strcpy(reg_store[count], reg);
count++;
free(reg);
}
// rt in position 0, immediate in position 1 and rs in position2
int immediate = atoi(reg_store[1]);
itype_instruction(token, reg_store[2], reg_store[0], immediate, Out);
// Dealloc reg_store
for (int i = 0; i < 3; i++) {
free(reg_store[i]);
}
free(reg_store);
}
// I-Type rt, rs, im
else if (strcmp(token, "andi") == 0 || strcmp( token, "ori") == 0
|| strcmp(token, "slti") == 0 || strcmp(token, "addi") == 0) {
// Parse the instruction - rt, rs, immediate
char *inst_ptr = tok_ptr;
char *reg = NULL;
// Create an array of char* that stores rt, rs
char **reg_store;
reg_store = malloc(3 * sizeof(char*));
if (reg_store == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
for (int i = 0; i < 3; i++) {
reg_store[i] = malloc(2 * sizeof(char));
if (reg_store[i] == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
}
// Keeps a reference to which register has been parsed for storage
int count = 0;
while (1) {
reg = parse_token(inst_ptr, " $,\n\t", &inst_ptr, NULL);
if (reg == NULL || *reg == '#') {
break;
}
strcpy(reg_store[count], reg);
count++;
free(reg);
}
// rt in position 0, rs in position 1 and immediate in position 2
int immediate = atoi(reg_store[2]);
itype_instruction(token, reg_store[1], reg_store[0], immediate, Out);
// Dealloc reg_store
for (int i = 0; i < 3; i++) {
free(reg_store[i]);
}
free(reg_store);
}
// I-Type $rt, immediate
else if (strcmp(token, "lui") == 0) {
// Parse the insturction, rt - immediate
char *inst_ptr = tok_ptr;
char *reg = NULL;
// Create an array of char* that stores rs, rt
char **reg_store;
reg_store = malloc(2 * sizeof(char*));
if (reg_store == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
for (int i = 0; i < 2; i++) {
reg_store[i] = malloc(2 * sizeof(char));
if (reg_store[i] == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
}
// Keeps a reference to which register has been parsed for storage
int count = 0;
while (1) {
reg = parse_token(inst_ptr, " $,\n\t", &inst_ptr, NULL);
if (reg == NULL || *reg == '#') {
break;
}
strcpy(reg_store[count], reg);
count++;
free(reg);
}
// rt in position 0, immediate in position 1
int immediate = atoi(reg_store[1]);
itype_instruction(token, "00000", reg_store[0], immediate, Out);
// Dealloc reg_store
for (int i = 0; i < 3; i++) {
free(reg_store[i]);
}
free(reg_store);
}
// I-Type $rs, $rt, label
else if (strcmp(token, "beq") == 0) {
// Parse the instruction - rs, rt
char *inst_ptr = tok_ptr;
char *reg = NULL;
// Create an array of char* that stores rs, rt
char **reg_store;
reg_store = malloc(2 * sizeof(char*));
if (reg_store == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
for (int i = 0; i < 2; i++) {
reg_store[i] = malloc(2 * sizeof(char));
if (reg_store[i] == NULL) {
fprintf(Out, "Out of memory\n");
exit(1);
}
}
// Keeps a reference to which register has been parsed for storage
int count = 0;
while (1) {
reg = parse_token(inst_ptr, " $,\n\t", &inst_ptr, NULL);
if (reg == NULL || *reg == '#') {
break;
}
strcpy(reg_store[count], reg);
count++;
free(reg);
if (count == 2)
break;
}
reg = parse_token(inst_ptr, " $,\n\t", &inst_ptr, NULL);
// Find hash address for a register and put in an immediate
int *address = hash_find(hash_table, reg, strlen(reg)+1);
int immediate = *address + instruction_count;
// Send instruction to itype function
itype_instruction(token, reg_store[0], reg_store[1], immediate, Out);
// Dealloc reg_store
for (int i = 0; i < 2; i++) {
free(reg_store[i]);
}
free(reg_store);
}
}
// J-Type
else if (inst_type == 'j') {
// Parse the instruction - get label
char *inst_ptr = tok_ptr;
// If comment, extract the label alone
char *comment = strchr(inst_ptr, '#');
if (comment != NULL) {
int str_len_count = 0;
for (int i = 0; i < strlen(inst_ptr); i++) {
if (inst_ptr[i] != ' ')
str_len_count++;
else
break;
}
char new_label[str_len_count+1];
for (int i = 0; i < str_len_count; i++)
new_label[i] = inst_ptr[i];
new_label[str_len_count] = '\0';
strcpy(inst_ptr, new_label);
}
else { printf("NO COMMENT\n");
inst_ptr[strlen(inst_ptr)-1] = '\0';
}
// Find hash address for a label and put in an immediate
int *address = hash_find(hash_table, inst_ptr, strlen(inst_ptr)+1);
// Send to jtype function
jtype_instruction(token, *address, Out);
}
}
if (strcmp(token, "nop") == 0) {
fprintf(Out, "00000000000000000000000000000000\n");
}
}
}
free(token);
}
}
}
// Determine Instruction Type
char instruction_type(char *instruction) {
if (strcmp(instruction, "add") == 0 || strcmp(instruction, "sub") == 0
|| strcmp(instruction, "and") == 0 || strcmp(instruction, "or")
== 0 || strcmp(instruction, "sll") == 0 || strcmp(instruction,
"slt") == 0 || strcmp(instruction, "srl") == 0 || strcmp(
instruction, "jr") == 0) {
return 'r';
}
else if (strcmp(instruction, "lw") == 0 || strcmp(instruction, "sw") == 0
|| strcmp(instruction, "andi") == 0 || strcmp(instruction, "ori")
== 0 || strcmp(instruction, "lui") == 0 || strcmp(instruction,
"beq") == 0 || strcmp(instruction, "slti") == 0 || strcmp(
instruction, "addi") == 0 || strcmp(instruction, "la") == 0) {
return 'i';
}
else if (strcmp(instruction, "j") == 0 || strcmp(instruction, "jal") == 0) {
return 'j';
}
return 0;
}
// Write out the R-Type instruction
void rtype_instruction(char *instruction, char *rs, char *rt, char *rd, int shamt, FILE *Out) {
// Set the instruction bits
char *opcode = "000000";
char *rdBin = "00000";
if (strcmp(rd, "00000") != 0)
rdBin = register_address(rd);
char *rsBin = "00000";
if (strcmp(rs, "00000") != 0)
rsBin = register_address(rs);
char *rtBin = "00000";
if (strcmp(rt, "00000") != 0)
rtBin = register_address(rt);
char *func = NULL;
char shamtBin[6];
}
// Print out the instruction to the file
fprintf(Out, "%s%s%s%s%s%s\n", opcode, rsBin, rtBin, rdBin, shamtBin, func);
}
// Write out the I-Type instruction
void itype_instruction(char *instruction, char *rs, char *rt, int immediateNum, FILE *Out) {
// Set the instruction bits
char *rsBin = "00000";
if (strcmp(rs, "00000") != 0)
rsBin = register_address(rs);
char *rtBin = "00000";
if (strcmp(rt, "00000") != 0)
rtBin = register_address(rt);
char *opcode = NULL;
char immediate[17];
size_t i;
for (i = 0; iMap[i].name != NULL; i++) {
if (strcmp(instruction, iMap[i].name) == 0) {
opcode = iMap[i].address;
}
}
// Convert immediate to binary
getBin(immediateNum, immediate, 16);
// Print out the instruction to the file
fprintf(Out, "%s%s%s%s\n", opcode, rsBin, rtBin, immediate);
}
// Write out the J-Type instruction
void jtype_instruction(char *instruction, int immediate, FILE *Out) {
// Set the instruction bits
char *opcode = NULL;
// Get opcode bits
size_t i;
for (i = 0; jMap[i].name != NULL; i++) {
if (strcmp(instruction, jMap[i].name) == 0) {
opcode = jMap[i].address;
}
}
// Convert immediate to binary
char immediateStr[27];
getBin(immediate, immediateStr, 26);
// Print out instruction to file
fprintf(Out, "%s%s\n", opcode, immediateStr);
}