Вы находитесь на странице: 1из 6

/* ASSEMBLER PASS I */

#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
struct optab
//OPCODE TABLE
{
char opcode[10], clas[5], info[14];
}opt[10];
struct symtab
//SYMBOL TABLE
{
char sym[10];
int addr, len;
}symt[5];
struct littab
{
char ltt[10];
int adr;
}lit[5];

//LITERAL TABLE

struct pooltab
{
int loc;
}pot[5];
void main()
{
FILE *fs, *ft1,*ft2,*ft3,*ft4,*ft5,*ft6;
//
int i=0, flag=0, org=0, n=0, k=0,ln=0, cnt=0, j=0, l=0, st=0, lt
=0, ot=0, pt=0;
int len,i=0,flag=0,org=0,n=0,k=0,ln=0,cnt=0,j=0,l=0,st=0,lt=0,pt=0,regfl
ag=0;
char op[4] = {' ','\n','\t',','};
char c, ch[6], *c1, word[50][10], ic[2], *str,temp[2][10];
char ad[5][10]={"start","end","origin","equ","ltorg"};
char dl[2][10]={"dc","ds"};
char is[11][10]={"stop","add","sub","mult","mover","movem","comp","bc","
div","read","print"};
clrscr();
fs=fopen("c:\\TC\\BIN\\in.c","r+"); //input file
ft1=fopen("c:\\TC\\BIN\\aspop1.c","w+"); //line numbers
ft3=fopen("c:\\TC\\BIN\\aspop3.c","w+"); //symbol table
ft4=fopen("c:\\TC\\BIN\\aspop4.c","w+"); //literal table
ft5=fopen("c:\\TC\\BIN\\aspop5.c","w+"); //pool table
ft6=fopen("c:\\TC\\BIN\\aspop6.c","w+"); //intermediate code

while((c=fgetc(fs))!=EOF)
{
for(i=0;i<4;i++)
store
{

//for seperating strings


//if any operator - dont

flag=0;
if(c==op[i])
{
flag=1;
break;
}
}
if(flag==0)
//store characters
{
word[n][k]=c;
k++;
}
else
//termination of string
{
if(k!=0)
{
if(c!='\t' && c!=' ')
//accept
the operators as seperate string
{
word[n][k]='\0';
n++;
k=0;
word[n][0]=c;
k++;
}
word[n][k]='\0';
n++;
k=0;
}
}
}
word[n][k]='\0';
cnt=n;
for(i=0;i<=cnt;i++)
{
printf("%s",word[i]);
}
for(i=0;i<cnt;i++)
//find address to
{
//store program
if((strcmp(word[i],ad[0]))==0)
{
i=i+1;
str= word[i];
ln=atoi(str);
//convert str
to int
break;
}
}
for(i=0;i<cnt;i++)
// symbol table
{
k=strlen(word[i]);
if(word[i][k-1]==':' || (strcmp(word[i+1],"ds")==0) || (strcmp(w
ord[i+1],"dc")==0))
{
strcpy(symt[st].sym,word[i]);
symt[st].len=1;
st++;
}

}
pot[pt].loc=1;
j=0;
fprintf(ft1,"\n
LINE NUMBERS \n");
for(i=0;i<cnt;i++)
//line numbers
{
if(word[i][0]>='0' && word[i][0]<='9')

//1st character=numb

er?
{
if( (strcmp(word[i-1],"dc")!=0) && (strcmp(word[i-1],"ds
")!=0) && (strcmp(word[i-1],ad[0])!=0) )
{
strcpy(lit[lt].ltt,word[i]);
lt++;
}
}
if(strcmp(word[i],"ltorg")==0)
{
for(l=0;l<lt;l++)
{
fprintf(ft1,"\n%d %s",ln,lit[l].ltt);
lit[l].adr=ln;
ln++;
}
pt++;
pot[pt].loc=lt+1;
org=lt;
}
if(strcmp(word[i+1],"ds")!=0)
{
if(strcmp(word[i],"ltorg")!=0 && strcmp(word[i+1],"ltorg
")!=0)
fprintf(ft1," %s",word[i]);
if(j<st && (strcmp(symt[j].sym,word[i])==0))
//symb
ol address
{
symt[j].addr=ln-1;
j++;
}
if(strcmp(word[i],"\n")==0 && i!=cnt-1 && strcmp(word[i+1],"ltorg")!=0)
{
fprintf(ft1,"%d ",ln);
ln++;
}
}

else

// data storage ds
{
for(l=0;l<st;l++)
{
if(strcmp(word[i],symt[l].sym)==0)
{
symt[l].addr=ln-1;
break;
}

}
str=word[i+2];
n=atoi(str);
for(k=0;k<n-1;k++)
//data storage space
{
fprintf(ft1,"\n%d ",ln);
ln++;
}
i=i+2;
}
}
for(i=org;i<lt;i++)
//end address for literals
{
fprintf(ft1,"%d %s\n",ln,lit[i].ltt);
lit[i].adr=ln;
ln++;
}
fprintf(ft3,"\n\n
SYMBOL TABLE\n");
fprintf(ft3,"SYMBOL\tADDR\tLEN\n\n");
for(i=0;i<st;i++)
//SYMBOL TABLE
{
k=strlen(symt[i].sym);
if(symt[i].sym[k-1]==':')
{
strcpy(temp[0],symt[i].sym);
temp[0][k-1]='\0';
fprintf(ft3,"\n%s",temp[0]);
fprintf(ft3,"\t%d\t%d\n",symt[i].addr,symt[i].len);
}
else
fprintf(ft3,"\n%s\t%d\t%d\n",symt[i].sym,symt[i].addr,symt[i].len);
}
fprintf(ft4,"\n\n
LITERAL TABLE\n");
fprintf(ft4,"LITERAL\tADDR\n\n");

for(i=0;i<lt;i++)
//LITERAL TABLE
{
fprintf(ft4,"\n%s\t %d\n",lit[i].ltt,lit[i].adr);
}
fprintf(ft5,"\n\n
POOL TABLE\n");
fprintf(ft5,"INDEX\tLOCATION\n\n");
for(i=0;i<=pt;i++)
//POOL TABLE
{
fprintf(ft5,"\n%d\t #%d\n",i+1,pot[i].loc);
}
fprintf(ft6,"\n\nINTERMEDIATE CODE\n");

for(i=0;i<=cnt;i++)
//INTERMEDIATE CODE
{
flag=0;
for(l=0;l<5;l++)

{
if(strcmp(word[i],ad[l])==0)
{
strcpy(ic,"AD");
k=l+1;
flag=1;
regflag=1;
break;
}
}
for(l=0;l<2;l++)
{
if(strcmp(word[i],dl[l])==0)
{
strcpy(ic,"DL");
k=l+1;
flag=1;
regflag=1;
break;
}
}
for(l=0;l<11;l++)
{
if(strcmp(word[i],is[l])==0)
{
strcpy(ic,"IS");
k=l+1;
flag=1;
regflag=1;
break;
}
}

for(l=0;l<lt;l++)
{
if(strcmp(word[i],lit[l].ltt)==0)
{
strcpy(ic,"L ");
k=l+1;
flag=1;
regflag=1;
break;
}
}
if(strcmp(word[i],"areg")==0 || strcmp(word[i],"breg")==0 || str
cmp(word[i],"creg")==0 || strcmp(word[i],"dreg")==0)
{
fprintf(ft6," %s ",word[i]);
regflag=1;
}

if((strcmp(word[i-1],"ds")==0) || (strcmp(word[i-1],"dc")==0) ||
(strcmp(word[i-1],"start")==0))
{
strcpy(ic,"C ");
str=word[i];

k=atoi(str);
flag=1;
regflag=1;
}
if(strcmp(word[i],"\n")==0)
{
fprintf(ft6,"%s",word[i]);
regflag=1;
}
len=strlen(word[i]);
if(regflag==0 && word[i][len-1]!=':' && strcmp(word[i+1],"dc")!
=0 && strcmp(word[i+1],"ds")!=0 && word )
{
fprintf(ft6,"%s",word[i]);
}
if(flag==1)
{
fprintf(ft6,"(%s,%d)\t",ic,k);
}
regflag=0;
}
fclose(ft1);
fclose(ft2);
fclose(ft3);
fclose(ft4);
fclose(ft5);
fclose(ft6);
fclose(fs);
getch();
}

Оценить