-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pass1.c
247 lines (246 loc) · 9.07 KB
/
Pass1.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
struct ASMS{
char Label[7];
char Operand[7];
char Operator[7];
};
struct symboltable{
int lno;
char lab[7];
};
typedef struct symboltable symboltable;
typedef struct ASMS ASMS;
symboltable stab[10];
int stab_size = 0;
int LOCATION_COUNTER = 0;
int val(char c){
if (c >= '0' && c <= '9')
return (int)c - '0';
else
return (int)c - 'A' + 10;
}
int toDeci(char *str, int base){
int len = strlen(str);
int power = 1;
int num = 0;
int i;
for (i = len - 1; i >= 0; i--)
{
if (val(str[i]) >= base)
{
printf("Invalid Number");
return -1;
}
num += val(str[i]) * power;
power = power * base;
}
return num;
}
int no_of_words(char *statement){
int i=0;
int count=0;
char c;
while(statement[i]!='\n'){
if(statement[i]==' '){
count++;
}
i++;
}
return ++count;
}
int no_of_lines_in_file(FILE *fp){
int count=0;
char ss[66];
while(fgets(ss,55,fp)){
count++;
}
return count;
}
int processing_of_start(ASMS *asms){
if(strcmp(asms[0].Operand,"START")==0 || strcmp(asms[0].Operand,"start")==0){
///printf("We are now going to process start\n");
///LOCATION_COUNTER = atoi(asms[0].Operator);
LOCATION_COUNTER = toDeci(asms[0].Operator,16);
///printf("The location counter vale is : %d",LOCATION_COUNTER);
}
}
int checking_for_Valid_operand(char *op){
FILE *fp = fopen("optab.txt","r");
char statement[15];
char a[8],b[8];
while(fgets(statement,15,fp)){
sscanf(statement,"%s %s",a,b);
if(strcmp(a,op)==0){
return 1;
}
}
return 0;
}
int creation_of_symbol_table_array(ASMS *asms,int size){
int i,j=0;
for(i=0;i<size;i++){
if(strcmp(asms[i].Operand,"WORD")==0){
stab[stab_size].lno = i+1;
sscanf(asms[i].Label,"%s",stab[stab_size].lab);
stab_size++;
}
if(strcmp(asms[i].Operand,"RESW")==0){
stab[stab_size].lno = i+1;
sscanf(asms[i].Label,"%s",stab[stab_size].lab);
stab_size++;
}
if(strcmp(asms[i].Operand,"BYTE")==0){
stab[stab_size].lno = i+1;
sscanf(asms[i].Label,"%s",stab[stab_size].lab);
stab_size++;
}
if(strcmp(asms[i].Operand,"RESB")==0){
stab[stab_size].lno = i+1;
sscanf(asms[i].Label,"%s",stab[stab_size].lab);
stab_size++;
}
}
///Check for repetitions
char *c;
FILE *errfile = fopen("ErrorsFile.txt","w+");
for(;j<stab_size;j++){
strcpy(c,stab[j].lab);
int k;
for(k=j;k<stab_size;k++){
if(strcmp(stab[k].lab,c)==0 && j != k){
printf("Repetition occured\n");
fprintf(errfile,"Repetition : %s \t Line No : %d\n",stab[k].lab,stab[k].lno);
}
}
}
fclose(errfile);
return 1;
}
int writing_to_intermediate_file(ASMS *asms,int size){
FILE *fp2 = fopen("intermediateFile.txt","w+");
FILE *symfile = fopen("symboltable.txt","w+");
FILE *errfile = fopen("ErrorsFile.txt","a");
int i=0;
symboltable symtbl[10];
int j=0;
//printf("\n\nOk now we are writing to the file\n\n");
for(i=0;i<size;i++){
int locctr = LOCATION_COUNTER;
char *label=asms[i].Label;
char *operand= asms[i].Operand;
char *operatr= asms[i].Operator;
if(strcmp(asms[i].Operand,"START")==0){
//printf("START : %X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
LOCATION_COUNTER+=0;
continue;
}
if(strcmp(asms[i].Operand,"END")==0){
//printf("END : %X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
LOCATION_COUNTER+=0;
continue;
}
if(strcmp(asms[i].Operand,"WORD")==0){
//printf("WORD : %X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(symfile,"%X %s\n",LOCATION_COUNTER,asms[i].Label);
LOCATION_COUNTER+=3;
continue;
}
if(strcmp(asms[i].Operand,"BYTE")==0){
//printf("RESW : %X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(symfile,"%X %s\n",LOCATION_COUNTER,asms[i].Label);
LOCATION_COUNTER+=1;
}
if(strcmp(asms[i].Operand,"RESW")==0){
//printf("RESW : %X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(symfile,"%X %s\n",LOCATION_COUNTER,asms[i].Label);
LOCATION_COUNTER+=3*atoi(asms[i].Operator);
continue;
}
if(strcmp(asms[i].Operand,"RESB")==0){
//printf("RESB : %X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(symfile,"%X %s\n",LOCATION_COUNTER,asms[i].Label);
LOCATION_COUNTER+=atoi(asms[i].Operator);
continue;
}
if(strcmp(asms[i].Operand,"LTORG")){
//Literal table comes into picture
}
if(checking_for_Valid_operand(asms[i].Operand)){
//printf("Valid : %X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
if(asms[i].Operator[0]=='='){
if(!(strcmp(asms[i].Label,"\t")==0)){
fprintf(symfile,"%X %s\n",LOCATION_COUNTER,asms[i].Label);
}
if(asms[i].Operator[1]=='C'){
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
LOCATION_COUNTER+=strlen(asms[i].Operator)-3;
continue;
}
if(asms[i].Operator[1]=='X'){
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
LOCATION_COUNTER+=(strlen(asms[i].Operator)-3)/2;
continue;
}
}
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
if(strcmp(asms[i].Label,"\t")){
fprintf(symfile,"%X %s\n",LOCATION_COUNTER,asms[i].Label);
}
LOCATION_COUNTER+=3;
continue;
}
else{
//printf("ELSE : %X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(fp2,"%X %s %s %s\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator);
fprintf(errfile,"Invalid Operation : %X %s %s %s\t at line no %d\n",LOCATION_COUNTER,asms[i].Label,asms[i].Operand,asms[i].Operator,(i+1));
LOCATION_COUNTER+=1;
continue;
}
}
}
int main(){
FILE *fp1 = fopen("input.txt","r");
char statement[444];
int no_of_lines = no_of_lines_in_file(fp1);
fp1 = fopen("input.txt","r");
ASMS asms[no_of_lines];
int i=0;
while(fgets(statement,444,fp1)){
if(no_of_words(statement)==3){
//printf("Statement %d = %s\n",i,statement);
sscanf(statement,"%s %s %s", asms[i].Label, asms[i].Operand, asms[i].Operator);
}
if(no_of_words(statement)==2){
//printf("Statement %d = %s\n",i,statement);
sscanf(statement,"%s %s", asms[i].Operand, asms[i].Operator);
strcpy(asms[i].Label,"\t");
}
if(no_of_words(statement)==1){
//printf("Statement %d = %s\n",i,statement);
sscanf(statement,"%s", asms[i].Operand);
strcpy(asms[i].Label,"\t");
strcpy(asms[i].Operator,"\t");
}
i++;
}
processing_of_start(asms);
creation_of_symbol_table_array(asms,no_of_lines);
writing_to_intermediate_file(asms,no_of_lines);
/*
for(i=0;i<no_of_lines;i++){
printf(" ---------------------Statement %d -----------------------\n",i);
printf("The label is : %s\n",asms[i].Label);
printf("The operand is : %s\n",asms[i].Operand);
printf("The operator is : %s\n",asms[i].Operator);
}*/
return 1;
}