Monday, 9 May 2011

Pass-1 of Two Pass Assembler... (My Best Ever C++ Program)


/**********************************************/
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
/**********************************************/
int i,j,k,cnt,l,kahihi,add,flag,regular,addr,litno;
FILE *pk;
int flag1,rest,symcnt,litaddress,purvichakahihi,consno;
int poolind[30],pool,poolpresence[30],sadhalen,attempt;
char p,str[200][30],temp[2],a[5],sadha[10],s1[10],s2[3],q;
//char typ[3],val[3],address[5],op[3],clas[3],mnemonic[7];
int endadd,poi,ptr,s1add;
// endadd is var for showing address of END mnemonic...
// litaddress is used for lit address determination...
// purvichakahihi is the variable used for getting last correct position of kahihi...
/*********************************************/
class mn
{
public:
char mnemonic[200][7],clas[200][3],op[200][3],address[200][5];
char typ[200][3],val[200][3];
int count[200];
mn()
{
for(kahihi=0;kahihi<200;kahihi++)
{
strcpy(mnemonic[i],NULL);
strcpy(clas[i],NULL);
strcpy(op[i],NULL);
strcpy(address[i],NULL);
strcpy(typ[i],NULL);
strcpy(val[i],NULL);
count[kahihi]=0;
}
}
};
mn MOT,resultMOT,resultLIT,restpartlit,resultSYM,firstofline;
void declare()
{
// Declareation of all mnemonics.....

strcpy(MOT.mnemonic[0],"STOP");
strcpy(MOT.mnemonic[1],"ADD");
strcpy(MOT.mnemonic[2],"SUB");
strcpy(MOT.mnemonic[3],"MULT");
strcpy(MOT.mnemonic[4],"MOVER");
strcpy(MOT.mnemonic[5],"MOVEM");
strcpy(MOT.mnemonic[6],"COMP");
strcpy(MOT.mnemonic[7],"BC");
strcpy(MOT.mnemonic[8],"DIV");
strcpy(MOT.mnemonic[9],"READ");
strcpy(MOT.mnemonic[10],"PRINT");
strcpy(MOT.mnemonic[11],"START");
strcpy(MOT.mnemonic[12],"END");
strcpy(MOT.mnemonic[13],"ORIGIN");
strcpy(MOT.mnemonic[14],"EQU");
strcpy(MOT.mnemonic[15],"LTORG");
strcpy(MOT.mnemonic[16],"DS");
strcpy(MOT.mnemonic[17],"DC");
strcpy(MOT.mnemonic[18],"AREG");
strcpy(MOT.mnemonic[19],"BREG");
strcpy(MOT.mnemonic[20],"CREG");
strcpy(MOT.mnemonic[21],"EQ");
strcpy(MOT.mnemonic[22],"LT");
strcpy(MOT.mnemonic[23],"GT");
strcpy(MOT.mnemonic[24],"LE");
strcpy(MOT.mnemonic[25],"GE");
strcpy(MOT.mnemonic[26],"NE");
strcpy(MOT.mnemonic[27],"ANY");

// declaration of all classes of mnemonics....

strcpy(MOT.clas[0],"IS");
strcpy(MOT.clas[1],"IS");
strcpy(MOT.clas[2],"IS");
strcpy(MOT.clas[3],"IS");
strcpy(MOT.clas[4],"IS");
strcpy(MOT.clas[5],"IS");
strcpy(MOT.clas[6],"IS");
strcpy(MOT.clas[7],"IS");
strcpy(MOT.clas[8],"IS");
strcpy(MOT.clas[9],"IS");

strcpy(MOT.clas[10],"IS");
strcpy(MOT.clas[11],"AD");
strcpy(MOT.clas[12],"AD");
strcpy(MOT.clas[13],"AD");
strcpy(MOT.clas[14],"AD");
strcpy(MOT.clas[15],"AD");
strcpy(MOT.clas[16],"DL");
strcpy(MOT.clas[17],"DL");
strcpy(MOT.clas[18],"RG");
strcpy(MOT.clas[19],"RG");
strcpy(MOT.clas[20],"RG");
strcpy(MOT.clas[21],"CC");
strcpy(MOT.clas[22],"CC");
strcpy(MOT.clas[23],"CC");
strcpy(MOT.clas[24],"CC");
strcpy(MOT.clas[25],"CC");
strcpy(MOT.clas[26],"CC");
strcpy(MOT.clas[27],"CC");

// declaration of all opcodes of mnemonics....

strcpy(MOT.op[0],"00");
strcpy(MOT.op[1],"01");
strcpy(MOT.op[2],"02");
strcpy(MOT.op[3],"03");
strcpy(MOT.op[4],"04");
strcpy(MOT.op[5],"05");
strcpy(MOT.op[6],"06");
strcpy(MOT.op[7],"07");
strcpy(MOT.op[8],"08");
strcpy(MOT.op[9],"09");
strcpy(MOT.op[10],"10");
strcpy(MOT.op[11],"01");
strcpy(MOT.op[12],"02");
strcpy(MOT.op[13],"03");
strcpy(MOT.op[14],"04");
strcpy(MOT.op[15],"05");
strcpy(MOT.op[16],"01");
strcpy(MOT.op[17],"02");
strcpy(MOT.op[18],"01");
strcpy(MOT.op[19],"02");
strcpy(MOT.op[20],"03");
strcpy(MOT.op[21],"01");
strcpy(MOT.op[22],"02");
strcpy(MOT.op[23],"03");
strcpy(MOT.op[24],"04");
strcpy(MOT.op[25],"05");
strcpy(MOT.op[26],"06");
strcpy(MOT.op[27],"07");
}
//MOT.mnemonic;
/*********************************************/
void findwordtype()
{
for(i=0;i<29;i++)
{
if(strcmp(str[j-1],MOT.mnemonic[i])==0)
{
// function is for getting starting address of prg...
if(flag==1)
{
add=atoi(str[j-2]);
flag=2;
}
if(strcmp(str[j-1],MOT.mnemonic[11])==0 && flag!=1)
flag=1;

// compare both strings & if matches then make reslut MOT
strcpy(resultMOT.mnemonic[k],MOT.mnemonic[i]);
strcpy(resultMOT.clas[k],MOT.clas[i]);
strcpy(resultMOT.op[k],MOT.op[i]);
strcpy(restpartlit.clas[rest],MOT.clas[i]);
strcpy(restpartlit.op[rest],MOT.op[i]);
resultMOT.count[k]=k;
addr=add+cnt;
itoa(addr,MOT.address[i],10);
strcpy(resultMOT.address[k],MOT.address[i]);
k++;
flag1=1;// used for checking lit
goto baher;
}
flag1=0;
}
baher:
addr=add+cnt;
itoa(addr,a,10);
if(q=='\n' && p==' ')
{
strcpy(firstofline.mnemonic[rest],str[j-1]);
firstofline.count[rest]=rest;
strcpy(firstofline.address[rest],a);
}
strcpy(restpartlit.mnemonic[rest],str[j-1]);
restpartlit.count[rest]=rest;
strcpy(restpartlit.address[rest],a);
rest++;
}
/*********************************************/
void getwordsfromfile()
{
pk=fopen("c:\\fil.txt","r");
j=0;
flag1=0;
q='\0';
for(i=0;i<200;i++)
strcpy(str[i],NULL);

while(!feof(pk))
{
regular=j;
p=fgetc(pk);
if(p!=' ' || p!='\n')
{
if(p!=',' && p!=' ' && p!='\n')
{
temp[0]=p;
temp[1]='\0';
strcat(str[j],temp);
}
}
//checking word.....
// increment wor number....
if(p==' ' || p=='\n' || p==',')
j++;

if(regular!=j)
findwordtype();
if(p=='\n')
cnt=cnt+1;
if(p==' ' || p=='\n' || p==',')
q=p;
}
}
/**********************************************/
void resultofMOT()
{
cout<<"\nMOT Table is ==> ";
cout<<"\n\n\tMN\tCLASS\tOPCODE\tADDRESS";
for(i=0;i<k;i++)
cout<<"\n\n\t"<<resultMOT.count[i]<<"\t"<<resultMOT.mnemonic[i]<<".\t"<<resultMOT.clas[i]<<"\t"<<resultMOT.op[i]<<"\t"<<resultMOT.address[i];
getch();
clrscr();
cout<<"\nMOT Table is ==> ";
cout<<"\n\n\tMN\tCLASS\tOPCODE\tADDRESS";
for(i=0;i<rest;i++)
cout<<"\n\t"<<resultMOT.count[i]<<"\t"<<firstofline.mnemonic[i]<<".\t"<<firstofline.address[i];
}
/**********************************************/
void findliteral()
{
for(i=0;i<rest;i++)
{
if(strcmp(restpartlit.mnemonic[i],"LTORG")==0 || strcmp(restpartlit.mnemonic[i],"END")==0)
{
poolind[pool]=litno;
if(strcmp(restpartlit.mnemonic[i],"END")==0)
litaddress=atoi(restpartlit.address[i])+1;
else
litaddress=atoi(restpartlit.address[i]);
for(kahihi=purvichakahihi;kahihi<rest;kahihi++)
{
if(restpartlit.mnemonic[kahihi][0]=='=')
{
strcpy(resultLIT.mnemonic[litno],restpartlit.mnemonic[kahihi]);
itoa(litaddress,restpartlit.address[kahihi],10);
strcpy(restpartlit.clas[kahihi],"L");
itoa(litno,restpartlit.op[kahihi],10);
itoa(litaddress,resultLIT.address[litno],10);
litaddress++;
resultLIT.count[litno]=litno;
litno++;
poolpresence[pool]++;
purvichakahihi=kahihi+1;
}
}
pool++;
}
}
}
/**********************************************/
void literaltable()
{
getch();
clrscr();
findliteral();
cout<<"\n\n\tRemaining => ";
for(i=0;i<rest;i++)
cout<<"\n\t"<<restpartlit.count[i]<<"\t"<<restpartlit.mnemonic[i]<<"\t"<<restpartlit.address[i]<<"\t"<<restpartlit.clas[i]<<"\t"<<restpartlit.op[i];
cout<<"\nLiteral Table is ==> ";
for(i=0;i<litno;i++)
cout<<"\n\n\t"<<resultLIT.count[i]<<"\t"<<resultLIT.mnemonic[i]<<"\t"<<resultLIT.address[i];

}
/**********************************************/
void findafterequ()
{
for(endadd=0;endadd<rest;endadd++)
{
if(strcmp(restpartlit.mnemonic[endadd],"EQU")==0 && strcmp(restpartlit.mnemonic[endadd-1],firstofline.mnemonic[kahihi])==0)
{
strcpy(sadha,restpartlit.mnemonic[endadd+1]);
ptr=0;
while(ptr!=strlen(sadha))
{
if(attempt==1)
{
temp[0]=sadha[ptr];
temp[1]='\0';
strcat(s2,temp);
sadhalen++;
}
if(sadha[ptr]=='+')
{
sadhalen=0;
attempt=1;
}
if(attempt!=1)
{
temp[0]=sadha[ptr];
temp[1]='\0';
strcat(s1,temp);
sadhalen++;
}
ptr++;
}
attempt=0;
int sadhaint=atoi(s2);
for(int ii=0;ii<rest;ii++)
{
if(strcmp(firstofline.mnemonic[ii],s1)==0)
{
s1add=atoi(firstofline.address[ii]);
goto neeche;
}
}
neeche:
s1add=s1add+sadhaint;
itoa(s1add,firstofline.address[kahihi],10);
}
}
}
/**********************************************/
void findsymbol()
{
getch();
clrscr();
for(i=0;i<rest;i++)
{
kahihi=i;
for(poi=0;poi<29;poi++)
{
if(strcmp(firstofline.mnemonic[kahihi],MOT.mnemonic[poi])==0)
goto khali;
}
strcpy(resultSYM.mnemonic[symcnt],firstofline.mnemonic[kahihi]);
findafterequ();
strcpy(resultSYM.address[symcnt],firstofline.address[kahihi]);
strcpy(restpartlit.clas[kahihi],"S");
itoa(symcnt,restpartlit.op[kahihi],10);
resultSYM.count[symcnt]=symcnt;
symcnt++;
khali:
}
for(i=0;i<rest;i++)
{
for(kahihi=0;kahihi<symcnt;kahihi++)
if(strcmp(restpartlit.mnemonic[i],resultSYM.mnemonic[kahihi])==0)
{
strcpy(restpartlit.clas[i],"S");
itoa(symcnt,restpartlit.op[kahihi],10);
}
}
iner:
cout<<"\n\n\tSYMTABLE => ";
for(i=0;i<symcnt;i++)
cout<<"\n\n\t"<<resultSYM.count[i]<<"\t"<<resultSYM.mnemonic[i]<<"\t"<<resultSYM.address[i]<<"\t"<<resultSYM.op[i];
}
/**********************************************/
void pooltable()
{
getch();
clrscr();
cout<<"\n\n\tPool Table => ";
for(i=0;i<pool;i++)
{
if(poolpresence[i]!=0)
cout<<"\n\t"<<i<<"\t"<<poolind[i];
}
}
/**********************************************/
void output()
{
getch();
clrscr();
cout<<"\n\n\tOutput is  => ";
int varcnt;
varcnt=0;
endadd=atoi(restpartlit.address[0]);
for(i=0;i<restpartlit.count[rest-1];i++)
{
if(atoi(restpartlit.address[i])!=endadd)
{
if(strcmp(restpartlit.clas[i],"S")==0)
i++;
if(strcmp(restpartlit.op[i],NULL)!=0 && strcmp(restpartlit.op[i],NULL)!=0)
cout<<"\n\t("<<restpartlit.clas[i]<<","<<restpartlit.op[i]<<")";
else
cout<<"\n\t";
varcnt++;
}
else
{
if(varcnt==1)
{
if(strcmp(restpartlit.clas[i],"AD")==0)
{

}
else
{
if(strcmp(restpartlit.clas[i],"RG")==0)
cout<<"\t("<<restpartlit.clas[i]<<","<<restpartlit.op[i]<<")";
else
cout<<"\t(RG,00)";
}
}
else
cout<<"\t("<<restpartlit.clas[i]<<","<<restpartlit.op[i]<<")";
varcnt=0;

}
endadd=atoi(restpartlit.address[i]);
}
}
/**********************************************/
void hi()
{
getch();
clrscr();
cout<<"\n\n\tRemaining => ";
for(i=0;i<rest;i++)
cout<<"\n\t"<<restpartlit.count[i]<<"\t"<<restpartlit.mnemonic[i]<<"\t"<<restpartlit.address[i]<<"\t"<<restpartlit.clas[i]<<"\t"<<restpartlit.op[i];
}
/**********************************************/
void constants()
{
getch();
clrscr();
for(i=0;i<rest;i++)
{
if(strcmp(restpartlit.clas[i],NULL)==0)
{
for(kahihi=0;kahihi<symcnt;kahihi++)
if(strcmp(restpartlit.mnemonic[i],resultSYM.mnemonic[kahihi])==0){goto AA;}
AA: if(kahihi==symcnt)
{
strcpy(restpartlit.clas[i],"C");
itoa(consno,restpartlit.val[i],10);
consno++;
}
}
}
}
/**********************************************/
void main()
{
clrscr();
cout<<"Input to Assembler is => \n";
pk=fopen("c:\\fil.txt","r");
for(i=0;i<30;i++)
{
poolind[i]=0;
poolpresence[i]=0;
}
strcpy(sadha,NULL); // this string is used for defining string after EQU stmt...
strcpy(s1,NULL); // this string is used for defining string of 1st part after EQU stmt & before '+'...
strcpy(s2,NULL); // this string is used for defining string of 2nd part after EQU stmt & after '+'...
s1add=0;
attempt=0;
// it is the value used to detect attemption of '+' in file...
pool=0;
// it is the value used to measure pool values in file...
consno=0;
// it is the value used to measure cons values in file...
symcnt=0;
// it is the value used to measure rest values in file...
rest=0;
// it is the value used to measure rest values in file...
litno=0;
// it is the value used to measure no. of literals in file...
regular=0;
// it is the value used to measure all the words less than j parrameter in file...
k=0; // k is parameter used for counting no. of MOT records...
cnt=-1;// cnt is parameter used for measuring row number for address purpose...
fclose(pk);
clrscr();
declare();
getwordsfromfile();
findwordtype();
resultofMOT();
literaltable();
pooltable();
findsymbol();
constants();
hi();
output();
getch();
}
/**********************************************/
/******************** C:/fil.txt**********************/
START 1000

READ N
MOVER BREG,='1'
MOVEM BREG,TERM
L MULT BREG,TERM
MOVER CREG,TERM
COMP CREG,N
BC LE,AGAIN
NEXT EQU L+1
MOVEM B,RESULT
LTORG
PRINT RESULT
STOP
N DS 1
RESULT DS 20
TERM DS 1
END
/**********************************************/

No comments:

Post a Comment