Tuesday, January 4, 2011

Design of Single Pass Assembler using C

DESCRIPTION
 input : sic source code in the file 'INPUT.DAT'
 out put : files : SYMTAB.DAT,OBJFILE.DAT,ERROR.DAT 
RUN
abhisheklal@abhisheklal-System-Product-Name:~/Documents/Single Pass/Single Pass$ gcc SINGLE.c 
abhisheklal@abhisheklal-System-Product-Name:~/Documents/Single Pass/Single Pass$ ./a.out
Output file:OBJCODE.DAT
Errors are described in ERROR.DAT
Symbol table is in the file:SYMTAB.DATabhisheklal@abhisheklal-System-Product-Name:~/Documents/Single Pass/Single Pass$
 
CODE
#include<stdio.h>
#include<string.h>
#define q 11//no. of mnemonics in the array A
void main()
{
int lc,ad,address,err=0;
int s,num,l,i=0,j,n=0,line=1,f=0,f1=0,t=0,ni=0,m=0,t1;
FILE *fp1,*fp2,*fp3,*fp4;
char lab[10],op[10],val[10],code[10];
char a[20][15]={"STA","STL","LDA","LDB","J","JEQ","J","SUB","COMP","STCH","ADD","SUB"};
char b[20][15]={"14","32","03","69","34","30","48","28","24","16","0C"};
char sym[15][10];
int symadd[15];
//clrscr();
fp1=fopen("INPUT.DAT","r");
fp2=fopen("OBJFILE.DAT","w");
fp3=fopen("ERROR.DAT","w");
fp4=fopen("SYMTAB.DAT","w");
while((!feof(fp1)))
{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
t++;
m++;
if(strcmp(op,".")==0)
m=0;
else if(strcmp(op,"END")==0)
break;
}
t=t-1;
m--;
fclose(fp1);
fp1=fopen("INPUT.DAT","r");
fscanf(fp1,"%s\t%s\t%x",lab,op,&lc);
fprintf(fp3,"-------------------------------------\n");
fprintf(fp3,"LINE NO.\t|ERROR FOUND\n");
fprintf(fp3,"-------------------------------------");
fprintf(fp4,"SYMBOL\tADDRESS");
s=lc;
fprintf(fp2,"H^%s^00%x^%x\n",lab,lc,t*3);
fprintf(fp2,"T^00%x^",lc);
if(m>10)
fprintf(fp2,"1E");
else
fprintf(fp2,"%x",m*3);
while((op,".")!=0&&(!feof(fp1)))
{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
line++;
if(strcmp(lab,"$")!=0)
{
for(i=0;i<n;i++)
{
if(strcmp(lab,sym[i])==0)
{
f=1;
break;
}
f=0;
}
if(f==0)
{
strcpy(sym[n],lab);
symadd[n]=lc;
fprintf(fp4,"\n%s\t%x",lab,lc);
n++;
}
if(f==1){
fprintf(fp3,"%d\t\t|SYMBOL ALREADY DEFINED\n",line);err++;}
}
num=atoi(val);
if(strcmp(op,"RESW")==0)
lc=lc+(num*3);
else if(strcmp(op,"RESB")==0)
lc=lc+num;
else if(strcmp(op,"BYTE")==0)
{
num=strlen(val)-3;
lc=lc+num;
for(i=2,j=0;i<strlen(val)-1;i++)
{
code[j]=val[i];
j++;
}
code[j]='\0';
fprintf(fp2,"^%s",code);
ni++;
}
else
lc=lc+3;
if(strcmp(op,".")==0)
break;
}
while(strcmp(op,"END")!=0&&(!feof(fp1)))
{
fscanf(fp1,"%s\t%s\t%s",lab,op,val);
line++;
if(strcmp(op,"END")==0)
break;
if((strcmp(lab,"$")!=0)&&((strcmp(op,"RESW")!=0||strcmp(op,"RESB")!=0||strcmp(op,"WORD")!=0||strcmp(op,"BYTE")==0)))
{
for(i=0;i<n;i++)
{
if(strcmp(lab,sym[i])==0)
{
f=1;
break;
}
f=0;
}
if(f==0)
{
strcpy(sym[n],lab);
symadd[n]=lc;
fprintf(fp4,"\n%s\t%x",lab,lc);
n++;
}
else{
fprintf(fp3,"\n%d\t\t|SYMBOL ALREADY DEFINED",err++);}
}
else if(strcmp(op,"RESW")==0||strcmp(op,"RESB")==0||strcmp(op,"WORD")==0||strcmp(op,"BYTE")==0)
fprintf(fp3,"\n%d\t\t|Declaration not allowed here",line);
if(strcmp(op,"RESW")!=0&&strcmp(op,"RESB")!=0&&strcmp(op,"WORD")!=0&&strcmp(op,"BYTE")!=0)
{
for(i=0;i<q;i++)
{
if(strcmp(op,a[i])==0)
{
strcpy(code,b[i]);
f1=0;
break;
}
f1=1;
}
if(f1==1){
fprintf(fp3,"\n%d\t\t|WRONG OPCODE",line);err++;}
for(i=0;i<n;i++)
{
if(strcmp(val,sym[i])==0)
{
address=symadd[i];
f=0;
break;
}
f=1;
}
if(f){
fprintf(fp3,"\n%d\t\t|UNDEFINED SYMBOL",line);err++;}
}
if(ni<10)
{
fprintf(fp2,"^%s%x",code,address);
ni++;
}
else
{
fprintf(fp2,"T^00%x^",lc);
if(m>10)
{
fprintf(fp2,"1E");
m=m-10;
}
else
{
fprintf(fp2,"%x",m*3);
fprintf(fp2,"^%s%x",code,address);
ni=0;
}
}
lc=lc+3;
}
fprintf(fp2,"\nE^00%x",s);
fprintf(fp3,"\nNo of errors=%d\n--------------------------------------",err);
printf("Output file:OBJCODE.DAT\nErrors are described in ERROR.DAT\nSymbol table is in the file:SYMTAB.DAT");
//getch();
fcloseall();
}

No comments:

Post a Comment