Sunday, April 5, 2009

source code

#include
#include
#include
#include
void main()
{
int i,j,k;
char a,b,c,temp[10],a1,b1,c1,end[9];
FILE *fp,*p1;
fp=fopen("op.txt","r");
p1=fopen("output.txt","w");
while(!feof(fp))
{
fscanf(fp,"%s",temp);
if(strcmp(temp,"MOV")==0)
{
fprintf(p1,"%s\t",temp);
a=fgetc(fp);
a=fgetc(fp);
b=fgetc(fp);
b=fgetc(fp);
fprintf(p1,"%c",a);
fprintf(p1,"%c",',');
fprintf(p1,"%c\n",b);
ll:fscanf(fp,"%s",temp);
if(strcmp(temp,"end")==0)
exit(0);
if(strcmp(temp,"MOV")==0)
{
a1=fgetc(fp);
a1=fgetc(fp);
b1=fgetc(fp);
b1=fgetc(fp);
if(((a1==a)&&(b1==b)||(a1==b)&&(b1==a))==0)
{
fprintf(p1,"%s\t",temp);
fprintf(p1,"%c",a1);
fprintf(p1,"%c",',');
fprintf(p1,"%c\n",b1);
}
}
else
{
a=fgetc(fp);
a=fgetc(fp);
b=fgetc(fp);
b=fgetc(fp);
if(((strcmp(temp,"ADD")==0)&&(a!='0'))||((strcmp(temp,"MUL")==0)&&(a!='1'))||((strcmp(temp,"DIV")==0)&&(a!='1')))
{
fprintf(p1,"%s\t",temp);
fprintf(p1,"%c",a);
fprintf(p1,"%c",',');
fprintf(p1,"%c\n",b);
}
}
a=a1;
b=b1;
if(feof(fp)!=0)
exit(0);
else
goto ll;
}
}
fclose(fp);
fclose(p1);
}


INPUT:

OP.TXT

MOV a,R
ADD b,R
MOV R,a
MOV a,R
ADD d,R
MUL 1,R
DIV 1,R
MOV c,R
MOV R,c
END


OUTPUT:

OUTPUT.TXT

MOV a,R
ADD b,R
MOV R,a
ADD d,R
MOV c,R