-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu driven final progarm
More file actions
355 lines (353 loc) · 10.1 KB
/
Copy pathmenu driven final progarm
File metadata and controls
355 lines (353 loc) · 10.1 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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
int display();
void dispintegration(int);
typedef struct optab
{
char in[20];//instruction
int op;//opcode
}optab;
typedef struct instruc
{
int ad;//address of instruction
char l[10];//label
char i[10];//instruction
char o[10];//operand
int opcode;//opcode
char symtab[10];
}instruc;
int main()
{
optab a[20];
char fname[20],character; //TO STORE ALL INSTRUCTIONS AND OPCODES
int address[20]={0x0},amain[20]={0},h[10],size=0x0,f1; //TO STORE ADDRESSES OF SYMBOLS IN PROGRAM
instruc b[30]; //TO STORE PROGRAM
int ad=0x0,disp=0x0,t=0xaaaa,ch1; //ADDRESS,NEW ADDRESS,DISPLACEMENT
int i=0,j=0,nad=0x0,pc=0x0,k,m,c,k1,q,nnad,counter;
long long int t1; //VARIABLES
char ch;
FILE *f; //FILE POINTER
int choice,ch7;
system("COLOR 02");
printf("\n\n\t\tJAC ASSEMBLER:\n\n");
f=fopen("opcode.txt","r"); //READING OPCODE FILE
if(f==NULL)
{
puts("CAN'T OPEN FILE");
exit(1);
}
else
{
while(fscanf(f,"%s\t%x\n",&a[i].in,&a[i].op)!=EOF)
{
i++;
}
}
fclose(f);
printf("ENTER THE FILENAME : ");
gets(fname);
f=fopen(fname,"r");
//READING PROGRAM FILE
if(f==NULL)
{
puts("CAN'T OPEN FILE");
exit(2);
}
else
{
while(fscanf(f,"%s\t%s\t%s\n",&b[j].l,&b[j].i,&b[j].o)!=EOF)
{
//printf("%s\t%s\t%s\n",b[j].l,b[j].i,b[j].o);
j++;
}
}
fclose(f); //PROGRAM FILE READ
nad=0;
printf("\nPROGRAM WILL START AT 0000 ADDRESS INITIALLY\n");
printf("\nDO YOU WANT TO RELOCATE :\n\nPRESS y/Y FOR YES or ANY OTHER KEY FOR NO : "); //RELOCATABLE ADDRESS
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{
printf("\nENTER NEW ADDRESS:");
scanf("%x",&nad);
}
system("cls");
//pass 1
t=0xaaaa;
b[0].ad=nad;
b[0].opcode=0xaaaa;
pc=nad;
for(k=1;k<j;k++)
{
c=0;
//address calcultion
b[k].ad=pc;
//checking symbol in symbol table
if(strcmp(b[k].l,"_____"))
{
for(i=0;i<j;i++)
{
if(!strcmp(b[i].symtab,b[k].l))
{
printf("\nERROR\n");
exit(3);
}
}
strcpy(b[k].symtab,b[k].l);
address[k]=b[k].ad;
}
//checking for intruction in optab
for(m=0;m<37;m++)
{
if(!strcmp(b[k].i,a[m].in))
{
c++;
b[k].opcode=a[m].op;
amain[k]=1;
pc=pc+3;
}
}
if(c==0)
{
if(!strcmp(b[k].i,"RESB"))
{
q=atoi(b[k].o);
b[k].opcode=0xaaaa;
pc=pc+q;
}
else if(!strcmp(b[k].i,"RESW"))
{
q=atoi(b[k].o);
pc=pc+3*q;
b[k].opcode=0xaaaa;
}
else if(!strcmp(b[k].i,"BYTE"))
{
nnad=strlen(b[k].o);
//nnad=nnad-3;
pc=pc+nnad;
b[k].opcode=atoi(b[k].o);
}
else if(!strcmp(b[k].i,"WORD"))
{
pc=pc+3;
q=atoi(b[k].o);
b[k].opcode=q;
}
}
}//PASS 1 ENDS
size=(pc-nad)+1;
printf("\nPASS 1 ENDS:\n\nSIZE CALCULATED IS %x\n\n",size);
printf("\npress enter to continue:");
getch();
system("cls");
k1=k;
//pass 2
k1=k;
pc=nad;
for(k=1;k<j-1;k++)
{
c=0;
for(m=0;m<37;m++)
{
if(!strcmp(b[k].i,a[m].in))
{
c++;
pc=pc+3;
break;
}
}
if(c==1)
{
disp=0x0;
for(m=0;m<j;m++)
{
if(!strcmp(b[k].o,b[m].symtab))
{
disp=address[m];
}
}
b[k].opcode=(b[k].opcode*0x10000)+disp;
}
}//PASS 2 ENDS
printf("\nPASS 2 ENDS:\n");
printf("\nDO YOU WANT TO PRINT THE PROGRAM:\n\nPRESS 1 FOR YES AND ANY OTHER KEY FOR NO : ");
scanf("%d",&ch1);
printf("\n");
if(ch1==1)
{
for(k=0;k<j;k++)
{
printf("%x\t%s\t%s\t%s\t",b[k].ad,b[k].l,b[k].i,b[k].o);
if(b[k].opcode==0xaaaa)
{
printf(" \n");
}
else
{
printf("%x\n",b[k].opcode);
}
}
}
printf("\npress enter to continue:");
getch();
system("cls");
//writing output
pc=nad;
int qq=0;
printf("\n");
for(i=0;i<j;i++)
{
if(amain[i]==1)
{
if(b[i].opcode!=0xaaaa)
{
if(qq%4==0)
{
printf("\n%x\t",pc);
}
qq++;
printf("%x\t",b[i].opcode);
pc=pc+3;
}
else
{
printf("x");
}
}
}
//writing text record
system("cls");
counter=0;
f=fopen("record.txt","w");
fprintf(f,"H %x %x\n",nad,size);
for(i=0;i<j;i++)
{
if(amain[i]==1)
{
counter++;
}
}
if(counter>30)
{
if((counter/2)<=30)
{
counter=2;
}
else if((counter/3)<=30)
{
counter=3;
}
else if((counter/4)<=30)
{
counter=4;
}
else if((counter/5)<=30)
{
counter=5;
}
}
if(counter>0)
{
fprintf(f,"T ");
for(i=0;i<j;i++)
{
if(amain[i]==1)
{
if(b[i].opcode!=0xaaaa)
{
fprintf(f,"%x ",b[i].opcode);
}
if(b[i].opcode==0xaaaa)
{
fprintf(f,"\n");
break;
}
}
}
printf("\n");
counter--;
}
for(i=0;i<j;i++)
{
if(amain[i]==1)
{
f1=address[i];
break;
}
}
fprintf(f,"\ne %x\n",f1);
fclose(f);
printf("PROGRAM ENDS PRESS ANY KEY FOR MORE OPTIONS : ");
getche();
system("cls");
do
{
system("cls");
printf("1. PRESS 1 TO EXIT \n");
printf("2. PRESS 2 TO KNOW THE SIZE\n");
printf("3. CREDITS \n");
printf("4. INSTRUCTION SET \n");
printf("5. GIVEN PROGRAMME\n");
printf("6. WHOLE PROGRAMME \n");
printf("7. ABOUT\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("THE PROGRAMME TERMINATES\n");
exit(0);
case 2:
printf("SIZE IS EQUAL TO : %X",size);
break;
case 3:
f=fopen("instruction.txt","r");
while(!feof(f))
{
character=fgetc(f);
printf("%c",character);
}
break;
case 4:
for(pc=0;pc<37;pc++)
{
printf("%s %x\n",a[pc].in,a[pc].op);
}
break;
case 5:
printf("GIVEN PROGRAMME \n");
for(pc=0;pc<j;pc++)
{
printf("%s\t%s\t%s\n",b[pc].l,b[pc].i,b[pc].o);
}
break;
case 6:
printf("WHOLE PROGRAMME\n");
for(pc=0;pc<j;pc++)
{
printf("%x\t%s\t%s\t%s\t",b[pc].ad,b[pc].l,b[pc].i,b[pc].o);
if(b[pc].opcode==0xaaaa)
{
printf(" \n");
}
else
{
printf("%x\n",b[pc].opcode);
}
}
break;
case 7:
f=fopen("about.txt","r");
while(!feof(f))
{
character=fgetc(f);
printf("%c",character);
}
}
printf("\n\nenter choice if you want view options again 1:yes ");
scanf("%d",&ch7);
system("cls");
}while(ch7==1);
}