/*********************************************************************************
By => Ajit Medhekar... Xtream Kings... K.K.Wagh COE,Nashik...
Ajit's Blog :- http://ammedhekar.blogspot.com
*********************************************************************************
Design suitable data structure & Implement the pass-I of two pass MACRO processor...
*********************************************************************************/
# include <stdio.h>
# include<conio.h>
#include<string.h>
struct table1
{
char name[10];
int index,add_mdt;
}MNT[5];
struct table2
{
char des[30];
int add;
}MDT[30];
struct table3
{
char param[10],no[3];
}ALA[20];
/*
struct table1 MNT[5];
struct table2 MDT[30];
struct table3 ALA[20];
*/
FILE *fp1,*fp2;
char ch,word[20],word1[20],*mdtdes;
int newline=0;
void get_word();
char* num[10]={"#1","#2","#3","#4","#5","#6","#7","#8","#9"};
fpos_t pos;
void main()
{
int i,j,mnt=0,mdt=0,ala=0,k,t=0,com=1,flag,flag1=0;
char line[30],line1[30],line2[30];
clrscr();
fp1=fopen("c:\\a.asm","r");
fp2=fopen("c:\\b.txt","w");
i=0;
while(!feof(fp1))
{
fgetpos(fp1,&pos);
get_word();
if(strcmp(word,"MACRO")==0)
{
fgetpos(fp1,&pos);
get_word();
strcpy(MNT[mnt].name,word);
MNT[mnt].index=mnt;
MNT[mnt].add_mdt=mdt;
mnt++;
fsetpos(fp1,&pos);
i=0;
while(!feof(fp1))
{
ch=fgetc(fp1);
if(ch=='\n')
goto l9;
line1[i]=ch;
i++;
}
l9:line1[i]=NULL;
strcpy(MDT[mdt].des,line1);
MDT[mdt].add=mdt;
mdt++;
fsetpos(fp1,&pos); //find actual parameter
get_word();
do
{
get_word();
t=0;
if(strstr(word,"=")!=0)
{
while(word[t]!='=')
{
word1[t]=word[t];
t++;
}
}
else
strcpy(word1,word);
strcpy(ALA[ala].param,word1);
strcpy(ALA[ala].no,num[ala]);
ala++;
}while(ch!='\n');
}
else if(strcmp(word,"MEND")==0)
{
fsetpos(fp1,&pos);
get_word();
strcpy(MDT[mdt].des,word);
MDT[mdt].add=mdt;
mdt++;
}
else if(strcmp(word,"START")==0)
{
fsetpos(fp1,&pos);
while(!feof(fp1))
{
ch=fgetc(fp1);
fprintf(fp2,"%c",ch);
}
break;
}
else
{
//code in comment to display macro call as INCR &A,&B,®2
//otherwise macro call is like INCR #4,#5,#6
//
/* fsetpos(fp1,&pos);
get_word();
for(i=0;i<mnt;i++) //check nested call of macro
{
if(strcmp(word,MNT[i].name)==0)
{
fsetpos(fp1,&pos);
fgets(line2,30,fp1);
flag1=1;
}
}
if(flag1==1)
{
flag1=0;
strcpy(MDT[mdt].des,line2);
MDT[mdt].add=mdt;
mdt++;
}
else
*/ {
fsetpos(fp1,&pos);
get_word();
strcpy(line2,word);
com=1;
do
{
if(ch=='\n')
goto l1;
get_word();
strcat(line2," ");
flag=0;
for(i=0;i<ala;i++)
{
if(strcmp(word,ALA[i].param)==0)
{
strcpy(word1,ALA[i].no);
strcat(line2,word1);
flag=1;
break;
}
}
if(flag==0)
strcat(line2,word);
if(com==1)
{
strcat(line2,",");
com=0;
}
}while(ch!='\n');
l1:strcpy(MDT[mdt].des,line2);
MDT[mdt].add=mdt;
mdt++;
}
}
}
fclose(fp1);
fclose(fp2);
printf("\nMacro Name Table (MNT) :");
printf("\n\nIndex\tName\tAddress In MDT");
printf("\n-------------------------------");
for(j=0;j<mnt;j++)
printf("\n%d\t%s\t%d",MNT[j].index,MNT[j].name,MNT[j].add_mdt);
printf("\n\nArgument Array List (ALA) :");
printf("\n\nNo\tParam");
printf("\n-----------------");
for(j=0;j<ala;j++)
printf("\n%s\t%s",ALA[j].no,ALA[j].param);
printf("\n\nMacro Definition Table (MDT) :");
printf("\n\nAdd\tDef");
printf("\n-----------------");
for(j=0;j<mdt;j++)
printf("\n%d\t%s",MDT[j].add,MDT[j].des);
getch();
}
void get_word()
{
int i=0;
do
{
ch=getc(fp1);
if(ch!=' ' && ch!='\n' && ch!=',')
{
word[i]=ch;
i++;
}
if(ch==' ' || ch=='\n' || ch==',')
{
word[i]=NULL;
i=0;
break;
}
}while(!feof(fp1));
}
/********* OUTPUT ***********
Macro Name Table (MNT) :
Index Name Address In MDT
-------------------------------
0 INCR 0
1 DECR 5
Argument Array List (ALA) :
No Param
-----------------
#1 &X
#2 &Y
#3 ®1
#4 &A
#5 &B
#6 ®2
Macro Definition Table (MDT) :
Add Def
-----------------
0 INCR &X,&Y,®1=AREG
1 MOVER #3, #1
2 ADD #3, #2
3 MOVEM #3, #1
4 MEND
5 DECR &A,&B,®2=BREG
6 MOVER #6, #4
7 SUB #6, #5
8 MOVEM #6, #4
9 INCR #4,#5,#6
10 MEND
*/
/*********** a.asm ******************/
MACRO INCR &X,&Y,®1=AREG
MOVER ®1,&X
ADD ®1,&Y
MOVEM ®1,&X
MEND
MACRO DECR &A,&B,®2=BREG
MOVER ®2,&A
SUB ®2,&B
MOVEM ®2,&A
INCR &A,&B,®2
MEND
START 100
READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END
/*************** b.txt **************/
START 100READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END
By => Ajit Medhekar... Xtream Kings... K.K.Wagh COE,Nashik...
Ajit's Blog :- http://ammedhekar.blogspot.com
*********************************************************************************
Design suitable data structure & Implement the pass-I of two pass MACRO processor...
*********************************************************************************/
# include <stdio.h>
# include<conio.h>
#include<string.h>
struct table1
{
char name[10];
int index,add_mdt;
}MNT[5];
struct table2
{
char des[30];
int add;
}MDT[30];
struct table3
{
char param[10],no[3];
}ALA[20];
/*
struct table1 MNT[5];
struct table2 MDT[30];
struct table3 ALA[20];
*/
FILE *fp1,*fp2;
char ch,word[20],word1[20],*mdtdes;
int newline=0;
void get_word();
char* num[10]={"#1","#2","#3","#4","#5","#6","#7","#8","#9"};
fpos_t pos;
void main()
{
int i,j,mnt=0,mdt=0,ala=0,k,t=0,com=1,flag,flag1=0;
char line[30],line1[30],line2[30];
clrscr();
fp1=fopen("c:\\a.asm","r");
fp2=fopen("c:\\b.txt","w");
i=0;
while(!feof(fp1))
{
fgetpos(fp1,&pos);
get_word();
if(strcmp(word,"MACRO")==0)
{
fgetpos(fp1,&pos);
get_word();
strcpy(MNT[mnt].name,word);
MNT[mnt].index=mnt;
MNT[mnt].add_mdt=mdt;
mnt++;
fsetpos(fp1,&pos);
i=0;
while(!feof(fp1))
{
ch=fgetc(fp1);
if(ch=='\n')
goto l9;
line1[i]=ch;
i++;
}
l9:line1[i]=NULL;
strcpy(MDT[mdt].des,line1);
MDT[mdt].add=mdt;
mdt++;
fsetpos(fp1,&pos); //find actual parameter
get_word();
do
{
get_word();
t=0;
if(strstr(word,"=")!=0)
{
while(word[t]!='=')
{
word1[t]=word[t];
t++;
}
}
else
strcpy(word1,word);
strcpy(ALA[ala].param,word1);
strcpy(ALA[ala].no,num[ala]);
ala++;
}while(ch!='\n');
}
else if(strcmp(word,"MEND")==0)
{
fsetpos(fp1,&pos);
get_word();
strcpy(MDT[mdt].des,word);
MDT[mdt].add=mdt;
mdt++;
}
else if(strcmp(word,"START")==0)
{
fsetpos(fp1,&pos);
while(!feof(fp1))
{
ch=fgetc(fp1);
fprintf(fp2,"%c",ch);
}
break;
}
else
{
//code in comment to display macro call as INCR &A,&B,®2
//otherwise macro call is like INCR #4,#5,#6
//
/* fsetpos(fp1,&pos);
get_word();
for(i=0;i<mnt;i++) //check nested call of macro
{
if(strcmp(word,MNT[i].name)==0)
{
fsetpos(fp1,&pos);
fgets(line2,30,fp1);
flag1=1;
}
}
if(flag1==1)
{
flag1=0;
strcpy(MDT[mdt].des,line2);
MDT[mdt].add=mdt;
mdt++;
}
else
*/ {
fsetpos(fp1,&pos);
get_word();
strcpy(line2,word);
com=1;
do
{
if(ch=='\n')
goto l1;
get_word();
strcat(line2," ");
flag=0;
for(i=0;i<ala;i++)
{
if(strcmp(word,ALA[i].param)==0)
{
strcpy(word1,ALA[i].no);
strcat(line2,word1);
flag=1;
break;
}
}
if(flag==0)
strcat(line2,word);
if(com==1)
{
strcat(line2,",");
com=0;
}
}while(ch!='\n');
l1:strcpy(MDT[mdt].des,line2);
MDT[mdt].add=mdt;
mdt++;
}
}
}
fclose(fp1);
fclose(fp2);
printf("\nMacro Name Table (MNT) :");
printf("\n\nIndex\tName\tAddress In MDT");
printf("\n-------------------------------");
for(j=0;j<mnt;j++)
printf("\n%d\t%s\t%d",MNT[j].index,MNT[j].name,MNT[j].add_mdt);
printf("\n\nArgument Array List (ALA) :");
printf("\n\nNo\tParam");
printf("\n-----------------");
for(j=0;j<ala;j++)
printf("\n%s\t%s",ALA[j].no,ALA[j].param);
printf("\n\nMacro Definition Table (MDT) :");
printf("\n\nAdd\tDef");
printf("\n-----------------");
for(j=0;j<mdt;j++)
printf("\n%d\t%s",MDT[j].add,MDT[j].des);
getch();
}
void get_word()
{
int i=0;
do
{
ch=getc(fp1);
if(ch!=' ' && ch!='\n' && ch!=',')
{
word[i]=ch;
i++;
}
if(ch==' ' || ch=='\n' || ch==',')
{
word[i]=NULL;
i=0;
break;
}
}while(!feof(fp1));
}
/********* OUTPUT ***********
Macro Name Table (MNT) :
Index Name Address In MDT
-------------------------------
0 INCR 0
1 DECR 5
Argument Array List (ALA) :
No Param
-----------------
#1 &X
#2 &Y
#3 ®1
#4 &A
#5 &B
#6 ®2
Macro Definition Table (MDT) :
Add Def
-----------------
0 INCR &X,&Y,®1=AREG
1 MOVER #3, #1
2 ADD #3, #2
3 MOVEM #3, #1
4 MEND
5 DECR &A,&B,®2=BREG
6 MOVER #6, #4
7 SUB #6, #5
8 MOVEM #6, #4
9 INCR #4,#5,#6
10 MEND
*/
/*********** a.asm ******************/
MACRO INCR &X,&Y,®1=AREG
MOVER ®1,&X
ADD ®1,&Y
MOVEM ®1,&X
MEND
MACRO DECR &A,&B,®2=BREG
MOVER ®2,&A
SUB ®2,&B
MOVEM ®2,&A
INCR &A,&B,®2
MEND
START 100
READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END
/*************** b.txt **************/
START 100READ N1
READ N2
DECR N1,N2
STOP
N1 DS 1
N2 DS 2
END
its really easy to understand..........
ReplyDeletecopied fron techmax publication book
ReplyDeleteLol someone is jealous..
Delete:D good job
ReplyDeleteit is very good program... plz send me pass 2 in continuation of this to my email id nehabhosale13@gmail.com
ReplyDelete