-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
759 lines (553 loc) · 15.7 KB
/
main.c
File metadata and controls
759 lines (553 loc) · 15.7 KB
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
// File Name: main.c
// File Type: source file
// main.c - performs the first scan according to the algorithm
#include "header.h"
FILE *ext;
char *ex;
char *register_table[]={
"r0","r1","r2","r3","r4","r5","r6","r7"
,"PSW","PC","SP","carry","zero","not_a_register_word"};
int dc,ic,ERROR;
char label_name[LABEL_LENGTH];
int ind=0,
chck_lbl, //used to check labels
command_type, //command type
tmp,data_zone[DATA_ZONE],code_zone[DATA_ZONE],
temp_data[80]={0}, //holds temp lines
end_ic;
int line_address[MAXLINES], //holds the lines address
flag[MAXLINES], //indicates when there's a label in a line
good_line[MAXLINES];
int there_is_extern=no,there_is_entry=no;
char colon;
char *ext_file;
struct commands {
char *code;
char *command;
int num_of_operands;
int L; //how many words are needed for the command
} command_list[]={{"00","mov",2,-2},{"01","cmp",2,-2},{"02","add",2,-2},
{"03","sub",2,-2},{"04","div",2,-2},{"05","mod",2,-2},
{"06","lea",2,-2},{"07","inc",1,-1},
{"10","dec",1,-1},{"11","jmp",1,-1},{"12","bne",1,-1},
{"13","get",2,-2},{"14","prn",1,-1},{"15","jsr",1,-1},
{"16","rts",0,1},{"17","hlt",0,1},{"20",'\0',0,0}};
struct label *root;
// function main gets the parameters from the command line.
// it calls for the first scan, then it rewinds the source file
// and calls the second scan function.
int main(int argc,char *argv[])
{
FILE *ifp;
char filename[MAX_NAME];
char *suffix=".as";
if (argc==1)
return !OK;
while (--argc>0)
{
strcpy(filename,*++argv); //copy the file name to the array filename
strcat(filename,suffix); //adds suffix .as to the filename
if ((ifp=fopen(filename,"r"))==NULL)
printf("Could not open file %s\n",*argv);
else
{
printf("\nAssembling %s ...",filename);
ex=filename;
there_is_extern=no;
ERROR=no;
first_scan(ifp);
rewind(ifp);
second_scan(ifp);
build_entries_file(filename);
if (ext!=NULL)
fclose(ext);
if (!ERROR)
build_object_file(filename);
fclose(ifp);
}
}
printf("\n");
return OK;
}
// function first_scan gets the original source file and performs
// the first scan according to the algorithm we were given (page #28)
void first_scan(FILE*ifp)
{
char* found_label, //holds the label name
line[LINE_LENGTH]; //holds input line
int label, //checks if the label is in the line
answer_type=no,
line_counter=0; //counts the lines
int num_of_data,found,
legal_string; //will help to check if the string is legal
root=NULL;
ic=0;dc=0; //setting ic,dc to 0 - line #1 in algorithm
//ic=instruction counter; dc=data counter
while (fgets(line,LINE_LENGTH,ifp)!=NULL)
//read a line - line #2 in algorithm
{
line_counter++;
good_line[line_counter]=yes;
flag[line_counter]=no;
tmp=0;
num_of_data=legal_string=yes;
chck_lbl=no;
found=no;
label=no;
ind=0;
if (strchr(line,'\n')!=NULL) //if '\n' appears in line
*(strchr(line,'\n'))='\0';
if (line[0]=='\0')
{
line_address[line_counter]=bad;
continue;
}
/* check if there are empty spaces or mark in the line */
if (line[0] == ';')
//if the line begins with a ';' it is a remark line
{
line_address[line_counter]=bad;
continue; /* remark line */
}
while (isspace(line[ind]) && line[ind+1]!='\0')//skip spaces
ind++;
if (line[ind+1]=='\0')
{
line_address[line_counter]=bad;
continue; /*all line is space */
}
if (line[ind]==';' && ind>0)
//the remark begins somewhere in the middle of the code line
{
good_line[line_counter]=nftfc;
continue;
}
if (ind==0)
//checking if the first field is a flag - line #3 in algorithm
{
colon=':';
label=is_legal_label(line);
//checks if the first string is a legal label
if (label==yes) //if label!=yes then skip to line #5 in algorithm
{
tmp=0;
found_label=exclude_label_from_line(line);
if(is_a_command_or_a_register(found_label))
{
label=reserverd_word;
//illegal label - label is a reserved word
}
else
{
while (isspace(line[ind]) && line[ind+1]!='\0')
{
if (line[ind+1]=='\0')
break;
// there is label without any continue
ind++;
}
flag[line_counter]=yes;
// found a legal label- line #4 in algorithm
}
}
}
command_type=has_data_or_string(line);
//checking if this is a virtual instruction - .data or .string
// line #5 in algorithm
if (command_type==data || command_type==string)
//fifth line in algorithm
//if command_type!=data || command_type!=string -> then skip to line #8
{
chck_lbl=no;
line_address[line_counter]=dc;
if(command_type==data)
{
if (flag[line_counter]==yes)
//insert label with dc address
root=add_label_to_label_table(root,found_label,dc,"data");
num_of_data= is_legal_data(line);
if (num_of_data)
store_data(num_of_data);
else
good_line[line_counter]=bad_numbers;
}
else //command_type==string
{
if (flag[line_counter]==yes)
//if there's a symbol - line #6 in algorithm
root=add_label_to_label_table(root,found_label,dc,"data");
// add the label to table with dc value
legal_string=is_a_legal_string(line);
//checking if the .string is legal
if (legal_string==yes)
save_str(line);
else
good_line[line_counter]=legal_string;
}
if(chck_lbl)
label=chck_lbl;
if(label>yes)
good_line[line_counter]=label;
continue; //back to line #2 in algorithm
}
answer_type = check_if_extrn_or_entry(line); //line #8 in algorithm
if (answer_type==extrn || answer_type==entry)
// if (answer_type!=extrn || answer_type!=entry)->then skip to line #11 in the algorithm.
{
line_address[line_counter]=bad;
chck_lbl=no;
while (!isspace(line[ind]))
ind++;
while (isspace(line[ind])) //skip declarations
ind++;
tmp=ind;
colon='\0';
label=is_legal_label(line);
if (label==yes)
{
found_label=exclude_label_from_line(line);
if(is_a_command_or_a_register(found_label))
label=reserverd_word;
else
{
if (answer_type==extrn) //line #9 in algorithm
{
root=add_label_to_label_table(root,label_name,0,"extr");
//add the label to the label table.
}
}
}
if (good_line[line_counter]==yes)
good_line[line_counter]=label;
if (chck_lbl)
label=chck_lbl;
if (label>yes)
good_line[line_counter]=label;
continue; //back to line #2 in algorithm.(line #10 in algorithm)
}
chck_lbl=no;
if (flag[line_counter]==yes)
root=add_label_to_label_table(root,found_label,ic,"code");
found=find_command(line,line_counter); //line # 12 in algorithm.
if (found!=bad)//it's a legal command
{
line_address[line_counter]=ic;
if(command_list[found].L==1)
ic=ic+1;
else
ic=ic+how_many_lines_to_save(line);
//line #13 in algorithm.
}
else //found=bad.
{
line_address[line_counter]=bad;
}
if (chck_lbl)
label=chck_lbl;
if (label>yes)
good_line[line_counter]=label;
if (found==bad && label<=yes)
good_line[line_counter]=Illegel;
}
end_ic=ic;
}
// function build_new_file_name get the source file name and
// creates a new file using the suffix provided as an argument
char *build_new_file_name(char *source_file,char file_name[])
{
char *dot; //points to the dot in file name
char *p;
p=(char *) malloc(strlen(source_file)+strlen(file_name)-2);
//allocate memory for new file name.
//size of length of source name, minus 2 - the suffix
strncpy(p,source_file,strlen(source_file));
//copy to p the prefix name of source file
dot=strchr(p,'.');
//find where the '.' is in the name and advance it by 1
++dot;
strcpy(dot,file_name); //add the suffix argument after the '.'
return p;
}
// function is_legal_label checks if the label is legal-
// a legal label can begin with an alphanumeric char followed by
// letters or digits and ends with ':'
// The label must begin in first column of the line.
// Label length can not exceed 30 chars
int is_legal_label(char *line)
{
int tmp_ind=1;
if (strchr(line,colon)==NULL)
return no;
if (isalpha(line [ind])) //if first char in line is alphanumeric
{
while (isalnum(line[ind]))
//while rest of chars are alphanumric or digits
{
if (tmp_ind>LABEL_LENGTH) return label_too_long;
//length of label can not exceed 30 chars - illegal label
ind++;
tmp_ind++;
if (line[ind]==colon)return yes;
}
return Illegal_chars_in_label;
//the chars are not alphanumeric-illegal label
}
else return illegal_first_char;
//illegal label - first char is not alphanumeric
}
// function exclude_label_from_line - excludes the label in line and
// returns the label
char *exclude_label_from_line(char *line)
{
int tmp_ind=0;
ind=tmp;
while (line[ind]!=colon)
{ // while we have not reached the end of label
label_name[tmp_ind]=line[ind];
ind++;
tmp_ind++;
}
label_name[tmp_ind]='\0';
return label_name;
}
// function add_lable gets the labels_table, the label we want to add
// to the table,it's name,type and address and returns the update table.
// If the new label is an extrenal one - it calls for the extrenal label
// file builder function.
struct label* add_label_to_label_table(struct label*p,char *lbl_name,int
address,char* type)
{
if (p==NULL)
{
p=(struct label*) malloc (sizeof(struct label));
p->next=NULL;
strcpy(p->label_name,lbl_name);
p->address=address;
p->entry=no;
strcpy(p->type,type);
if (!(strcmp(type,"extr"))) //if this label is an external one
{
if (!there_is_extern)
//if this is the first external label in file - create ext file
{
there_is_extern=yes;
ext_file=build_new_file_name(ex,"ext");
//found an external label-create ext file
if ((ext=fopen(ext_file,"w"))==NULL)
printf("Can not open file %s.Openning the next file.\n\n",ext_file);
}
}
}
else if (strcmp(p->label_name,lbl_name))
p->next=add_label_to_label_table(p->next,lbl_name,address,type);
else
{
if (!(strcmp(p->type,type)))
chck_lbl=label_already_exists;
else
chck_lbl=different_type;
}
return p;
}
//function is_a_command_or_a_register checkes if the string is
// a reserved command or a register
int is_a_command_or_a_register(char *found_label)
{
int list=0;
while ((command_list[list].command)!='\0')
{
if (strcmp(found_label,(command_list[list].command))==0)
return yes;
else
list++;
}
list=0; //the label is not a command. Checking if it's a register
while (strcmp(register_table[list],"not_a_register_word"))
{
if (0==(strcmp(found_label,register_table[list])))
return yes;
else
list++;
}
return no;
}
//function check_if_extrn_or_entry returns the type of the command
// - extern or entry or no
int check_if_extrn_or_entry(char* line)
{
if (strstr(line,".entry")!=NULL)
return entry;
if (strstr(line,".extern")!=NULL)
return extrn;
else return no;
}
// function has_data_or_string checks if current line has '.data'
// or '.string' in it.
int has_data_or_string(char*line)
{
if (strstr(line,".data")!=NULL)
return data;
if (strstr(line,".string")!=NULL)
return string;
return 0;
}
// function is_legal_data checks parameters on (numbers),
// in ".data" line. if one is illegal no element is saved
int is_legal_data(char*line)
{
int mone=0,i=0,j,t,terms=0;
char str[]=".data",data_par[LINE_LENGTH],*token,seps[]=",",*cll;
strcpy(data_par,line);
ind=strstr (data_par,str)-data_par+5;
while(isspace(data_par[ind]) && data_par[ind]!='\0')
// check if rest of line is empty
ind++;
if (line[ind]=='\0')
return no;
j=strlen(line);
for (t=ind;t<j;t++) if (data_par[t]==',')
mone++;
cll=strstr(data_par,".data");
for (t=1;t<=5;cll++,++t);
token=(strtok(cll,seps));
while (token!=NULL)
{
if (is_legal_number(token))
{
temp_data[i]=atoi(token);
terms++;
}
else return no;
i++;
token=strtok(NULL,seps);
}
if (mone+1==terms)
return i;
return no;
}
// function is_legal_number checks if the number is legal
// (with or without +/-)
int is_legal_number(char p[])
{
int n=0,k,l=yes;
while (isspace(p[n]))
n++;
if (isdigit(p[n])|| p[n]=='+' || p[n]=='-')
{
k=(strlen(p));
n++;
while (n<k && isdigit(p[n]))
n++;
while (n<k)
{
if (isspace(p[n]))
n++;
else return no;
}
}
else return no;
return l;
}
//function store_data stores data in the data zone /
void store_data(int num)
{
int i;
for(i=0;i<num;i++,dc++)
data_zone[dc]=temp_data[i];
}
//function how many lines should be saved
//for the code according to the parameters in the current line of code
int how_many_lines_to_save(char*line)
{
int ind1,ind2=0,
total=1;
char operand[LINE_LENGTH];
for(ind2=0,ind1=0;ind2<2;ind2++)
{
ind1=0;
while(isspace(line[ind]))
ind++;
if (line[ind]=='@'||line[ind]=='#'||line[ind]=='*')
ind++;
while(isspace(line[ind]))
ind++;
while((line[ind]!=','))
{
if(line[ind]==' '||line[ind]=='\0')
break;
operand[ind1]=line[ind];
ind1++;
ind++;
}
operand[ind1]='\0';
if (!(is_a_command_or_a_register(operand)))
total++;
while(isspace(line[ind]))
ind++;
if(line[ind]==',')
ind++;
else
return total;
}
return total;
}
// function is_a_legal_string checks if the string is a legal one
int is_a_legal_string(char*line)
{
int ind1;
char quotes='"';
char str[]=".string";
ind=strstr (line,str)-line+7;
while (line[ind]!=quotes)
{
if (isspace(line[ind]))
ind++;
else
return no_begin;
}
if (strchr(line,quotes)==strrchr(line,quotes))
return no_end;
ind1=strrchr(line,quotes)-line;
while (isspace(line[ind1]))
ind1++;
if (line[ind1+1]=='\0')
return yes;
else
return no_end;
}
// function save_str saves chars from the string into the data zone
void save_str(char*line)
{
int current,last_char;
current =strchr (line,'"')-line; //the first char
last_char= strrchr(line,'"')-line; //the last char
while (++current !=last_char)
{
data_zone[dc]= line[current];
dc++;
}
data_zone[dc]=0; // 0 indicates the end of the string
dc++;
}
//function find_command checks if the command appears in the command
//table.if it finds the command-it returns the number of the command
int find_command(char* line,int line_counter)
{
char comm_name[LINE_LENGTH]; //holds the command name to check
int ind1=0,ind2=0; //temp indexes
if(flag[line_counter])
ind=strchr(line,':')-line+1;
else ind=0;
while (isspace(line[ind]))
ind++;
while (!isspace(line[ind]) && line[ind]!='\0')
comm_name[ind1++]=line[ind++];
comm_name[ind1]='\0';
while ((command_list[ind2].command)!='\0')
{
if (0==strcmp (comm_name,command_list[ind2].command))
return ind2;
else ind2++;
}
return bad;
}