C - Program to Implement Two Pass Assembler
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct a
{
char b[10][10];
};
struct a source[10];
struct a symtab[10];
struct a littab[10];
struct a opcode[10];
struct a reg[10];
struct a decstate[3];
FILE *f1,*f2,*f3,*f4,*f5,*f6,*f7,*f8
;
int litcount,litlines,vari,prolines,w,x,symlines,reglines,oplines,loc,flag=0;
void main ()
{
int i,k=0,j,l,t,flag1=0;
char ch;
f1=fopen("source2.txt","r");
f2=fopen("symtab1.txt","w");
f3=fopen("opcodes.txt","r");
f5=fopen("literal.txt","w");
f4=fopen("register.txt","r");
f6=fopen("out2pass1.txt","w");
f7=fopen("error.txt","w");
f8=fopen("decstate.txt","r");
clrscr();
printf("\n source program \n");
while(!feof(f1))
{
for(i=0;i<4;i++)
{
fscanf(f1,"%s",&source[k].b[i]);
printf("%s",source[k].b[i]);
printf("\t");
}
printf("\n");
k++;
}
prolines=k;
getch();
clrscr();
loc=atoi(source[0].b[3]);
j=0;
k=0;
printf("\nDeclaration Statement\n");
while(!feof(f8))
{
for(i=0;i<2;i++)
{
fscanf(f8,"%s",&decstate[k].b[i]);
printf("%s",decstate[k].b[i]);
printf("\t");
}
printf("\n");
k++;
}
if(strcmp(source[0].b[1],"START")!=0)
{
fprintf(f7,"Invalid Start of program");
}
else
fprintf(f7,"");
for(i=0;i<prolines;i++)
{
if(strcmp(source[i].b[1],"END")==0)
{
flag=1;
}
}
if(flag==0)
fprintf(f7,"\nInvalid End of Program");
else
fprintf(f7,"");
printf("\n SYMBOL TABLE\n");
printf("SI.NO\tSYMBOL\tADDRESS\n");
for(i=0;i<23;i++)
{
if(strcmp(source[i].b[0],"*")!=0)
{
j++;
fprintf(f2,"%d\t%s\t%d",j,source[i].b[0],(loc+i)+1);
printf("%d\t%s\t%d",j,source[i].b[0],(loc+i)+1);
printf("\n");
fprintf(f2,"\n");
}
}
getch();
clrscr();
k=0;
printf("\n register table\n");
while(!feof(f4))
{
for(i=0;i<2;i++)
{
fscanf(f4,"%s",reg[k].b[i]);
printf("%s",reg[k].b[i]);
printf("\t");
}
printf("\n");
k++;
}
reglines=k;
getch();
clrscr();
k=0;
loc=atoi(source[0].b[3]);
for(i=0;i<10;i++)
if(strcmp(source[i].b[1],"LTORG")==0)
litcount=loc+i+1;
printf("\nliteral table\n");//literal table generation
k=1;
for(i=1;i<=prolines;i++)
if(source[i].b[3][0]=='=')
{
fprintf(f5,"%d\t%s\t%d",k,source[i].b[3],litcount);
printf("%d\t%s\t%d",k,source[i].b[3],litcount);
k++;
litcount++;
printf("\n");
}
litlines=k-1;
getch();
clrscr();
k=0;
printf("\n opcode table\n");
while(!feof(f3))
{
for(i=0;i<2;i++)
{
fscanf(f3,"%s",&opcode[k].b[i]);
printf("%s",opcode[k].b[i]);
printf("\t");
}
printf("\n");
k++;
}
fclose(f3);
oplines=k;
f3=fopen("opcodes.txt","r");
for(l=0;l<23;l++)
{
flag1=0;
for(t=0;t<19;t++)
{
if(strcmp(source[l].b[1],opcode[t].b[0])==0)
{
flag1=1;
break;
}
}
if(flag1==0)
fprintf(f7,"\nInvalid opcode %d %s %s",l,source[l].b[1],opcode[t].b[0]);
}
getch();
clrscr();
k=0;
fclose(f5);
f5=fopen("literal.txt","r");
while(!feof(f5))
{
for(i=0;i<3;i++)
{
fscanf(f5,"%s",&littab[k].b[i]);
}
k++;
}
fclose(f1);
fclose(f2);
fclose(f3);
fclose(f4);
fclose(f5);
fclose(f7);
f2=fopen("symtab1.txt","r");
k=0;
while(!feof(f2)) //creation of symbol table struct
{
for(i=0;i<3;i++)
{
fscanf(f2,"%s",&symtab[k].b[i]);
}
k++;
}
symlines=k;
fclose(f2);
printf("\nintermediate code \n");
fprintf(f6,"\nINTERMEDIATE CODE \n");
ch=source[10].b[2][0];
prolines=prolines+2;
printf("(AD,00)\t\t(C,%s)\n",source[0].b[3]);
fprintf(f6,"(AD,00)\t\t(C,%s)\n",source[0].b[3]);
for(i=1;i<=prolines;i++)//starting the creation of intermediate code
{
w=x=0;
if(strcmp(source[i].b[1],"DS")!=0 && strcmp(source[i].b[1],"DC")!=0 )
{
for(j=0;j<10;j++)
if(strcmp(source[i].b[1],opcode[j].b[0])==0)
{
//vari=atoi(opcode[j].b[1]);
printf("(IS,%s)\t",opcode[j].b[1]);
fprintf(f6,"(IS,%s)\t",opcode[j].b[1]);
}
}
if(strcmp(source[i].b[1],"DS")==0 || strcmp(source[i].b[1] ,"DC")==0 )
{
for(j=0;j<3;j++)
if(strcmp(source[i].b[1],decstate[j].b[0])==0)
{
printf("(DL,%s)\t",decstate[j].b[1]);
fprintf(f6,"(DL,%s)\t",decstate[j].b[1]);
}
} //finish of first column
for(j=0;j<4;j++)
{
if(strcmp(source[i].b[2],reg[j].b[0])==0)
{
w=1;
printf("(%s)\t",reg[j].b[1]);
fprintf(f6,"(%s)\t",reg[j].b[1]);
}
}
if(source[i].b[2][0]==ch) //where ch=" ' "
{
x=1;
printf("\t(C,%c)\t",source[i].b[2][1]);
fprintf(f6,"\t(C,%c)\t",source[i].b[2][1]);
}
if(w!=1 && x!=1 && strcmp(source[i].b[1],"LTORG")!=0)
{
printf("\t");
fprintf(f6,"\t");
}
for(j=0;j<symlines;j++)
if(strcmp(source[i].b[3],symtab[j].b[1])==0)
{
printf("(S,%d)",atoi(symtab[j].b[0]));
fprintf(f6,"(S,%d)",atoi(symtab[j].b[0]));
}
if(source[i].b[3][0]=='=')
for(j=0;j<3;j++)
if(strcmp(source[i].b[3],littab[j].b[1])==0)
{
printf("(L,%s)",littab[j].b[0]);
fprintf(f6,"(L,%s)",littab[j].b[0]);
}
if(strcmp(source[i].b[1],"LTORG")!=0)
{
printf("\n");
fprintf(f6, "\n");
}
if(strcmp(source[i].b[1],"END")==0)
printf("(AD,03)");
fprintf(f6,"(AD,03)");
}
fclose(f8);
getch();
}
#include<stdio.h>
#include<stdlib.h>
struct a
{
char b[10][10];
};
struct a source[10];
struct a symtab[10];
struct a littab[10];
struct a opcode[10];
struct a reg[10];
struct a decstate[3];
FILE *f1,*f2,*f3,*f4,*f5,*f6,*f7,*f8
;
int litcount,litlines,vari,prolines,w,x,symlines,reglines,oplines,loc,flag=0;
void main ()
{
int i,k=0,j,l,t,flag1=0;
char ch;
f1=fopen("source2.txt","r");
f2=fopen("symtab1.txt","w");
f3=fopen("opcodes.txt","r");
f5=fopen("literal.txt","w");
f4=fopen("register.txt","r");
f6=fopen("out2pass1.txt","w");
f7=fopen("error.txt","w");
f8=fopen("decstate.txt","r");
clrscr();
printf("\n source program \n");
while(!feof(f1))
{
for(i=0;i<4;i++)
{
fscanf(f1,"%s",&source[k].b[i]);
printf("%s",source[k].b[i]);
printf("\t");
}
printf("\n");
k++;
}
prolines=k;
getch();
clrscr();
loc=atoi(source[0].b[3]);
j=0;
k=0;
printf("\nDeclaration Statement\n");
while(!feof(f8))
{
for(i=0;i<2;i++)
{
fscanf(f8,"%s",&decstate[k].b[i]);
printf("%s",decstate[k].b[i]);
printf("\t");
}
printf("\n");
k++;
}
if(strcmp(source[0].b[1],"START")!=0)
{
fprintf(f7,"Invalid Start of program");
}
else
fprintf(f7,"");
for(i=0;i<prolines;i++)
{
if(strcmp(source[i].b[1],"END")==0)
{
flag=1;
}
}
if(flag==0)
fprintf(f7,"\nInvalid End of Program");
else
fprintf(f7,"");
printf("\n SYMBOL TABLE\n");
printf("SI.NO\tSYMBOL\tADDRESS\n");
for(i=0;i<23;i++)
{
if(strcmp(source[i].b[0],"*")!=0)
{
j++;
fprintf(f2,"%d\t%s\t%d",j,source[i].b[0],(loc+i)+1);
printf("%d\t%s\t%d",j,source[i].b[0],(loc+i)+1);
printf("\n");
fprintf(f2,"\n");
}
}
getch();
clrscr();
k=0;
printf("\n register table\n");
while(!feof(f4))
{
for(i=0;i<2;i++)
{
fscanf(f4,"%s",reg[k].b[i]);
printf("%s",reg[k].b[i]);
printf("\t");
}
printf("\n");
k++;
}
reglines=k;
getch();
clrscr();
k=0;
loc=atoi(source[0].b[3]);
for(i=0;i<10;i++)
if(strcmp(source[i].b[1],"LTORG")==0)
litcount=loc+i+1;
printf("\nliteral table\n");//literal table generation
k=1;
for(i=1;i<=prolines;i++)
if(source[i].b[3][0]=='=')
{
fprintf(f5,"%d\t%s\t%d",k,source[i].b[3],litcount);
printf("%d\t%s\t%d",k,source[i].b[3],litcount);
k++;
litcount++;
printf("\n");
}
litlines=k-1;
getch();
clrscr();
k=0;
printf("\n opcode table\n");
while(!feof(f3))
{
for(i=0;i<2;i++)
{
fscanf(f3,"%s",&opcode[k].b[i]);
printf("%s",opcode[k].b[i]);
printf("\t");
}
printf("\n");
k++;
}
fclose(f3);
oplines=k;
f3=fopen("opcodes.txt","r");
for(l=0;l<23;l++)
{
flag1=0;
for(t=0;t<19;t++)
{
if(strcmp(source[l].b[1],opcode[t].b[0])==0)
{
flag1=1;
break;
}
}
if(flag1==0)
fprintf(f7,"\nInvalid opcode %d %s %s",l,source[l].b[1],opcode[t].b[0]);
}
getch();
clrscr();
k=0;
fclose(f5);
f5=fopen("literal.txt","r");
while(!feof(f5))
{
for(i=0;i<3;i++)
{
fscanf(f5,"%s",&littab[k].b[i]);
}
k++;
}
fclose(f1);
fclose(f2);
fclose(f3);
fclose(f4);
fclose(f5);
fclose(f7);
f2=fopen("symtab1.txt","r");
k=0;
while(!feof(f2)) //creation of symbol table struct
{
for(i=0;i<3;i++)
{
fscanf(f2,"%s",&symtab[k].b[i]);
}
k++;
}
symlines=k;
fclose(f2);
printf("\nintermediate code \n");
fprintf(f6,"\nINTERMEDIATE CODE \n");
ch=source[10].b[2][0];
prolines=prolines+2;
printf("(AD,00)\t\t(C,%s)\n",source[0].b[3]);
fprintf(f6,"(AD,00)\t\t(C,%s)\n",source[0].b[3]);
for(i=1;i<=prolines;i++)//starting the creation of intermediate code
{
w=x=0;
if(strcmp(source[i].b[1],"DS")!=0 && strcmp(source[i].b[1],"DC")!=0 )
{
for(j=0;j<10;j++)
if(strcmp(source[i].b[1],opcode[j].b[0])==0)
{
//vari=atoi(opcode[j].b[1]);
printf("(IS,%s)\t",opcode[j].b[1]);
fprintf(f6,"(IS,%s)\t",opcode[j].b[1]);
}
}
if(strcmp(source[i].b[1],"DS")==0 || strcmp(source[i].b[1] ,"DC")==0 )
{
for(j=0;j<3;j++)
if(strcmp(source[i].b[1],decstate[j].b[0])==0)
{
printf("(DL,%s)\t",decstate[j].b[1]);
fprintf(f6,"(DL,%s)\t",decstate[j].b[1]);
}
} //finish of first column
for(j=0;j<4;j++)
{
if(strcmp(source[i].b[2],reg[j].b[0])==0)
{
w=1;
printf("(%s)\t",reg[j].b[1]);
fprintf(f6,"(%s)\t",reg[j].b[1]);
}
}
if(source[i].b[2][0]==ch) //where ch=" ' "
{
x=1;
printf("\t(C,%c)\t",source[i].b[2][1]);
fprintf(f6,"\t(C,%c)\t",source[i].b[2][1]);
}
if(w!=1 && x!=1 && strcmp(source[i].b[1],"LTORG")!=0)
{
printf("\t");
fprintf(f6,"\t");
}
for(j=0;j<symlines;j++)
if(strcmp(source[i].b[3],symtab[j].b[1])==0)
{
printf("(S,%d)",atoi(symtab[j].b[0]));
fprintf(f6,"(S,%d)",atoi(symtab[j].b[0]));
}
if(source[i].b[3][0]=='=')
for(j=0;j<3;j++)
if(strcmp(source[i].b[3],littab[j].b[1])==0)
{
printf("(L,%s)",littab[j].b[0]);
fprintf(f6,"(L,%s)",littab[j].b[0]);
}
if(strcmp(source[i].b[1],"LTORG")!=0)
{
printf("\n");
fprintf(f6, "\n");
}
if(strcmp(source[i].b[1],"END")==0)
printf("(AD,03)");
fprintf(f6,"(AD,03)");
}
fclose(f8);
getch();
}