-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
243 lines (237 loc) · 7.21 KB
/
main.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
struct ASMS{
char LocCTR[5];
char Label[7];
char Operand[7];
char Operator[7];
};
typedef struct ASMS ASMS;
struct SYMBOLTABLE{
char locCTR[5];
char Label[7];
};
typedef struct SYMBOLTABLE SYMBOLTABLE;
struct OPTAB{
char Operand[7];
char Opcode[3];
};
typedef struct OPTAB OPTAB;
struct OPCODES{
char o[6];
};
typedef struct OPCODES OPCODES;
int optab_size;
int symboltable_size;
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;
while(statement[i]!='\n'){
char c = statement[i];
if(statement[i]==' '){
count++;
}
if(statement[i]=='\t'){
if(statement[i-1]!=39)
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 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;
}
void read_optab(OPTAB *op){
FILE *fp = fopen("optab.txt","r");
char statement[10];
int i=0;
while(fgets(statement,10,fp)){
///printf("\n%s %d\n",statement,no_of_words(statement));
sscanf(statement,"%s %s",op[i].Operand, op[i].Opcode);
//printf("\nOperand : %s\nOpcode : %s\n",op[i].Operand, op[i].Opcode);
i++;
}
fclose(fp);
}
void read_symbol_table(SYMBOLTABLE *s){
FILE *fp = fopen("symboltable.txt","r");
char ss[15];
int i=0;
while(fgets(ss,15,fp)){
sscanf(ss,"%s %s",s[i].locCTR,s[i].Label);
i++;
}
}
void get_opcode(OPTAB *op,char *key,char *value){
int i;
for(i=0;i<optab_size;i++){
if(strcmp(op[i].Operand,key)==0){
strcpy(value,op[i].Opcode);
}
}
}
void get_symbol(SYMBOLTABLE *stab,char *key,char *value){
int i;
for(i=0;i<symboltable_size;i++){
if(strcmp(stab[i].Label,key)==0){
strcpy(value,stab[i].locCTR);
}
}
}
void rev(char *str){
int i = 0;
int j = strlen(str) - 1;
char temp;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
int generate_opcodes(OPCODES *opcodes,int opcodes_size,ASMS *asms,int asms_size,OPTAB *otab,SYMBOLTABLE *stab){
int i,j=0;
for(i=0;i<asms_size;i++){
printf("\n");
if(strcmp(asms[i].Operand,"WORD")==0){
sscanf(asms[i].Operator,"%s",opcodes[j].o);
strcat(opcodes[j].o,"00000");
rev(opcodes[j].o);
printf("-- word --\nLoc : %s\nLabel : %s\nOperand : %s\nOperator : %s\n",asms[i].LocCTR,asms[i].Label,asms[i].Operand,asms[i].Operator);
//opcodes[j].o = strrev(opcodes[j].o);
printf("opcode : %s\n",opcodes[j].o);
j++;
continue;
}
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(asms[i].Operator[1]=='C'){
strcpy(opcodes[j].o,"------");
printf("Loc : %s\nLabel : %s\nOperand : %s\nOperator : %s\n",asms[i].LocCTR,asms[i].Label,asms[i].Operand,asms[i].Operator);
printf("opcode : ------\n");
j++;
continue;
}
if(asms[i].Operator[1]=='X'){
strcpy(opcodes[j].o,"------");
printf("Loc : %s\nLabel : %s\nOperand : %s\nOperator : %s\n",asms[i].LocCTR,asms[i].Label,asms[i].Operand,asms[i].Operator);
printf("opcode ; ------\n");
j++;
continue;
}
}
char f2[2],l4[4] = "0000";
get_opcode(otab,asms[i].Operand,f2);
get_symbol(stab,asms[i].Operator,l4);
strcpy(opcodes[j].o,strcat(f2,l4));
printf("Loc : %s\nLabel : %s\nOperand : %s\nOperator : %s\n",asms[i].LocCTR,asms[i].Label,asms[i].Operand,asms[i].Operator);
printf("opcode : %s\n",opcodes[j].o);
j++;
continue;
}
else{
printf("Loc : %s\nLabel : %s\nOperand : %s\nOperator : %s\n",asms[i].LocCTR,asms[i].Label,asms[i].Operand,asms[i].Operator);
printf("opcode : error\n");
strcpy(opcodes[j].o,"------");
}
}
}
int main(){
system("cc Pass1.c");
system("./a.out");
FILE *fpa = fopen("intermediateFile.txt","r");
int no_of_lines = no_of_lines_in_file(fpa);
FILE *fp1 = fopen("intermediateFile.txt","r");
ASMS asms[no_of_lines];
char statement[30];
int i=0;
int lno=-1;
while(fgets(statement,50,fp1)){
lno++;
if(lno==9){
int p1 = 0;
}
if(no_of_words(statement)==4){
//printf("Statement %d = %s\n",i,statement);
sscanf(statement,"%s %s %s %s", asms[i].LocCTR, asms[i].Label, asms[i].Operand, asms[i].Operator);
//printf("\nLocctr : %s\nLabel : %s\nOperand : %s\nOperator : %s\n", asms[i].LocCTR, asms[i].Label, asms[i].Operand, asms[i].Operator);
i++;
}
if(no_of_words(statement)==3){
//printf("Statement %d = %s\n",i,statement);
sscanf(statement,"%s \t %s %s", asms[i].LocCTR, asms[i].Operand, asms[i].Operator);
strcpy(asms[i].Label,"\t");
//printf("\nLocctr : %s\nLabel : %s\nOperand : %s\nOperator : %s\n", asms[i].LocCTR, asms[i].Label, asms[i].Operand, asms[i].Operator);
i++;
}
if(no_of_words(statement)==2){
//printf("Statement %d = %s\n",i,statement);
sscanf(statement,"%s \t %s \t", asms[i].LocCTR, asms[i].Operand);
strcpy(asms[i].Label,"\t");
strcpy(asms[i].Operator,"\t");
//printf("\nLocctr : %s\nLabel : %s\nOperand : %s\nOperator : %s\n", asms[i].LocCTR, asms[i].Label, asms[i].Operand, asms[i].Operator);
i++;
}
}
///for reading otpab
FILE *foptab = fopen("optab.txt","r");
optab_size = no_of_lines_in_file(foptab);
OPTAB optab[optab_size];
read_optab(optab);
fclose(foptab);
//printf("\n\n\n\n\n\n");
///for reading symboltable
FILE *fstab = fopen("symboltable.txt","r");
symboltable_size = no_of_lines_in_file(fstab);
SYMBOLTABLE stab[symboltable_size];
read_symbol_table(stab);
fclose(fstab);
OPCODES op[no_of_lines];
generate_opcodes(op,no_of_lines,asms,no_of_lines,optab,stab);
return 1;
}