#include
#include
#include
#include
#include
#include
int hexdec(char m[10])
{
char ds[10];
strcpy(ds,m);
char *s=strrev(ds);
int l=strlen(s);
char d[3]="00";
int i=0,sum=0;
while(i
if(s[i]>=48 && s[i]<=57)
d[1]=s[i];
else
{
if(s[i]=='A')
strcpy(d,"10");
else if(s[i]=='B')
strcpy(d,"11");
else if(s[i]=='C')
strcpy(d,"12");
else if(s[i]=='D')
strcpy(d,"13");
else if(s[i]=='E')
strcpy(d,"14");
else if(s[i]=='F')
strcpy(d,"15");
}
// printf("-%s- ",d);
sum=sum+ (pow(16,i)*atof(d));
// printf("<%d> ",sum);
i++;
}
return sum;
}
PASS.C
#include "FUNCT.CPP"
int locctr=0;
int preloc=0;
int startAdd=0;
int proLength;
class Statement
{
public:
char label[20];
char opcode[20];
char operand[20];
void upper();
};
void Statement::upper()
{
strcpy(label,strupr(label));
strcpy(opcode,strupr(opcode));
strcpy(operand,strupr(operand));
}
class Intermediate:public Statement
{
public:
char location[20];
char label[20];
char opcode[20];
char operand[20];
void insert(int a,char b[],char c[],char d[])
{
char *st;
sprintf(st,"%x",a);
strcpy(location,st);
strcpy(label,b);
strcpy(opcode,c);
strcpy(operand,d);
cout<
};
class Optab
{
public:
char opcode[20];
char opvalue[10];
friend int search(char *s);
};
Optab Op[]={ {"ADD","18"},{"SUB","1C"} };
int search(char s[])
{
for(int i=0;i<2;i++)
{
if( stricmp(Op[i].opcode,s) ==0 )
{
return 1;
}
}
return 0;
}
class Symtab
{
public:
char label[20][20];
int address[20];
int top;
Symtab()
{
top=-1;
}
int search(char *sp)
{
for(int i=0;i<=top;i++)
{
if(stricmp(sp,label[i])==0)
{
return 1;
}
}
return 0;
}
void insert(char *lab,int ad)
{
top++;
strcpy(label[top],lab);
address[top]=ad;
}
};
void readprogram()//function read program
{
ofstream f1;
Statement s;
f1.open("source.txt",ios::binary);
cout<<"Type your program: \n";
cin>>s.label>>s.opcode>>s.operand;
while( stricmp(s.opcode,"END") !=0 )
{
s.upper();
f1.write( (char*)&s,sizeof(Statement));
cin>>s.label>>s.opcode>>s.operand;
s.upper();
}
f1.write( (char*)&s,sizeof(Statement));
f1.close();
}
void printprogram()//function print program
{
ifstream f1;
Statement s;
f1.open("source.txt",ios::binary);
cout<<"\nYour program: \n";
while(f1.read( (char*)&s,sizeof(Statement)))
{
cout<
f1.close();
}
void pass1()
{
ifstream f2;
ofstream f3;
Statement s;
Symtab S;
Intermediate I;
f2.open("source.txt",ios::binary);
f3.open("intermediate.txt",ios::binary);
f2.read( (char*)&s,sizeof(Statement));
if( stricmp(s.opcode,"START")==0 || !f2.eof() )
{
startAdd=hexdec(s.operand);
locctr=startAdd;
preloc=startAdd;
I.insert(preloc,s.label,s.opcode,s.operand);
// f3.write( (char*)&I,sizeof(Intermediate) );
f2.read( (char*)&s,sizeof(Statement));
}
while( !stricmp(s.opcode,"END") && !f2.eof())
{
if( stricmp(s.label,"-")!=0 )
{
if( S.search(s.label)==1)
cout<<"Error : duplicate label \n";
else
S.insert(s.label,locctr);
}
if( search(s.opcode)==1 )
{
locctr+=3;
}
else if( stricmp(s.opcode,"WORD")==0 )
{
locctr=locctr+3;
}
else if( stricmp(s.opcode,"RESW")==0 )
{
locctr=locctr+3*hexdec(s.operand);
}
else if( stricmp(s.opcode,"RESB")==0 )
{
locctr=locctr+hexdec(s.operand);
}
else if( stricmp(s.opcode,"BYTE")==0 )
{
locctr=locctr+strlen(s.operand)-3;
}
else
{
cout<<"Error : opcode not found \n"<
I.insert(preloc,s.label,s.opcode,s.operand);
// f3.write( (char*)&I,sizeof(Intermediate) );
f2.read( (char*)&s,sizeof(Statement));
preloc=locctr;
}//while end
I.insert(preloc,s.label,s.opcode,s.operand);
// f3.write( (char*)&I,sizeof(Intermediate) );
proLength=locctr-startAdd;
f2.close();
// f3.close();
}
void main()
{
clrscr();
readprogram();
printprogram();
// pass1();
getch();
return;
}
No comments:
Post a Comment