Assembler Pass 1 code
//ASSEMBLER PASS I C-SOURCE CODE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct mot
{
char name[10];
char cl[5];
}mot[20];
struct symtab
{
char symb[20];
int add;
int len;
}symtab[10];
struct littab
{
char lit[5];
int add;
}littab[10];
struct pooltab
{
int litno;
}pooltab[10];
struct reg
{
char regname[5];
}reg[5];
struct comp
{
char name[5];
}comp[5];
int pooltab_ptr=0,littab_ptr=0,symtab_ptr=0;
void reg_init()
{
strcpy(reg[0].regname,"AREG");
strcpy(reg[1].regname,"BREG");
strcpy(reg[2].regname,"CREG");
strcpy(reg[3].regname,"DREG");
}
void comp_init()
{
strcpy(comp[0].name,"G");
strcpy(comp[1].name,"GE");
strcpy(comp[2].name,"L");
strcpy(comp[3].name,"LE");
}
void mot_init()
{
strcpy(mot[0].name,"STOP");
strcpy(mot[0].cl,"IS");
strcpy(mot[1].name,"ADD");
strcpy(mot[1].cl,"IS");
strcpy(mot[2].name,"SUB");
strcpy(mot[2].cl,"IS");
strcpy(mot[3].name,"MULT");
strcpy(mot[3].cl,"IS");
strcpy(mot[4].name,"MOVER");
strcpy(mot[4].cl,"IS");
strcpy(mot[5].name,"MOVEM");
strcpy(mot[5].cl,"IS");
strcpy(mot[6].name,"COMP");
strcpy(mot[6].cl,"IS");
strcpy(mot[7].name,"BC");
strcpy(mot[7].cl,"IS");
strcpy(mot[8].name,"DIV");
strcpy(mot[8].cl,"IS");
strcpy(mot[9].name,"READ");
strcpy(mot[9].cl,"IS");
strcpy(mot[10].name,"PRINT");
strcpy(mot[10].cl,"IS");
}
void ins_sym(char str1[],int add,int len)
{
strcpy(symtab[symtab_ptr].symb,str1);
symtab[symtab_ptr].add=add;
symtab[symtab_ptr++].len=len;
}
int mot_search(char str1[])
{
int i;
for(i=0;i<11;i++)
{
if(!strcmp(str1,mot[i].name))
return i;
}
return -1;
}
int sym_search(char str1[])
{
int i;
for(i=0;i<symtab_ptr;i++)
{
if(!strcmp(str1,symtab[i].symb))
return i;
}
return -1;
}
int reg_search(char str1[])
{
int i;
for(i=0;i<4;i++)
{
if(!strcmp(str1,reg[i].regname))
return i+1;
}
return -1;
}
int lit_search(char str1[])
{
int i;
for(i=0;i<littab_ptr;i++)
{
if(!strcmp(str1,littab[i].lit))
return i;
}
return -1;
}
int comp_search(char str1[])
{
int i;
for(i=0;i<4;i++)
{
if(!strcmp(str1,comp[i].name))
return i+1;
}
return -1;
}
void pass1(char str[])
{
int i,j,loc=0,cntr;
//Initilization
pooltab[0].litno=0;
char temp[50],temp1[20],temp2[20],temp3[20],temp4[10],temp5[10],ch,sym;
mot_init();
reg_init();
comp_init();
printf("\nFILE=\n%s",str);
// getch();
i=0;
// int k=0;
printf("\n*****************IR**************\n");
while(1)
{
j=0;
do
{
ch=str[i];
temp[j]=str[i++];
if(ch==',')
temp[j]=' ';
j++;
}while(ch!='\n'&&ch!='\0');
if(ch=='\0')
break;
temp[--j]='\0';
//printf("\nTemp=%s",temp);
cntr=sscanf(temp,"%s%s%s%s",temp1,temp2,temp3,temp4);
//START or ORIGIN
if(!strcmp(temp1,"START")||!strcmp(temp1,"ORIGIN"))
{
loc=atoi(temp2);
printf("\n(AD,01) (C,%d)",loc);
loc--;
}
//Insert Label
j=0;
do
{
ch=temp1[j++];
}while(ch!=':'&&ch!=' '&&ch!='\0');
if(ch==':')
{
temp1[--j]=' ';
char str1[10],str2[10];
sscanf(temp1,"%s%s",str1,str2);
ins_sym(str1,loc,0);
strcpy(temp1,str2);//For removing label from temp1
temp1[strlen(temp1)]='\0';//For inserting \0 at last
}
//DL
if(!strcmp(temp2,"DS")||!strcmp(temp2,"DC"))
{
if(!strcmp(temp3,"?"))
{
char str4[5];
itoa(symtab[symtab_ptr-1].len,str4,10); //STR4=len
strcpy(temp3,str4);
}
j=0;
//For removing '
do
{
ch=temp3[j++];
if(!isalnum(ch))
temp3[--j]=' ';
}while(ch!=' '&&ch!='\0'&&ch!='\n');
//end of '
int y=sym_search(temp1);
if(y==-1)
{
ins_sym(temp1,loc,atoi(temp3));
loc+=atoi(temp3)-1;
}
else
{
symtab[y].add=loc;
symtab[y].len=atoi(temp3);
}
if(!strcmp(temp2,"DS"))
printf(" (DL,2) (C,%d)",atoi(temp3));
else
printf(" (DL,1) (C,%d)",atoi(temp3));
}
//IS
int x=mot_search(temp1);
if(x>-1)
{
printf(" (IS,%d)",x);
int y=reg_search(temp2);
if(y>-1)
printf(" (RG,%d)",y);
else
{
if(temp2[0]=='=')
{
printf(" (L,%d)",littab_ptr);
strcpy(littab[littab_ptr].lit,temp2);
littab[littab_ptr++].add=-1;
pooltab[pooltab_ptr++].litno=littab_ptr;
}
else
{
int z=sym_search(temp2);
if(z>-1)
printf(" (S,%d)",z);
else
{
int t1=comp_search(temp2);
if(t1>-1)
printf(" (CC,%d)",t1);
else
{
ins_sym(temp2,0,0);
z=sym_search(temp2);
if(z>-1)
printf(" (S,%d)",z);
}
}
}
}
if(cntr>2)
{
y=reg_search(temp3);
if(y>-1)
printf(" (RG,%d)",y);
else
{
if(temp3[0]=='=')
{
printf(" (L,%d)",littab_ptr);
strcpy(littab[littab_ptr].lit,temp3);
littab[littab_ptr++].add=-1;
pooltab[pooltab_ptr++].litno=littab_ptr;
}
else
{
int z=sym_search(temp3);
if(z>-1)
printf(" (S,%d)",z);
else
{
int t1=comp_search(temp3);
if(t1>-1)
printf(" (CC,%d)",t1);
else
{
ins_sym(temp3,0,0);
z=sym_search(temp3);
if(z>-1)
printf(" (S,%d)",z);
}
}
}
}
}
}
if(!strcmp(temp1,"LTORG"))
{
}
if(temp1[0]=='=')
{
int y=lit_search(temp1);
if(y>-1)
{
littab[y].add=loc;
}
else
{
strcpy(littab[littab_ptr].lit,temp1);
littab[littab_ptr++].add=loc;
}
}
printf("\n");
loc++;
// k++;
}
loc--;
for(i=0;i<littab_ptr;i++)
{
if(littab[i].add==-1)
{
littab[i].add=loc++;
}
}
//END
printf("(AD,02)");
// printf(" cntr=%d",cntr);
printf("\n************SYMTAB*********\n");
for(i=0;i<symtab_ptr;i++)
{
printf("\n %s %d %d",symtab[i].symb,symtab[i].add,symtab[i].len);
}
printf("\n**********LITTAB***********\n");
for(i=0;i<littab_ptr;i++)
printf("\n %s %d",littab[i].lit,littab[i].add);
}
void main()
{
char str[500],filename[10];
int i=0;
FILE *fp;
clrscr();
printf("\nEnter file name=");
scanf("%s",&filename);
fp=fopen(filename,"r");
if(fp!=NULL)
{
char ch;
do
{
ch=getc(fp);
str[i]=ch;
i++;
}while(ch!=EOF);
str[--i]='\0';
strupr(str);
pass1(str);
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct mot
{
char name[10];
char cl[5];
}mot[20];
struct symtab
{
char symb[20];
int add;
int len;
}symtab[10];
struct littab
{
char lit[5];
int add;
}littab[10];
struct pooltab
{
int litno;
}pooltab[10];
struct reg
{
char regname[5];
}reg[5];
struct comp
{
char name[5];
}comp[5];
int pooltab_ptr=0,littab_ptr=0,symtab_ptr=0;
void reg_init()
{
strcpy(reg[0].regname,"AREG");
strcpy(reg[1].regname,"BREG");
strcpy(reg[2].regname,"CREG");
strcpy(reg[3].regname,"DREG");
}
void comp_init()
{
strcpy(comp[0].name,"G");
strcpy(comp[1].name,"GE");
strcpy(comp[2].name,"L");
strcpy(comp[3].name,"LE");
}
void mot_init()
{
strcpy(mot[0].name,"STOP");
strcpy(mot[0].cl,"IS");
strcpy(mot[1].name,"ADD");
strcpy(mot[1].cl,"IS");
strcpy(mot[2].name,"SUB");
strcpy(mot[2].cl,"IS");
strcpy(mot[3].name,"MULT");
strcpy(mot[3].cl,"IS");
strcpy(mot[4].name,"MOVER");
strcpy(mot[4].cl,"IS");
strcpy(mot[5].name,"MOVEM");
strcpy(mot[5].cl,"IS");
strcpy(mot[6].name,"COMP");
strcpy(mot[6].cl,"IS");
strcpy(mot[7].name,"BC");
strcpy(mot[7].cl,"IS");
strcpy(mot[8].name,"DIV");
strcpy(mot[8].cl,"IS");
strcpy(mot[9].name,"READ");
strcpy(mot[9].cl,"IS");
strcpy(mot[10].name,"PRINT");
strcpy(mot[10].cl,"IS");
}
void ins_sym(char str1[],int add,int len)
{
strcpy(symtab[symtab_ptr].symb,str1);
symtab[symtab_ptr].add=add;
symtab[symtab_ptr++].len=len;
}
int mot_search(char str1[])
{
int i;
for(i=0;i<11;i++)
{
if(!strcmp(str1,mot[i].name))
return i;
}
return -1;
}
int sym_search(char str1[])
{
int i;
for(i=0;i<symtab_ptr;i++)
{
if(!strcmp(str1,symtab[i].symb))
return i;
}
return -1;
}
int reg_search(char str1[])
{
int i;
for(i=0;i<4;i++)
{
if(!strcmp(str1,reg[i].regname))
return i+1;
}
return -1;
}
int lit_search(char str1[])
{
int i;
for(i=0;i<littab_ptr;i++)
{
if(!strcmp(str1,littab[i].lit))
return i;
}
return -1;
}
int comp_search(char str1[])
{
int i;
for(i=0;i<4;i++)
{
if(!strcmp(str1,comp[i].name))
return i+1;
}
return -1;
}
void pass1(char str[])
{
int i,j,loc=0,cntr;
//Initilization
pooltab[0].litno=0;
char temp[50],temp1[20],temp2[20],temp3[20],temp4[10],temp5[10],ch,sym;
mot_init();
reg_init();
comp_init();
printf("\nFILE=\n%s",str);
// getch();
i=0;
// int k=0;
printf("\n*****************IR**************\n");
while(1)
{
j=0;
do
{
ch=str[i];
temp[j]=str[i++];
if(ch==',')
temp[j]=' ';
j++;
}while(ch!='\n'&&ch!='\0');
if(ch=='\0')
break;
temp[--j]='\0';
//printf("\nTemp=%s",temp);
cntr=sscanf(temp,"%s%s%s%s",temp1,temp2,temp3,temp4);
//START or ORIGIN
if(!strcmp(temp1,"START")||!strcmp(temp1,"ORIGIN"))
{
loc=atoi(temp2);
printf("\n(AD,01) (C,%d)",loc);
loc--;
}
//Insert Label
j=0;
do
{
ch=temp1[j++];
}while(ch!=':'&&ch!=' '&&ch!='\0');
if(ch==':')
{
temp1[--j]=' ';
char str1[10],str2[10];
sscanf(temp1,"%s%s",str1,str2);
ins_sym(str1,loc,0);
strcpy(temp1,str2);//For removing label from temp1
temp1[strlen(temp1)]='\0';//For inserting \0 at last
}
//DL
if(!strcmp(temp2,"DS")||!strcmp(temp2,"DC"))
{
if(!strcmp(temp3,"?"))
{
char str4[5];
itoa(symtab[symtab_ptr-1].len,str4,10); //STR4=len
strcpy(temp3,str4);
}
j=0;
//For removing '
do
{
ch=temp3[j++];
if(!isalnum(ch))
temp3[--j]=' ';
}while(ch!=' '&&ch!='\0'&&ch!='\n');
//end of '
int y=sym_search(temp1);
if(y==-1)
{
ins_sym(temp1,loc,atoi(temp3));
loc+=atoi(temp3)-1;
}
else
{
symtab[y].add=loc;
symtab[y].len=atoi(temp3);
}
if(!strcmp(temp2,"DS"))
printf(" (DL,2) (C,%d)",atoi(temp3));
else
printf(" (DL,1) (C,%d)",atoi(temp3));
}
//IS
int x=mot_search(temp1);
if(x>-1)
{
printf(" (IS,%d)",x);
int y=reg_search(temp2);
if(y>-1)
printf(" (RG,%d)",y);
else
{
if(temp2[0]=='=')
{
printf(" (L,%d)",littab_ptr);
strcpy(littab[littab_ptr].lit,temp2);
littab[littab_ptr++].add=-1;
pooltab[pooltab_ptr++].litno=littab_ptr;
}
else
{
int z=sym_search(temp2);
if(z>-1)
printf(" (S,%d)",z);
else
{
int t1=comp_search(temp2);
if(t1>-1)
printf(" (CC,%d)",t1);
else
{
ins_sym(temp2,0,0);
z=sym_search(temp2);
if(z>-1)
printf(" (S,%d)",z);
}
}
}
}
if(cntr>2)
{
y=reg_search(temp3);
if(y>-1)
printf(" (RG,%d)",y);
else
{
if(temp3[0]=='=')
{
printf(" (L,%d)",littab_ptr);
strcpy(littab[littab_ptr].lit,temp3);
littab[littab_ptr++].add=-1;
pooltab[pooltab_ptr++].litno=littab_ptr;
}
else
{
int z=sym_search(temp3);
if(z>-1)
printf(" (S,%d)",z);
else
{
int t1=comp_search(temp3);
if(t1>-1)
printf(" (CC,%d)",t1);
else
{
ins_sym(temp3,0,0);
z=sym_search(temp3);
if(z>-1)
printf(" (S,%d)",z);
}
}
}
}
}
}
if(!strcmp(temp1,"LTORG"))
{
}
if(temp1[0]=='=')
{
int y=lit_search(temp1);
if(y>-1)
{
littab[y].add=loc;
}
else
{
strcpy(littab[littab_ptr].lit,temp1);
littab[littab_ptr++].add=loc;
}
}
printf("\n");
loc++;
// k++;
}
loc--;
for(i=0;i<littab_ptr;i++)
{
if(littab[i].add==-1)
{
littab[i].add=loc++;
}
}
//END
printf("(AD,02)");
// printf(" cntr=%d",cntr);
printf("\n************SYMTAB*********\n");
for(i=0;i<symtab_ptr;i++)
{
printf("\n %s %d %d",symtab[i].symb,symtab[i].add,symtab[i].len);
}
printf("\n**********LITTAB***********\n");
for(i=0;i<littab_ptr;i++)
printf("\n %s %d",littab[i].lit,littab[i].add);
}
void main()
{
char str[500],filename[10];
int i=0;
FILE *fp;
clrscr();
printf("\nEnter file name=");
scanf("%s",&filename);
fp=fopen(filename,"r");
if(fp!=NULL)
{
char ch;
do
{
ch=getc(fp);
str[i]=ch;
i++;
}while(ch!=EOF);
str[--i]='\0';
strupr(str);
pass1(str);
}
getch();
}
0 comments:
Post a Comment