AIM: Write a program to implement PASS 1 of a two pass Assembler.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct symtab
{
char sym[10];
int
symadd;
};
struct symtab
st[20];
void main()
{
char
opcode[10],mnemonic[3],op1[10],op2[10],label[10],code[10],ic[10];
int
locctr,start,length,symcnt=0,symid=0;
FILE
*finput,*foutput,*fsymtab,*foptab;
clrscr();
finput=fopen("C:/SP_Prog/SP/INPUT.txt","r");
fsymtab=fopen("C:/SP_Prog/SP/SYMTAB.txt","w");
foutput=fopen("C:/SP_Prog/SP/OUTPUT.txt","w");
foptab=fopen("C:/SP_Prog/SP/OPTAB.txt","r");
fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);
char icop1[10],icop2[10];
if(strcmp(opcode,"START")==0)
{
start=atoi(op1);
locctr=start;
strcpy(icop1,"(C,");
strcat(icop1,op1);
strcat(icop1,")");
fprintf(foutput,"%s\t\t%s\t\t%s\n","(AD-01)",icop1,"");
fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);
}
else
locctr=0;
int i;
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"ORIGIN")==0)
{
locctr=atoi(op1);
strcpy(ic,"(AD,03)");
}
if(strcmp(label,"**")!=0)
{
symcnt=symcnt
+ 1;
if(strcmp(opcode,"EQU")==0)
{
if(isdigit(op1[0]));
{
fprintf(fsymtab,"%s\t%d\n",label,locctr);
}
}
symid=symid+1;
fprintf(fsymtab,"%d\t\t%s\t\t%d\n",symid,label,locctr);
}
rewind(foptab);
fscanf(foptab,"%s%s",code,ic);
while(strcmp(code,NULL)!=0)
{
if(strcmp(opcode,code)==0)
{
locctr+=1;
break;
}
fscanf(foptab,"%s%s",code,ic);
}
char icop1[5];
if(isdigit(op1[0])!=0)
{
strcpy(icop1,"(C,");
strcat(icop1,op1);
strcat(icop1,")");
}
else
if(strcmp(op1,"AREG")==0)
{
strcpy(icop1,"(1)");
}
else
if(strcmp(op1,"BREG")==0)
{
strcpy(icop1,"(2)");
}
else
if(strcmp(op1,"CREG")==0)
{
strcpy(icop1,"(3)");
}
else
if(strcmp(op1,"DREG")==0)
{
strcpy(icop1,"(4)");
}
else
{
strcpy(icop1,op1);
}
if(strcmp(op2,"**")==0)
strcpy(icop2,"");
else
strcpy(icop2,op2);
fprintf(foutput,"%s\t\t%s\t\t%s\n",ic,icop1,icop2);
fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);
}
fscanf(foutput,"%s%s%s%s",label,opcode,op1,op2);
length=locctr-start;
printf("The length of the
program is %d",length);
fclose(finput);
fclose(fsymtab);
fclose(foutput);
fclose(foptab);
getch();