You are on page 1of 6

#include<iostream.

h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<fstream.h>
int i,j,instr_no,flag,found,p;
struct REGS
{
int opcode;
char class1[20],instr[20];
}reg[4]={{1,"REGS","AREG"},{2,"REGS","BREGS"},{3,"REGS","CREGS"},{4,"REGS","DREG
S"}};
struct Is
{
int opcode;
char class1[20],instr[20];
}is[11]={{0,"IS","STOP"},{1,"IS","ADD"},{2,"IS","SUB"},{3,"IS","MULT"},{4,"IS","
MOVER"},{5,"IS","MOVEM"},{6,"IS","COMP"},{7,"IS","BC"},{8,"IS","DIV"},{9,"IS","R
EAD"},{10,"IS","PRINT"}};
struct AD
{
int opcode;
char class1[20],instr[20];
}ad[6]={{0,"AD","START"},{1,"AD","END"},{2,"AD","ORIGIN"},{3,"AD","EQU"},{4,"AD"
,"LTORG"}};
struct DC
{
int opcode;
char class1[20],instr[20];
}dc[2]={{0,"DC","DS"},{1,"DS","DC"}};

int isno(char word[])


{
i=0,flag=0;
while(word[i]!=NULL)
{
if(word[i]>=48&&word[i]<=57)
{
flag=1;
}
else
{
flag=0;

break;
}
i++;
}
if(flag==1)
{
return 1;
}
else
{
return -1;
}
}
int search(char word[],int instr_no)
{
flag=0;
for(i=0;i<11;i++)
{
if(instr_no==0)
{
if(strcmp(ad[i].instr,word)==0)
{
flag=1;
goto L;
}
if(strcmp(is[i].instr,word)==0)
{
flag=2;
goto L;
}
}
else if(instr_no==1)
{
if(strcmp(reg[i].instr,word)==0)
{
flag=1;
goto L;
}
}
}
L:

if(flag==1)
{
return i;
}
else
{
return -1;
}

void main()
{
char word[20],ch,mat[10][10][20],literal[5][3][20],str_opc[3],index[10];
int i,k,n,m,instr_no,a,b,cnt;
ifstream f1;
clrscr();
f1.open("instr.txt");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
strcpy(mat[i][j],"-1");
strcpy(literal[i][j],"-1");
}
}
char str[10];
i=0,m=0,n=-1,a=0,b=0,cnt=0;
instr_no=0;
int check_no=0,l;
while(!f1.eof())
{
f1.get(ch);
if(ch!=32&&ch!='\n'&&ch!=44)
{
word[i]=ch;
i++;
}
else if(ch==32||ch==44)
{
word[i]=NULL;
p=strlen(word);
if(word[p-1]==':')
{
i=0;
goto LAST;
}
n++;

check_no=isno(word);
if(check_no==-1)
{
found=0;
for(i=0;i<5;i++)
{
if(strcmp(ad[i].instr,word)==0)
{
found=1;
break;
}
}
if(found!=1)
{
k=search(word,instr_no);
strcpy(str,"(");
if(instr_no==1)
{
strcat(str,reg[k].class1);
strcat(str," , ");
itoa(reg[k].opcode,str_opc, 10);
strcat(str,str_opc);
}
else if(instr_no==0)
{
strcat(str,is[k].class1);
strcat(str," , ");
itoa(is[k].opcode,str_opc, 10);
strcat(str,str_opc);
}
strcat(str,")");
}
else
{
strcpy(str,"(");
strcat(str,ad[i].class1);
strcat(str," , ");
itoa(ad[i].opcode,str_opc, 10);
strcat(str,str_opc);
strcat(str,")");
}
}
else
{
strcpy(str,"(CONS)");
}
strcpy(mat[m][n],"-1");
strcpy(mat[m][n],str);

i=0;
instr_no++;
}
else if(ch=='\n')
{
n++;
word[i]=NULL;
check_no=isno(word);
if(check_no==-1)
{
if(word[0]=='=')
{
strcpy(str,"(");
if(instr_no==2)
{
int l;
for(l=1;l<10;l++)
{
str[l]=NULL;
}
str[1]=word[2];
}
strcat(str,")");
}
else if(instr_no==0)
{
for(i=0;i<5;i++)
{
if(strcmp(ad[i].instr,word)==0)
{
break;
}
}
int l1,l2;
l1=strlen(str);
strcpy(str,"(");
strcat(str,ad[i].class1);
strcat(str,",");
itoa(ad[i].opcode,str_opc, 10);
strcat(str,str_opc);
strcat(str,")");
l2=strlen(str);
}
else
{
strcpy(literal[a][1],word);
itoa(cnt,index,10);
strcpy(literal[a][0],index);

strcpy(str,"(l , ");
strcat(str,literal[a][0]);
strcat(str,")");
a++;
cnt++;
}
}
else
{
strcpy(str,"(CONS)");
}
strcpy(mat[m][n],"-1");
strcpy(mat[m][n],str);
i=0;

m++;

instr_no=0;

}
LAST:
}
for(i=0;i<4;i++)
{
cout<<"\n\n";
for(j=0;j<3;j++)
{
if(strcmp(mat[i][j],"-1")!=0)
{
cout<<"\t"<<mat[i][j];
}
}
}
getch();
}

n=-1;

You might also like