-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecond_scan.c
More file actions
773 lines (688 loc) · 25 KB
/
second_scan.c
File metadata and controls
773 lines (688 loc) · 25 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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/****************************************************************
* File Name: second_scan.c *
* File Type: source file *
*****************************************************************/
/*-------------------------------------------------------------------*/
/* The function performs the second scan according to the algorithm */
/* and creates the as file */
/*-------------------------------------------------------------------*/
#include "header.h"
extern int dc,ic, //data instruction counters
end_ic, //last ic line
ERROR;
extern char label_name[LABEL_LENGTH ]; //holds the label names
extern int ind,tmp,
data_zone[DATA_ZONE],code_zone[DATA_ZONE]; //data and code zones
extern int there_is_extern,there_is_entry,
good_line[MAXLINES], //legal lines
flag[MAXLINES], //indicates that a line has a label
line_address[MAXLINES]; //lines address
extern char colon;
enum {no_exist=-200,fn_register,bad_num_oper,bad_for_one,bad_for_one2,fn_direct_for_one,
fn_number_for_one,bad_for_two,fn_number_for_two,bad_second,fn_reg_leb_for_one
,fn_lab_for_one,fn_direct_for_two};
extern char *register_table[],*ex;
extern struct commands {
char *code;
char *command;
int num_of_operands;
int L;
} command_list[];
extern struct label *root;
int exist //used to check if a label exists
,f,
external[DATA_ZONE], //external commands
relocatable[DATA_ZONE]; //realocatable parameters
extern FILE *ext;
char *is_data;
/*********************************************************************/
/* performs the second scan according to the algorithm */
/*********************************************************************/
void second_scan(FILE *ifp)
{
char *found_label,
line[LINE_LENGTH];
int command_ind,
line_counter=0,//line counter
answer_type,two,one,i,l=0,a1;
struct label *root1=root;
for (i=0;i<DATA_ZONE;external[i]=relocatable[i]=0,i++);
//perform the second scan
is_data=NULL;
ic=0; //first line in algorithm
while (fgets(line,LINE_LENGTH,ifp)!=NULL) //while it`s not end of file
{
if (strchr(line,'\n')!=NULL) //if '\n' is found, replace with '\0'
*(strchr(line,'\n'))='\0';
++line_counter;
ind=0;
if (line[ind]=='\0') //no information
continue;
if (line[ind]==';') //remarks->skip
continue;
while (line[ind]!='\0')
{
if (isspace(line[ind])) //this is an empty line
ind++;
else
break;
}
if (line[ind]=='\0') //line contains only white spaces->skip
continue;
if ((good_line[line_counter])!=yes) //an error was found during first scan
{
if (good_line[line_counter]!=different_type && good_line[line_counter]!=label_already_exists)
{ // the error is not because of a reoccurring type
if ((command_ind=find_command(line,line_counter))!=bad)
{ //continue with increasing ic
if(command_list[command_ind].L==1)
ic=ic+1;
else
ic=ic+how_many_lines_to_save(line);
}
//command is unknown
print_error(line_counter,good_line[line_counter]);
continue;
}
}
if (has_data_or_string(line)) //line #4 in algorithm
continue;
answer_type = check_if_extrn_or_entry(line);
if (answer_type==extrn || answer_type==entry) //line #5 in algorithm
//extern or entry
{
if (answer_type==entry) //line #6 in algorithm
{
there_is_entry=yes;
ind=strstr(line,".entry")-line+7;
while (isspace(line[ind]))
ind++;
tmp=ind;
colon='\0';
found_label=exclude_label_from_line(line);
exist=yes;
root=does_entry_label_exist(found_label,root);
if (!exist)
print_error(line_counter,no_exist);
}
continue; //end of line 6 in second algorithm
} //if there is a command
if ((command_ind=find_command(line,line_counter))==bad)
continue; //end of third line in algorithm
code_zone[ic]=command_ind*8*8*8*8;
if (num_of_operands(command_ind,line)==bad_num_oper)
{//number of operands found does not match the num of operands required by this command
print_error(line_counter,bad_num_oper);
if(command_list[command_ind].L==1)
ic=ic+1;
else
ic=ic+how_many_lines_to_save(line);
// line #9 in algorithm
continue;
}
if (command_list[command_ind].num_of_operands==1)
//a one operand command
{
one=one_oprand(command_ind,line);
if (one<no) //an error was found
print_error(line_counter,one);
}
if (command_list[command_ind].num_of_operands==2)
//a two operand command
{
a1=ind;
two=two_oprand(command_ind,line);
ind=a1;
if (two<no) //an error was found
print_error(line_counter,two);
}
if (flag[line_counter]==yes)
//line #11 in algorithm
{
if (good_line[line_counter]==different_type || good_line[line_counter]==label_already_exists)
print_error(line_counter,good_line[line_counter]);
}
if(command_list[command_ind].L==1)
ic=ic+1;
else
ic=ic+how_many_lines_to_save(line);
// line #9 in algorithm
}
}
/*********************************************************************/
/* function build_entries_file builds an entry file (*.ent) */
/*********************************************************************/
void build_entries_file(char *argv)
{
FILE *ent;
char *entries;
if (there_is_entry)
{
entries=build_new_file_name(argv,"ent");
if ((ent=fopen(entries,"w"))==NULL)
printf("Can not open file %s,for the entries \n",entries);
else
{
print_entries_file(root,ent); //write into the file
fclose(ent);
}
}
}
/*********************************************************************/
/* function print_entries_file writes the entry information into */
/* the entry file built by the previous function build_entries_file */
/*********************************************************************/
void print_entries_file(struct label*p,FILE* fp)
{
if (p!=NULL)
{
if (p->entry)
{
if (!(strncmp(p->type,"data",4)))
fprintf(fp,"%-30s %4o\n",p->label_name,p->address+end_ic);
else
fprintf(fp,"%-30s %4o\n",p->label_name,p->address);
}
print_entries_file(p->next,fp);
}
}
/*********************************************************************************/
/* print_externals_file writes information into the external labels file (*.ext) */
/*********************************************************************************/
void print_externals_file(char* name_of_ext)
{
if (ext!=NULL)
fprintf(ext," %-30s %4o\n",name_of_ext,ic+1);
}
/*********************************************************************/
/* function does_entry_label_exist checks if an entry label exists */
/*********************************************************************/
struct label* does_entry_label_exist (char* ll,struct label*p)
{
if (p==NULL)
exist=no;
else
if(strcmp(p->label_name,ll))
p->next=does_entry_label_exist(ll,p->next);
else
p->entry=yes;
return p;
}
/*********************************************************************/
/* function num_of_operands counts the number of operands by counting*/
/* the number of commas between the operands */
/*********************************************************************/
int num_of_operands(int found,char* line)
{
int a1;
int commas=0, //counts the ','
there_is_operand=no, //check operand after ','
oper=no; //num of operands
a1=strstr(line,command_list[found].command)-line+strlen(command_list[found].command);
while (line[a1]!='\0')
{
if (line[a1]==',')
commas++;
if (!isspace(line[a1]))
there_is_operand=yes;
a1++;
}
oper=commas+there_is_operand;
if (command_list[found].num_of_operands!=oper)
return bad_num_oper;
else
return oper;
}
/**********************************************************************/
/*function add_zero prints zeros at the number prefix to adjust length*/
/**********************************************************************/
void add_zero (FILE *out,int n)
{
if (n<8)
fprintf(out,"00000");
else if (n<8*8)
fprintf(out,"0000");
else if (n<8*8*8)
fprintf(out,"000");
else if (n<8*8*8*8)
fprintf(out,"00");
else if (n<8*8*8*8*8)
fprintf(out,"0");
}
/*********************************************************************/
/* function one_operand handles one operand commands in source file. */
/* The function identifies the addressing type and the data and puts */
/* them in the code zone */
/*********************************************************************/
int one_oprand (int found,char* line)
{
int b,r,n=ind;
char operand_name[LINE_LENGTH ]; //operand name
n=strstr(line,command_list[found].command)-line+strlen(command_list[found].command);
//find operand location
while (isspace(line[n]))
n++;
switch(line[n]){
case '#': //immediate addressing
if (!strcmp (command_list[found].command,"inc")||
!strcmp (command_list[found].command,"dec")||
!strcmp (command_list[found].command,"jmp")||
!strcmp (command_list[found].command,"bne")||
!strcmp (command_list[found].command,"prn")||
!strcmp (command_list[found].command,"jsr"))
return fn_direct_for_one;
n++;
b=0;
while(line[n]!='\0')
operand_name[b++]=line[n++];
operand_name[b]='\0';
if (!is_legal_number(operand_name))
return fn_number_for_one;
code_zone[ic+1]=atoi(operand_name);
return yes;
break;
case '@'://indirect addressing or indirect register addressing 2,5
n++;
b=0;
while(line[n]!='\0' && line[n]!=' ')
operand_name[b++]=line[n++];
operand_name[b]='\0';
r=find_number_of_registers(operand_name);
if (r==bad) //it is not a register
{
f=bad;
is_data=NULL;
found_label(operand_name,root);
if (f==bad)
return bad_for_one2;
code_zone[ic]=code_zone[ic]+2*8;
code_zone[ic+1]=f;
if (external[ic+1])
print_externals_file(operand_name);
else
relocatable[ic+1]=yes;
if (is_data)
code_zone[ic+1]=code_zone[ic+1]+end_ic;
return yes;
} //if this is a register
else{
code_zone[ic]=code_zone[ic]+5*8+r;
code_zone[ic+1]=r;
return yes;
break;
}
case'*': // rational addressing 3
n++;
b=0;
while(line[n]!='\0' && line[n]!=' ')
operand_name[b++]=line[n++];
operand_name[b]='\0';
f=bad;
found_label(operand_name,root);
if (f==bad)
return bad_for_one;
code_zone[ic]=code_zone[ic]+3*8;
code_zone[ic+1]=f-ic;
if (external[ic+1])
print_externals_file(operand_name);
else
relocatable[ic+1]=yes;
return yes;
break;
default: //direct register addressing or direct addressing 1,4
b=0;
while(line[n]!='\0' && !isspace(line[n]))
operand_name[b++]=line[n++];
operand_name[b]='\0';
r=find_number_of_registers(operand_name);
if (r==bad) // direct addressing 1
{
is_data=NULL;
f=bad;
found_label(operand_name,root);
if (f==bad)
return bad_for_one2;
code_zone[ic]=code_zone[ic]+1*8;
code_zone[ic+1]=f;
if (external[ic+1])
print_externals_file(operand_name);
else
relocatable[ic+1]=yes;
if (is_data)
code_zone[ic+1]=code_zone[ic+1]+end_ic;
return yes;
}
// direct addressing 4
if (!strcmp (command_list[found].command,"inc") ||
!strcmp (command_list[found].command,"dec") ||
!strcmp (command_list[found].command,"jmp") ||
!strcmp (command_list[found].command,"bne") ||
!strcmp (command_list[found].command,"prn") ||
!strcmp (command_list[found].command,"jsr") )
{
code_zone[ic]=code_zone[ic]+4*8+r;
return yes;
}
else
return fn_register;
break;
}
}
/*********************************************************************/
/* function set_second_operand handles the second operand in the two */
/* operand commands */
/*********************************************************************/
//
int set_second_operand(int found,char* line)
{
int b,r2;
char ttt[80];
while(isspace(line[ind]))
ind++;
ind++;
switch(line[ind]){
case '#': //immediate addressing to the second operand
if (strcmp(command_list[found].command,"cmp"))
return fn_direct_for_two;
ind++;
b=0;
while(line[ind]!='\0')
ttt[b++]=line[ind++];
ttt[b]='\0';
if (!is_legal_number(ttt))
return fn_number_for_two;
code_zone[ic+2]=atoi(ttt);
return yes;
break;
case '@': //indirect addressing or indrect register addressing
ind++;
b=0;
while(line[ind]!='\0' && line[ind]!=' ')
ttt[b++]=line[ind++];
ttt[b]='\0';
r2=find_number_of_registers(ttt);
if (r2==bad) //this is not a register
{
is_data=NULL;
f=bad;
found_label(ttt,root);
if (f==bad)
return bad_for_two;
code_zone[ic]=code_zone[ic]+2*8;
code_zone[ic+2]=f+end_ic;
if (external[ic+2])
print_externals_file(ttt);
else
relocatable[ic+2]=yes;
if (is_data)
code_zone[ic+2]=code_zone[ic+2]+end_ic;
return yes;
}
code_zone[ic]=code_zone[ic]+5*8+r2;
return yes;
break;
case '*': //rational addressing
ind++;
b=0;
while(line[ind]!='\0' && line[ind]!=' ')
ttt[b++]=line[ind++];
ttt[b]='\0';
is_data=NULL;
f=bad;
found_label(ttt,root);
if (f==bad)
return bad_second;
code_zone[ic]=code_zone[ic]+3*8;
code_zone[ic+2]=f-ic;
if (external[ic+2])
print_externals_file(ttt);
if (is_data)
code_zone[ic+2]=code_zone[ic+2]+end_ic;
return yes;
break;
default://direct addressing or direct register addressing 1,4
b=0;
while(line[ind]!='\0' && !isspace(line[ind]))
ttt[b++]=line[ind++];
ttt[b]='\0';
r2=find_number_of_registers(ttt);
if (r2==bad)
{
f=bad;
is_data=NULL;
found_label(ttt,root);
if (f==bad)
return bad_for_two;
code_zone[ic]=code_zone[ic]+1*8;
code_zone[ic+2]=f;
if (external[ic+2])
print_externals_file(ttt);
else
relocatable[ic+2]=yes;
if (is_data)
code_zone[ic+2]=code_zone[ic+2]+end_ic;
return yes;
}
code_zone[ic]=code_zone[ic]+4*8+r2;
return yes;
break;
}
}
/*********************************************************************/
/* function two_operand deals with with two operand commands.it */
/* identifies the addressing type and the data and puts them in the */
/* code zone */
/*********************************************************************/
int two_oprand (int found,char* line)
{
int b,r2;
char tmp_operand[LINE_LENGTH];
ind=strstr(line,command_list[found].command)-line+strlen(command_list[found].command);
while (isspace(line[ind]))
ind++;
b=0;
switch(line[ind]){
case '#': //immediate addressing for the first operand '0'
ind++;
if (!strcmp (command_list[found].command,"lea"))
return fn_direct_for_one;
while ((line[ind]!=',') && (!isspace(line[ind])))
tmp_operand[b++]=line[ind++];
tmp_operand[b]='\0';
if (!is_legal_number(tmp_operand))
return fn_number_for_one;
code_zone[ic+1]=atoi(tmp_operand);
while (isspace(line[ind]))
ind++;
return (set_second_operand(found,line));
case '@': // indirect addressing or indirect register addressing 2,5
ind++;
b=0;
if (!strcmp (command_list[found].command,"lea"))
return fn_reg_leb_for_one;
while ((line[ind]!=',') && (!isspace(line[ind])))
tmp_operand[b++]=line[ind++];
tmp_operand[b]='\0';
r2=find_number_of_registers(tmp_operand);
if (r2==bad) //this is not a register
{
is_data=NULL;
f=bad;
found_label(tmp_operand,root);
if (f==bad)
return bad_for_one2;
code_zone[ic]=code_zone[ic]+2*8*8*8;
code_zone[ic+1]=f;
if (external[ic+1])
print_externals_file(tmp_operand);
else
relocatable[ic+1]=yes;
if (is_data)
code_zone[ic+1]=code_zone[ic+1]+end_ic;
}
else code_zone[ic]=code_zone[ic]+5*8*8*8+r2*8*8;
return (set_second_operand(found,line));
case '*': //first operand - rational addressing 3
ind++;
if (!strcmp (command_list[found].command,"lea"))
return fn_lab_for_one;
while ((line[ind]!=',') && (!isspace(line[ind])))
tmp_operand[b++]=line[ind++];
tmp_operand[b]='\0';
is_data=NULL;
f=bad;
found_label(tmp_operand,root);
if (f==bad)
return bad_for_one;
code_zone[ic]=code_zone[ic]+3*8*8*8;
code_zone[ic+1]=f-ic;
if (external[ic+1])
print_externals_file(tmp_operand);
if (is_data)
code_zone[ic+1]=code_zone[ic+1]+end_ic;
return (set_second_operand(found,line));
default://first operand - direct addressing 1 or direct register addressing 4
b=0;
while(line[ind]!=',' && !isspace(line[ind]))
tmp_operand[b++]=line[ind++];
tmp_operand[b]='\0';
r2=find_number_of_registers(tmp_operand);
if (r2==bad) //direct addressing
{
f=bad;
is_data=NULL;
found_label(tmp_operand,root);
if (f==bad)
return bad_for_one2;
code_zone[ic]=code_zone[ic]+1*8*8*8;
code_zone[ic+1]=f;
if (external[ic+1])
print_externals_file(tmp_operand);
else
relocatable[ic+1]=yes;
if (is_data)
code_zone[ic+1]=code_zone[ic+1]+end_ic;
}
else // direct register addressing
{
if (!strcmp (command_list[found].command,"lea"))
return bad_for_one;
code_zone[ic]=code_zone[ic]+4*8*8*8+r2*8*8;
}
return (set_second_operand(found,line));
}
}
/*********************************************************************/
/* function found_label finds the label location and updates if it is*/
/* a part of data or external */
/*********************************************************************/
void found_label (char* ll,struct label*p)
{
char*r=p->type;
if (p!=NULL)
{
if (!(strcmp(p->label_name,ll)))
{
f=p->address;
external[ic+1]=(!(strcmp(p->type,"extr")));
is_data=(strstr(p->type,"data"));
}
found_label(ll,p->next); //recursive call
}
}
/*********************************************************************/
/* function find_number_of_registers */
/*********************************************************************/
int find_number_of_registers(char* temp)
{
int i;
for(i=0;i<=7;i++)
{
if(!strcmp(register_table[i],temp))
return i;
}
return bad;
}
/*********************************************************************/
/* function print_error prints error messages into the as file */
/*********************************************************************/
void print_error(int line_counter,int x)
{
printf("**Error** %s(%d) - ",ex,line_counter);
good_line[line_counter]=x;
ERROR=yes;
switch(x) {
case illegal_first_char :
printf("First char in label is illegal\n");
break;
case label_too_long :
printf("Length of label is more than 30 characters\n");
break;
case Illegal_chars_in_label :
printf("Label has non alfa characters\n");
break;
case nftfc :
printf("Colon is not in the first column\n");
break;
case different_type:
printf("Label already exists with a different type \n");
break;
case label_already_exists:
printf("Label already defined \n");
break;
case reserverd_word :
printf("Can't be a name of a command or a register\n");
break;
case bad_numbers :
printf("Bad data\n");
break;
case no_begin :
printf("No legal beginning to the string\n");
break;
case no_end :
printf("No legal ending to the string\n");
break;
case Illegel:
printf("Illegel instruction\n");
break;
case no_exist:
printf("Symbol doesn't exist\n");
break;
case bad_num_oper:
printf("Illegal number of operands\n");
break;
case bad_for_one :
printf("The operand must be a label\n");
break;
case bad_for_one2 :
printf("The operand must be a label or a register\n");
break;
case fn_register :
printf("The operand can not be a register\n");
break;
case fn_direct_for_one :
printf("The operand can not be of direct addressing\n");
break;
case fn_number_for_one :
printf("Operand of direct addressing must be a legal number \n");
break;
case bad_for_two:
printf("The second operand must be a label or a register\n");
break;
case fn_reg_leb_for_one:
printf("The operand can not be a register or a lebel\n");
break;
case fn_lab_for_one:
printf("The operand can not be a label\n");
break;
case fn_number_for_two:
printf("Second operand of direct addressing must be a legal number \n");
break;
case bad_second:
printf("The second operand must be a label\n");
break;
case fn_direct_for_two:
printf("Second operand can not be direct\n");
break;
}
}