Implematation of Pass 1
Implematation of Pass 1
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char opcode[10],mnemonic[3],operand[10],label[10],code[10];
int loc,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
fp1=fopen("input.dat","r");
fp2=fopen("symtab.dat","w");
fp3=fopen("out.dat","w");
fp4=fopen("opcode.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start=atoi(operand);
loc=start;
fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
else
loc=0;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d\t",loc);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,loc);
fscanf(fp4,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
if(strcmp(opcode,code)==0)
{
loc+=3;
break;
}
fscanf(fp4,"%s%s",code,mnemonic);
}
if(strcmp(opcode,"WORD")==0)
loc+=3;
else if(strcmp(opcode,"RESW")==0)
loc+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
loc+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0)
++loc;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
fprintf(fp3,"%d\t%s\t%s\t%s\n",loc,label,opcode,operand);
length=loc-start;
printf("The length of the program is : %d",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
INPUT FILES :
input.dat
**
START
2000
**
**
**
**
ALPHA
FIVE
CHARZ
C1
**
LDA
STA
LDCH
STCH
RESW
WORD
BYTE
RESB
END
opcode.dat
START
LDA
STA
LDCH
STCH
END
*
03
0F
53
57
FIVE
ALPHA
CHARZ
C1
1
5
C'Z'
1
**