-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1.SS.Assempler_Pass1.c
127 lines (114 loc) · 2.36 KB
/
_1.SS.Assempler_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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
printf("\nPASS 1 OF A TWO PASS ASSEMBLER\n");
printf("-----------------------------------\n");
int locctr,start,length,flag=0,len,err=1,c=0;
char opcode[10],label[10],operand[10],code[10],mnemonic[10],l[10];
FILE *fp1,*fp2,*fp3,*fp4;
fp1 = fopen("input.txt","r");
fp2 = fopen("optab.txt","r");
fp3 = fopen("symtab.txt","w+");
fp4 = fopen("intermediate.txt","w");
fscanf(fp1,"%s\t%s\t%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start = atoi(operand);
locctr = start;
fprintf(fp4,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s\t%s\t%s",label,opcode,operand);
}
else
locctr = 0;
while(strcmp(opcode,"END")!=0)
{
flag = 0;
fprintf(fp4,"%d\t",locctr);
if(strcmp(label,"**")!=0) //searching symtab for label
{
fseek(fp3,0,SEEK_SET);
fscanf(fp3,"%s\n",l);
while(!feof(fp3))
{
if(strcmp(label,l)==0)
{
break;
err=0;
}
//else
fscanf(fp3,"%s\n",l);
}
fseek(fp3,0,SEEK_END);
if(err==1)
fprintf(fp3,"%s\t%d\n",label,locctr);
//fprintf(fp3,"%s\t%d\n",label,locctr);
}
fscanf(fp2,"%s\t%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
if(strcmp(code,opcode)==0)
{
locctr+=3;
flag =1;
break;
}
fscanf(fp2,"%s\t%s",code,mnemonic);
}
if(strcmp(opcode,"WORD")==0)
{
flag=1;
locctr+=3;
}
else if(strcmp(opcode,"RESW")==0)
{
flag=1;
locctr+=(3*(atoi(operand)));
}
else if(strcmp(opcode,"RESB")==0)
{
flag=1;
locctr+=(atoi(operand));
}
else if(strcmp(opcode,"BYTE")==0)
{
flag=1;
len=strlen(operand)-2;
locctr+=len;
}
else if(flag==0)
{
printf("\nERROR!..OPCODE '%s' NOT FOUND!\n",opcode);
break;
}
fprintf(fp4,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s\t%s\t%s",label,opcode,operand);
}
fprintf(fp4,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
length = locctr- start;
printf("\nLength of the program is %d\n",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}
// INPUT.txt
// ** START 2000
// ** LDA FIVE
// ** STA ALPHA
// ** LDCH CHARZ
// ** STCH C1
// ALPHA RESW 2
// FIVE WORD 5
// CHARZ BYTE C'Z'
// C1 RESB 1
// BETA MULL K
// * END *
// OPTAB.txt
// START *
// LDA 03
// STA 0f
// LDCH 53
// STCH 57
// END *