Macro Pass 1 - C code
// MACRO PASS 1- C CODE
//files included
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct mntab
{
char name[8];
int add;
int argcnt;
}mnt[5];
struct mdtab
{
char def[40];
}mdt[20];
struct ala
{
char formal[10];
char actual[10];
}ala[20];
//to be discussed later
int imdt=0; //variable used for storing address..
int imnt=0;
int iala=0;
void main()
{
char fname[10],buffer[80],s1[8],s2[8],s3[16],s4[16],ref[10];
int i,j,k,cnt,flag;
//file pointers created
FILE *fin,*fout,*fmnt,*fmdt,*fala;
printf("\n Enter file name(for input code)");
scanf("%s",&fname);
fin=fopen(fname,"r");
fout=fopen("output.txt","w+");
while(fgets(buffer,40,fin)!=0)
{
cnt=sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
if(strcmp(s1,"MACRO")!=0)
fputs(buffer,fout);
else
{
printf("\n Macro found");
printf("\n Processing macro name table");
fgets(buffer,40,fin);
for(i=0;i<40;i++)
if(buffer[i]==',')
buffer[i]=' ';
//cnt baghaychay
cnt=sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
i=(s1[1]-65)%5; //<-----
strcpy(mnt[i].name,s1);
mnt[i].add=imdt;
mnt[i].argcnt=cnt-1;
printf("\n processing arguments list");
if(cnt>1)
{
j=(s2[1]-65)%20;
strcpy(ala[j].formal,s2);
}
if(cnt>2)
{
flag=0;
for(i=0;i<16;i++)
j=(s3[1]-65)%20;
for(i=0;i<16;i++)
{
if(s3[i]=='=')
{
s3[i]=' ';
sscanf(s3,"%s%s",ref,s3);
flag=1;
break;
}
}
if(flag==1)
{
strcpy(ala[j].formal,ref);
strcpy(ala[j].actual,s3);
}
else
strcpy(ala[j].formal,s3);
}
if(cnt>3)
{
flag=0;
j=(s4[1]-65)%20;
for(i=0;i<16;i++)
{
if(s4[i]=='=')
{
s4[i]=' ';
sscanf(s4,"%s%s",ref,s4);
flag=1;
}
}
if(flag==1)
{
strcpy(ala[j].formal,ref);
strcpy(ala[j].actual,s4);
}
else
strcpy(ala[j].formal,s4);
}
printf("processing mdt");
sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
i=(s2[1]-65)%20;
j=(s3[1]-65)%20;
k=(s4[1]-65)%20;
sprintf(buffer,"%s%d%d%d",s1,i,j,k);
printf("\n\t %d",imdt);
while(1)
{
flag=0;
strcpy(s1,"");
strcpy(s2,"");
strcpy(s3,"");
strcpy(s4,"");
fgets(buffer,40,fin);
for(i=0;i<40;i++)
if(buffer[i]==',')
buffer[i]=' ';
sscanf(buffer,"%s%s%s",s1,s2,s3);
if(s2[0]=='&')
{
j=(s2[1]-65)%20;
flag=1;
sprintf(buffer,"%s%d%s",s1,j,s3);
}
if(s3[0]=='&')
{
k=(s3[1]-65)%20;
if(flag==1)
sprintf(buffer,"%s%d%d",s1,j,k);
else
sprintf(buffer,"%s%s%d",s1,s2,k);
}
if(strcmp(s1,"MEND")!=0)
strcpy(mdt[imdt++].def,buffer);
else
{
strcpy(mdt[imdt++].def,"MEND");
break;
}
printf("\n\t%d",imdt);
}
}
}
fseek(fin,0,0);
fseek(fout,0,0);
printf("\n\t=====IP=====");
while(fgets(buffer,40,fin)!=0)
puts(buffer);
fclose(fin);
printf("\n\t Output Of Pass I ");
while(fgets(buffer,40,fin)!=0)
puts(buffer);
fclose(fout);
fmnt=fopen("mnt.text","w");
printf("\n\t MNT == ");
for(i=0;i<5;i++)
{
printf("\n\t%s\t%d\t%d",mnt[i].name,mnt[i].add,mnt[i].argcnt);
fprintf(fmnt,"\n\t%s\t%d\t%d\n",mnt[i].name,mnt[i].add,mnt[i].argcnt);
}
fclose(fmnt);
fmdt=fopen("mdt.txt","w");
printf("\n\t=====MDT=====");
for(i=0;i<imdt;i++)
{ printf("\n%d\t%s",i,mdt[i].def);
fprintf(fmdt,"%s\n",mdt[i].def);
}
fclose(fmdt);
fala=fopen("ala.txt","w");
printf("\n\t====ala====");
for(i=0;i<20;i++)
{
printf("\n\t%s\t%s",ala[i].formal,ala[i].actual);
fprintf(fala,"\n\t%s\t%s",ala[i].formal,ala[i].actual);
}
fclose(fala);
getch();
}
//files included
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct mntab
{
char name[8];
int add;
int argcnt;
}mnt[5];
struct mdtab
{
char def[40];
}mdt[20];
struct ala
{
char formal[10];
char actual[10];
}ala[20];
//to be discussed later
int imdt=0; //variable used for storing address..
int imnt=0;
int iala=0;
void main()
{
char fname[10],buffer[80],s1[8],s2[8],s3[16],s4[16],ref[10];
int i,j,k,cnt,flag;
//file pointers created
FILE *fin,*fout,*fmnt,*fmdt,*fala;
printf("\n Enter file name(for input code)");
scanf("%s",&fname);
fin=fopen(fname,"r");
fout=fopen("output.txt","w+");
while(fgets(buffer,40,fin)!=0)
{
cnt=sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
if(strcmp(s1,"MACRO")!=0)
fputs(buffer,fout);
else
{
printf("\n Macro found");
printf("\n Processing macro name table");
fgets(buffer,40,fin);
for(i=0;i<40;i++)
if(buffer[i]==',')
buffer[i]=' ';
//cnt baghaychay
cnt=sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
i=(s1[1]-65)%5; //<-----
strcpy(mnt[i].name,s1);
mnt[i].add=imdt;
mnt[i].argcnt=cnt-1;
printf("\n processing arguments list");
if(cnt>1)
{
j=(s2[1]-65)%20;
strcpy(ala[j].formal,s2);
}
if(cnt>2)
{
flag=0;
for(i=0;i<16;i++)
j=(s3[1]-65)%20;
for(i=0;i<16;i++)
{
if(s3[i]=='=')
{
s3[i]=' ';
sscanf(s3,"%s%s",ref,s3);
flag=1;
break;
}
}
if(flag==1)
{
strcpy(ala[j].formal,ref);
strcpy(ala[j].actual,s3);
}
else
strcpy(ala[j].formal,s3);
}
if(cnt>3)
{
flag=0;
j=(s4[1]-65)%20;
for(i=0;i<16;i++)
{
if(s4[i]=='=')
{
s4[i]=' ';
sscanf(s4,"%s%s",ref,s4);
flag=1;
}
}
if(flag==1)
{
strcpy(ala[j].formal,ref);
strcpy(ala[j].actual,s4);
}
else
strcpy(ala[j].formal,s4);
}
printf("processing mdt");
sscanf(buffer,"%s%s%s%s",s1,s2,s3,s4);
i=(s2[1]-65)%20;
j=(s3[1]-65)%20;
k=(s4[1]-65)%20;
sprintf(buffer,"%s%d%d%d",s1,i,j,k);
printf("\n\t %d",imdt);
while(1)
{
flag=0;
strcpy(s1,"");
strcpy(s2,"");
strcpy(s3,"");
strcpy(s4,"");
fgets(buffer,40,fin);
for(i=0;i<40;i++)
if(buffer[i]==',')
buffer[i]=' ';
sscanf(buffer,"%s%s%s",s1,s2,s3);
if(s2[0]=='&')
{
j=(s2[1]-65)%20;
flag=1;
sprintf(buffer,"%s%d%s",s1,j,s3);
}
if(s3[0]=='&')
{
k=(s3[1]-65)%20;
if(flag==1)
sprintf(buffer,"%s%d%d",s1,j,k);
else
sprintf(buffer,"%s%s%d",s1,s2,k);
}
if(strcmp(s1,"MEND")!=0)
strcpy(mdt[imdt++].def,buffer);
else
{
strcpy(mdt[imdt++].def,"MEND");
break;
}
printf("\n\t%d",imdt);
}
}
}
fseek(fin,0,0);
fseek(fout,0,0);
printf("\n\t=====IP=====");
while(fgets(buffer,40,fin)!=0)
puts(buffer);
fclose(fin);
printf("\n\t Output Of Pass I ");
while(fgets(buffer,40,fin)!=0)
puts(buffer);
fclose(fout);
fmnt=fopen("mnt.text","w");
printf("\n\t MNT == ");
for(i=0;i<5;i++)
{
printf("\n\t%s\t%d\t%d",mnt[i].name,mnt[i].add,mnt[i].argcnt);
fprintf(fmnt,"\n\t%s\t%d\t%d\n",mnt[i].name,mnt[i].add,mnt[i].argcnt);
}
fclose(fmnt);
fmdt=fopen("mdt.txt","w");
printf("\n\t=====MDT=====");
for(i=0;i<imdt;i++)
{ printf("\n%d\t%s",i,mdt[i].def);
fprintf(fmdt,"%s\n",mdt[i].def);
}
fclose(fmdt);
fala=fopen("ala.txt","w");
printf("\n\t====ala====");
for(i=0;i<20;i++)
{
printf("\n\t%s\t%s",ala[i].formal,ala[i].actual);
fprintf(fala,"\n\t%s\t%s",ala[i].formal,ala[i].actual);
}
fclose(fala);
getch();
}
0 comments:
Post a Comment