-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assembler.c
764 lines (573 loc) · 19.5 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
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
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "Assembler.h"
#include "Checkers.h"
#include "Insertion.h"
Symbol ** symbol_table; /*The symbols table*/
Instruction ** instructions_table; /* for data and instruction order*/
char ** ErrorsAssembler; /*Error in the compiling*/
int * data_table; /*Int dynamic array to store all the data instructions*/
EntrySy ** EntSymbolsTable; /*table that save all the indexes of the entrys labels*/
ExternSy ** ExtSymbolsTable; /*table that save all the indexes of the externs labels*/
unsigned SymbolEntCount; /*entry symbols counter*/
unsigned SymbolExtCount; /*extern symbols counter*/
unsigned IC; /*Instruction table counter*/
unsigned Total_IC; /*total of Instructions after the first iteration*/
unsigned DC; /*Data table counter*/
unsigned SC; /*Symbol counter*/
unsigned EC; /*Error counter*/
unsigned LC; /*Line counter*/
FILE * fp; /*FILE pointer to the given assembly file*/
/*Prototypes*/
void FirstCheckingCommand(char **);
void SecondCheckingCommand(char ** command);
/*function that checks if the new memory allocate was successed, if not the function will print to stderr a new error message and then will exit the program*/
void allocate_check(void * p)
{
if(!p)
{
fprintf(stderr,"Error to allocate new memory\n");
exit(0);
}
}
/*private function that return the number of digits for the given number*/
int lenOfNum(int n)
{
int ans;
ans = 1;
while ((n = (n / 10)) >= 1)
ans++;
return ans;
}
/*fucntion that insert new assembler error into ErrorsAssembler table */
void insertNewError(char * error)
{
if (EC==0)
{
ErrorsAssembler = (char **)malloc(sizeof(char *));
allocate_check((char **)ErrorsAssembler);
}
else
{
char ** temp;
temp = (char **)realloc(ErrorsAssembler, (EC + 1) * sizeof(char *));
allocate_check(temp);
ErrorsAssembler = temp;
}
ErrorsAssembler[EC] = (char *)malloc(strlen(error) + lenOfNum(LC) + 1);
sprintf(ErrorsAssembler[EC], error, LC); /*( puts string into buffer */
ErrorsAssembler[EC][strlen(error) + lenOfNum(LC)] = '\0';
EC++;
}
/*get the whole command and transfer it to linked list, if returned ans>0 then there is an error in the input otherwise the input is valid*/
void CommandLineToLinkedList(int NumIteration)
{
char ** command; /*dynamic matrix of strings*/
char reader; /*char variable to iterate on content std*/
int chars_len, word_counter, isComa, isQuot, ignore,i; /*chars_len: the char length of the current word; word_counter: indicate in the current number of word; isComa: indicate if coma was encourted more than one time ; isQueat: indicate if quotation marks apeared for ignore the creation of new string */
LC++;
ignore = 0;
word_counter = 0;
chars_len = 1;
isComa = 0;
isQuot = 0;
command = (char **)malloc(sizeof(char *));
allocate_check(command); /*-------------Need to check if (char **)commands is valid------------*/
command[0] = (char *)calloc(1, sizeof(char));
allocate_check(command[0]);
Loop: while (((reader = fgetc(fp)) != EOF) && (reader != '\n'))
{
if ((reader == ';') && (!word_counter) && (chars_len == 1))
ignore = 1;
if ((!ignore) && ((!isspace(reader)) && ((reader != ',') || (isComa))))
{
isQuot = (reader == '"') ? isQuot ^ 1 : isQuot;
command[word_counter] = (char *)realloc((command[word_counter]), (chars_len + 1) * sizeof(char));
allocate_check(command[word_counter]);
command[word_counter][chars_len - 1] = reader;
command[word_counter][chars_len] = '\0';
chars_len++;
isComa = 0;
}
else
{
if ((!isQuot) && (chars_len>1))
{
word_counter++;
command = (char **)realloc(command, (word_counter + 1) * sizeof(char *));
allocate_check(command);
command[word_counter] = (char *)calloc(1, sizeof(char));
allocate_check(command[word_counter]);
chars_len = 1;
isComa = (reader == ',');
}
}
}
if (ignore)
{
LC++;
ignore = 0;
goto Loop;
}
/*Assign in the last+1 place a null (to indicate the end of the current command) */
if (chars_len>1)
{
char ** tempT;
word_counter++;
tempT = (char **)realloc(command, (word_counter + 1) * sizeof(char *));
allocate_check(command);
command = tempT;
}
command[word_counter] = NULL;
if (reader == '\n')
{
if (NumIteration == 1)
FirstCheckingCommand(command);
else
SecondCheckingCommand(command);
/*clean the command array*/
i=0;
while (i<=word_counter)
{
free(command[i]);
i++;
}
free(command);
CommandLineToLinkedList(NumIteration);
}
else /*if c is EOF*/
{
if (NumIteration == 1)
FirstCheckingCommand(command);
else
{
unsigned counterEntry;
counterEntry=0;
SecondCheckingCommand(command);
while ((!EC)&&(counterEntry<SymbolEntCount))
{
unsigned index;
index = EntSymbolsTable[counterEntry]->index;
symbol_table[index]->type = ENTRY;
counterEntry++;
}
}
/*clean the command array*/
i=0;
while (i<=word_counter)
{
free(command[i]);
i++;
}
free(command);
}
}
/*checking and processing the current command line*/
void FirstCheckingCommand(char ** command)
{
int flag_symbol_type;
/*if the given string list is null/empty */
if (!(*command))
return;
/*In the case that the first string on the current command line is a label(symbol) */
if ((isValidLabel((command[0]), 1)) && (((flag_symbol_type = isInstruction(command[1], 1)) >= 0)))
{
/*if the instrct type is an data or extern*/
if (((flag_symbol_type >= DATA) && (flag_symbol_type <= MAT)) || (flag_symbol_type == EXTERN))
{
if(flag_symbol_type != EXTERN)
{
insertSymbolToTable(command[0], flag_symbol_type);
insertToDT(&command[2], flag_symbol_type);
}
else
insertSymbolToTable(command[2], EXTERN);
}
/*if the instrct type is an instruction*/
else if (flag_symbol_type <= 15)
{
insertSymbolToTable(command[0], flag_symbol_type);
insertToIT(&command[2], flag_symbol_type); /*the command[2] is first operand*/
}
}
else /*if the commands[0] isn't label*/
{
if ((flag_symbol_type = isInstruction(command[0], 0)) >= 0)
{
if ((flag_symbol_type >= DATA) && (flag_symbol_type <= MAT))
{
insertToDT(&command[1], flag_symbol_type);
return;
}
if (flag_symbol_type >= 19)
{
if (flag_symbol_type == 20) /*if is .extern insruct type then we will enter the command into the symbol table*/
insertSymbolToTable(command[1], flag_symbol_type);
}
else
{
insertToIT(&command[1], flag_symbol_type); /*the command[1] is first operand*/
}
}
else if (!isValidLabel((command[0]), 1))
insertNewError("Unidentified command line: %d");
}
}
/*The Secound check of the given command line*/
void SecondCheckingCommand(char ** command)
{
int flag_symbol_type;
int flag; /*if there is a label(symbol) in the current given command line*/
/*if the given string list is null/empty */
if (!(*command))
return;
flag = 0;
if (isValidLabel(command[0], 1))
{
/*In the case that the first string on the current command line is a label(symbol) */
flag = 1;
}
if ((flag_symbol_type = isInstruction(command[flag], 1)) == 19)
{
/*In the case that the second string is .entry*/
unsigned index;
index = findSymbol(command[flag + 1]);
if (index == -1)
insertNewError("The entry symbol defining isn't valid: %d");
else
{
if(SymbolEntCount==0)
{
EntSymbolsTable=(EntrySy **)malloc(sizeof(EntrySy*));
allocate_check(EntSymbolsTable);
}
else
{
EntSymbolsTable=(EntrySy **)realloc(EntSymbolsTable,sizeof(EntrySy**)*(SymbolEntCount+1));
allocate_check(EntSymbolsTable);
}
EntSymbolsTable[SymbolEntCount]=(EntrySy *)malloc(sizeof(EntrySy));
allocate_check(EntSymbolsTable[SymbolEntCount]);
EntSymbolsTable[SymbolEntCount]->index = index;
EntSymbolsTable[SymbolEntCount]->type = symbol_table[index]->type;
SymbolEntCount++;
}
}
else if (flag_symbol_type <= 15)
{
updateInstruction(&command[flag + 1], flag_symbol_type); /*the command[2] is first operand*/
}
}
/*private function that calculate the given integer number to the special base 4 number*/
char * base4 (int i, int j)
{
int res,k,f,length;
char * temp;
char * temp1;
char * ans ;
k = 0;
if (i == 0){
ans = (char *)calloc(j+1,sizeof(char));
allocate_check(ans);
ans[0] = 'a';
for(f = 1; f < j; f++)
ans[f]='a';
ans[f] = '\0';
return ans;
}
ans = (char *)calloc(k+1,sizeof(char));
allocate_check(ans);
while (i > 0){
res = i%4;
i=i/4;
if(res == 0){ans[k++] = 'a';}
else if(res == 1){ans[k++] = 'b';}
else if(res == 2){ans[k++] = 'c';}
else if(res == 3){ans[k++] = 'd';}
temp = (char*)realloc(ans,(k+1));
allocate_check(ans);
ans = temp;
}
temp = (char*)realloc(ans,(k+1));
allocate_check(temp);
ans = temp;
temp = (char *)calloc(k+1,sizeof(char));
allocate_check(temp);
for(f = 0; f < k; f++)
temp[f] = ans[(k-1)-f];
temp[k]='\0';
free(ans);
ans = temp;
length = strlen(ans);
if (j > length)
{
temp1 = (char *)calloc(j+1,sizeof(char));
for (f = 0; f < j; f++)
{
if (f <(j-length))
temp1[f] = 'a';
else
temp1[f] = temp[f-(j-length)];
}
free(ans);
return temp1;
}
return ans;
}
/*private function that print all the instructions from the instructions table into .ob file*/
void printInstructionsToFile(FILE *output)
{
int i;
char *temp;
char *addr;
i = 0;
while (i < IC)
{
char ans[6] = "";
addr = base4(i+100,4);
if ((instructions_table[i]->type_order) == 0)
{
temp = base4(((InstructOrder *)instructions_table[i]->order)->opcode,2);
strcat(ans, temp);
free(temp);
temp = base4(((InstructOrder *)instructions_table[i]->order)->origin_addressing,1);
strcat(ans, temp);
free(temp);
temp = base4(((InstructOrder *)instructions_table[i]->order)->dest_addressing,1);
strcat(ans, temp);
free(temp);
temp = base4(((InstructOrder *)instructions_table[i]->order)->type_coding,1);
strcat(ans, temp);
free(temp);
}
if ((instructions_table[i]->type_order) == 1)
{
temp = base4((((InstructData*)instructions_table[i]->order)->value)+100,4);
strcat(ans, temp);
free(temp);
temp = base4(((InstructData*)instructions_table[i]->order)->type_coding,1);
strcat(ans, temp);
free(temp);
}
if ((instructions_table[i]->type_order) == 2)
{
temp = base4(((InstructRegisters*)instructions_table[i]->order)->reg1,2);
strcat(ans, temp);
free(temp);
temp = base4(((InstructRegisters*)instructions_table[i]->order)->reg2,2);
strcat(ans, temp);
free(temp);
strcat(ans, "a");
}
fprintf(output, "%s %s\n", addr, ans);
free(addr);
i++;
}
}
/*private function that print all the instructions data from the data table into .ob file*/
void printDataToFile(FILE *output)
{
int i;
char *ans;
char *addr;
i = 100+IC;
while (i < DC+IC+100)
{
addr = base4(i,4);
ans = base4(data_table[i-100-IC],5);
fprintf(output, "%s %s\n", addr, ans);
free(addr);
free(ans);
i++;
}
}
/*private function that free all the global variables and structs*/
void cleanAllmem()
{
int i;
i=0;
if(SC>0)
{
while(i<SC)
{
free(symbol_table[i]->label_name);
free(symbol_table[i]);
i++;
}
free(symbol_table);
i=0;
}
if(IC>0)
{
while(i<IC)
{
free(instructions_table[i]->order);
free(instructions_table[i]);
i++;
}
free(instructions_table);
i=0;
}
if(EC>0)
{
while(i<EC)
{
free(ErrorsAssembler[i]);
i++;
}
free(ErrorsAssembler);
}
if(DC>0)
free(data_table);
SymbolEntCount=0;
SymbolExtCount=0;
IC=0;
SC=0;
EC=0;
LC=0;
DC=0;
Total_IC=0;
}
int main(int argc,char ** argv) {
int argCounter;
FILE * objectFile;
FILE * entryFile;
FILE * externFile;
cleanAllmem();
argCounter=1;
if(argc<2)
fprintf(stderr,"No entered .as file name\n");
while(argCounter<argc)
{
char *asName;
char *objectName;
char *entryName ;
char *externName;
asName=(char *)calloc(strlen(argv[argCounter])+4,sizeof(char));
allocate_check(asName);
strcpy(asName,argv[argCounter]);
strcat(asName,".as");
fp = fopen (asName, "r");
if(!fp)
{
printf("The file name \"%s\" -isn't valid or not found\n",argv[argc]);
argCounter++;
free(asName);
continue;
}
free(asName);
/*First checking of the assembly*/
CommandLineToLinkedList(1);
/*sets the file position to the beginning of the assembly file*/
rewind(fp);
if (EC>0)
{
/*Print all the compile error from ErrorsAssembler and exit*/
int i;
i=0;
while(i<EC)
{
printf("%s\n",ErrorsAssembler[i]);
i++;
}
goto Final;
}
/*Second checking of the assembly*/
Total_IC=IC;
IC=0;
LC=0;
CommandLineToLinkedList(2);
if (EC>0)
{
/*Print all the compile error from ErrorsAssembler and exit*/
int i;
i=0;
while(i<EC)
{
printf("%s\n",ErrorsAssembler[i]);
i++;
}
goto Final;
}
fclose(fp);
objectName=(char *)calloc(strlen(argv[argCounter])+4,sizeof(char));
strcpy(objectName,argv[argCounter]);
strcat(objectName,".ob");
objectFile = fopen(objectName, "w");
if(!fp)
{
fprintf(stderr,"Can't write the object file\n");
exit(1);
}
printInstructionsToFile(objectFile);
printDataToFile(objectFile);
fclose(objectFile);
free(objectName);
if(SymbolEntCount>0)
{
unsigned i,index;
char *addr;
char *label;
i=0;
entryName=(char *)calloc(strlen(argv[argCounter])+5,sizeof(char));
strcpy(entryName,argv[argCounter]);
strcat(entryName,".ent");
entryFile=fopen(entryName,"w");
if(!entryFile)
{
fprintf(stderr,"Can't write the entry file\n");
exit(1);
}
while(i<SymbolEntCount)
{
index=EntSymbolsTable[i]->index;
label=symbol_table[index]->label_name;
if(((EntSymbolsTable[i]->type)>=16)&&((EntSymbolsTable[i]->type)<=18))
addr=base4((symbol_table[index]->dec_value)+100+IC,4);
else
addr=base4((symbol_table[index]->dec_value)+100,4);
fprintf(entryFile,"%s %s\n",label,addr);
free(addr);
free(EntSymbolsTable[i]);
i++;
}
free(EntSymbolsTable);
fclose(entryFile);
free(entryName);
}
if(SymbolExtCount>0)
{
int i;
char *addr;
char *label;
externName=(char *)calloc(strlen(argv[argCounter])+5,sizeof(char));
strcpy(externName,argv[argCounter]);
strcat(externName,".ext");
i=0;
externFile=fopen(externName,"w");
if(!externFile)
{
fprintf(stderr,"Can't write the extern file\n");
exit(1);
}
while(i<SymbolExtCount)
{
label=ExtSymbolsTable[i]->label_name;
addr=base4((ExtSymbolsTable[i])->addr+100,4);
fprintf(externFile,"%s %s\n",label,addr);
free(label);
free(addr);
free(ExtSymbolsTable[i]);
i++;
}
free(ExtSymbolsTable);
fclose(externFile);
free(externName);
}
Final:cleanAllmem();
argCounter++;
}
return 0;
}