Program to design and implement Macro > Assembly Program
Program to design and implement Macro > Assembly Program
System Programming and Compiler Construction
Program 1:
macro.c
macro.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct mnt
{
int sr;
char mn[30];
int mdtc;
int p;
}ntab[10];
struct mdt
{
int index;
char entry[30];
}dtab[10];
void main()
{
int ind1=1,k,len,i=0,count,j=1,ind=1,z=0,y=0,n=0,x;
char str[80],*s1,*s2,*s3,s4[100];
FILE *fp1,*fp2;
clrscr();
fp1=fopen("macro1.asm","r");
while(!feof(fp1))
{
y=z;
fgets(str,80,fp1);
sscanf(str,"%s%s%s",s1,s2,s3);
if(strcmp(s1,"MACRO")==0)
{
count=0;
len=strlen(s3);
for(k=0;k<len;k++)
if(s3[k]=='&')
count++;
ntab[i].sr=j;
strcpy(ntab[i].mn,s2);
ntab[i].p=count;
ntab[i].mdtc=ind;
z=1;
i++;
j++;
}
if(strcmp(s1,"MEND")==0)
{
z=0;
dtab[n].index=ind;
strcpy(dtab[n].entry,s1);
n++;
ind++;
y=z;
}
if(y==1)
{
dtab[n].index=ind;
strcpy(dtab[n].entry,s1);
strcat(dtab[n].entry," ");
len=strlen(s2);
for(k=0;k<len;k++)
{
s4[k]=s2[k];
if(s4[k]=='&')
{
s4[k]='#';
break;
}
}
s4[k+1]=ind1+'0';
ind1++;
strcat(dtab[n].entry,s4);
n++;
ind++;
}
}
fcloseall();
printf("Macro Name Table\nMNTC\tName\tParameters\tMDTC\n");
for(x=0;x<i;x++)
printf("%d\t%s\t\t%d\t%d\n",ntab[x].sr,ntab[x].mn,ntab[x].p,ntab[x].mdtc);
printf("\n");
printf("Macro Defination Table\nMDTC\tDefinition");
for(x=0;x<n;x++)
{
printf("\n%d\t%s",dtab[x].index,dtab[x].entry);
}
getch();
}
macro1.asm
MACRO SAMPLE1 &P,&Q
MOVER ARG,&P
MUL ARG,&Q
MEND
START
READ A
READ B
SAMPLE1 A,B
END
Output:
Program 2:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int MDTC=1,MNTC=1;
struct MNT
{
int index;
char mac_name[20];
int mdt_index;
}mntab[10];
struct MDT
{
int index;
char mac_def[20];
}mdtab[10];
struct arg_table
{
char name[20];
}atab[10];
void main()
{
int i=1,j=1,k,x=1,l=1,y,flag=0;
FILE *fp1;
char str[80],*s1,*s2,*s3,*s4,str1[10],*str2,str3[2]=",";
clrscr();
fp1=fopen("pg1.asm","r");
while(!feof(fp1))
{
fgets(str,80,fp1);
sscanf (str,"%s %s %s %s",s1,s2,s3,s4);
if(MNTC!=1 & MDTC!=1 & strcmp(s1,"MEND")!=0 & flag==1)
{
mdtab[i].index=i;
for(x=1;x<10;x++)
{
if(strcmp(s3,atab[x].name)==0)
{
y=x;
}
}
itoa(y,str1,10);
strcat(s1," ");
strcat(s1,s2);
strcat(s1," ");
strcat(s1,"#");
strcat(s1,str1);
strcpy(mdtab[i].mac_def,s1);
i++; x++;
MDTC=MDTC+1;
}
if(strcmp(s1,"MACRO")==0)
{
flag=1;
mntab[j].index=j;
strcpy(mntab[j].mac_name,s2);
mntab[j].mdt_index=MDTC;
MNTC=MNTC+1;
j++;
mdtab[i].index=i;
strcpy(mdtab[i].mac_def,s2);
MDTC=MDTC+1;
i++;
str2=strtok(s3,str3);
while(str2)
{
strcpy(atab[l].name,str2);
str2=strtok(NULL,str3);
l++;
}
}
if(strcmp(s1,"MEND")==0)
{
flag=0;
}
strcpy(s1," ");
strcpy(s2," ");
strcpy(s3," ");
strcpy(s4," ");
}
printf("MDT TABLE\n");
printf("index\tMACRO DEF\n");
for(k=1;k<i;k++)
{
printf("%d \t %s\n",mdtab[k].index,mdtab[k].mac_def);
}
printf("MNT TABLE\n");
printf("index\tMACRO NAME\tMDT index\n");
for(k=1;k<j;k++)
{
printf("%d \t %s\t\t %d\n",mntab[k].index,mntab[k].mac_name,mntab[k].mdt_index);
}
fcloseall();
getch();
}
pg1.asm
MACRO SAMPLE1 &P,&Q
MOVER AREG, &P
MUL AREG, &Q
MEND
START
READ A
SAMPLE1 A, B
END
/* OUTPUT :
MDT TABLE
index MACRO DEF
1 SAMPLE1
2 MOVER AREG, #1
3 MUL AREG, #2
MNT TABLE
index MACRO NAME MDT index
1 SAMPLE1 1
*/
Comments
Post a Comment