-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass1.txt
83 lines (68 loc) · 1.46 KB
/
pass1.txt
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
//Pass-1 of two-pass assembler
#include<stdio.h>
#include<string.h>
void main()
{
FILE *f1,*f2,*f3,*f4;
f1=fopen("input.txt","r");
f3=fopen("symtab.txt","w");
f4=fopen("output.txt","w");
int lc,sa;
char label[20],opcode[20],operand[20];
fscanf(f1,"%s %s %s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
sa=strtol(operand,NULL,16);
fprintf(f4,"%X\t%s\t%s\t%s\n",sa,label,opcode,operand);
}
else
sa=0;
lc=sa;
fscanf(f1,"%s %s %s",label,opcode,operand);
while(strcmp(opcode,"END")!=0)
{
fprintf(f4,"%X\t%s\t%s\t%s\n",lc,label,opcode,operand);
if(strcmp(label,"-")!=0)
{
fprintf(f3,"%s\t%X\n",label,lc);
}
char tempcode[20],tempval[20];
f2=fopen("optab.txt","r");
fscanf(f2,"%s %s",tempcode,tempval);
while(!feof(f2))
{
if(strcmp(opcode,tempcode)==0)
{
lc+=3;
break;
}
fscanf(f2,"%s %s",tempcode,tempval);
}
fclose(f2);
if(strcmp(opcode,"WORD")==0)
{
lc+=3;
}
if(strcmp(opcode,"RESW")==0)
{
lc=lc+(3*(strtol(operand,NULL,10)));
}
if(strcmp(opcode,"RESB")==0)
{
lc=lc+strtol(operand,NULL,10);
}
if(strcmp(opcode,"BYTE")==0)
{
if(opcode[0]=='X')
lc++;
else
lc=lc+strlen(operand)-3;
}
fscanf(f1,"%s %s %s",label,opcode,operand);
}
fprintf(f4,"%X\t%s\t%s\t%s\n",lc,label,opcode,operand);
printf("\nOutput File generated as output.txt\n");
fclose(f1);
fclose(f4);
fclose(f3);
}