Sunday 25 September 2011

                                               TWO PASS MACRO PROCESSOR

AIM:
To write a C program to implement the two pass macroprocessor.
ALGORITHM :
Pass 1:
1.      Store the Macro Name in the Macro Name Table
2.      Store the Arguments in the Argument Table
3.      Store the definition in the Definition Table.
Pass 2 :
1.      In Place of Macro Call in the main program Expand the Macro Definition read from the Macro Definition table
2.      Substitute Arguments got from argument table.
3.      Print the expanded program.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
static int count=0,address=1000,n=0,d=0,a=0,a1=0,o=0;
struct input
{
char label[10];
char opcode[10];
char operand[30];
}ip[50];
struct output
{
char label[10];
char opcode[10];
char operand[30];
}op[50];
struct def_table
{
int address;
char opcode[10];
char operand[30];
}deftab[20];
struct name_table
{
char name[10];
int start,end;
}namtab[20];
struct arg_table
{
char arg[10];
}argtab[10];
struct arg_table1
{
int no;
char arg[10];
}argtab1[10];

void main()
{
int def=0,temp,j,i;
char str[10]="\0";
char str1[10]="\0";
clrscr();
printf("\n\t\t MACRO PASS 1 ");
printf("\n\t\t--------------");
printf("\nEnter the input program\t:\t\n");
printf("\n\nLABEL\tOPCODE\tOPERAND\n\n");
for(i=0;i<50;i++)
{
scanf("%s%s%s",ip[i].label,ip[i].opcode,ip[i].operand);
count++;
if(strcmp(ip[i].opcode,"END")==0)
break;
}
//MACRO DEFINITION ARGUMENT
for(i=0;i<count;i++)
{
if(strcmp(ip[i].opcode,"MACRO")==0)
{
int t,last=0;
for(t=0;t<(strlen(ip[i].operand))+1;t++)
{
if(ip[i].operand[t]==','||t==(strlen(ip[i].operand)))
{
strcpy(argtab[a].arg,str);
strcpy(str,'\0');
a++;
last=0;
}
else if(ip[i].operand[t]!=',')
{
str[last]=ip[i].operand[t];
last++;
}
}
}
}
//DEFINITION TABLE
for(i=0;i<count;i++)
{
if(strcmp(ip[i].opcode,"MACRO")==0)
{
while(strcmp(ip[i].opcode,"MEND")!=0)
{
def++;
if(strcmp(ip[i].opcode,"MACRO")==0)
{
namtab[n].start=address;
strcpy(namtab[n].name,ip[i].label);
deftab[d].address=address;
strcpy(deftab[d].opcode,ip[i].label);
strcpy(deftab[d].operand,ip[i].operand);
d++;
address++;
}
else
{
char str1[20];
int flag=0,l=0,temp1;
deftab[d].address=address;
strcpy(deftab[d].opcode,ip[i].opcode);
for(temp=0;temp<a;temp++)
{
if(strstr(ip[i].operand,argtab[temp].arg))
{
flag=1;
for(temp1=0;temp1<(strlen(ip[i].operand));temp1++)
{
if(ip[i].operand[temp1]=='&')
{
temp1+=((strlen(argtab[temp].arg))-1);
switch(temp+1)
{
case 1:
str1[l]='?';
l++;
str1[l]='1';
break;
case 2:
str1[l]='?';
l++;
str1[l]='2';
break;
case 3:
str1[l]='?';
l++;
str1[l]='3';
break;
}
l++;
}
else
{
str1[l]=ip[i].operand[temp1];
l++;
}
}
str1[l]='\0';
strcpy(deftab[d].operand,str1);
strcpy(str1,'\0');
}
}
if(!(strstr(ip[i].operand,argtab[temp].arg))||flag==0)
strcpy(deftab[d].operand,ip[i].operand);
d++;
address++;
}
i++;
if(strcmp(ip[i].opcode,"MEND")==0)
{
namtab[n].end=address;
deftab[d].address=address;
strcpy(deftab[d].opcode,"MEND");
strcpy(deftab[d].operand,'\0');
n++;
}
}
}
}
printf("\n MACRO DEFINITION TABLE");
printf("\n------------------------");
printf("\n\taddress\topcode\toperand");
for(temp=0;temp<=def;temp++)
printf("\n\t%d\t%s\t%s",deftab[temp].address,deftab[temp].opcode,deftab[temp].operand);
printf("\n NAME TABLE");
printf("\n------------");
printf("\n\tname\tstart\tend");
for(temp=0;temp<n;temp++)
printf("\n\t%s\t%d\t%d",namtab[temp].name,namtab[temp].start,namtab[temp].end);
//MACRO INVOCATION ARGUMENT(ARGUMENT TABLE)
j=1;
for(i=0;i<count;i++)
{
for(temp=0;temp<n;temp++)
{
if(strcmp(ip[i].opcode,namtab[temp].name)==0)
{
int t,last=0;
for(t=0;t<(strlen(ip[i].operand))+1;t++)
{
if(ip[i].operand[t]==','||t==(strlen(ip[i].operand)))
{
argtab1[a1].no=j;
strcpy(argtab1[a1].arg,str1);
strcpy(str1,'\0');
a1++;j++;
last=0;
}
else if(ip[i].operand[t]!=',')
{
str1[last]=ip[i].operand[t];
last++;
}
}
}
}
}
printf("\n ARGUMENT TABLE");
printf("\n----------------");
printf("\n\tno\targument");
for(temp=0;temp<a1;temp++)
printf("\n\t%d\t%s",argtab1[temp].no,argtab1[temp].arg);
//EXPANDED VERSION
for(i=0;i<count;i++)
{
int z;
if(strcmp(ip[i].opcode,"MACRO")==0)
{
while(strcmp(ip[i].opcode,"MEND")!=0)
{
i++;
}
}
if(strcmp(ip[i].opcode,"MEND")==0)
continue;
for(temp=0;temp<n;temp++)
{
int temp1=0;
if(strcmp(ip[i].opcode,namtab[temp].name)==0)
{
strcpy(op[o].label,strcat(" .",ip[i].label));
strcpy(op[o].opcode,ip[i].opcode);
strcpy(op[o].operand,ip[i].operand);
o++;
for(temp1=1;temp1<def;temp1++)
{
if(strcmp(deftab[temp1].opcode,"MEND")!=0)
{
strcpy(op[o].label," - ");
strcpy(op[o].opcode,deftab[temp1].opcode);
for(z=0;z<strlen(deftab[temp1].operand);z++)
{
char s[10];
int i;
if(deftab[temp1].operand[z]=='?')
{
int i;
i=(int)deftab[temp1].operand[z+1]-48;
strcat(op[o].operand,argtab1[i-1].arg);
z++;
}
else
{
i=strlen(op[o].operand);
op[o].operand[i]=deftab[temp1].operand[z];
}
}
}
o++;
}
}
else
{
strcpy(op[o].label,ip[i].label);
strcpy(op[o].opcode,ip[i].opcode);
strcpy(op[o].operand,ip[i].operand);
o++;
}
}
}
printf("\n MACRO EXPANDED VERSION");
printf("\n------------------------");
printf("\n\tlabel\topcode\toperand");
for(temp=0;temp<o;temp++)
printf("\n\t%s\t%s\t%s",op[temp].label,op[temp].opcode,op[temp].operand);

getch();
}


OUTPUT:


MACRO PASS 1
-----------------
Enter the input program :


LABEL         OPCODE          OPERAND
COPY             START               0
RDBUFF           MACRO              &INDEV,&BUFADR,&RECLTH
-                CLEAR              Z
-                STC                =X'&INDEV'
-                MEND               -
CLOOP            RDBUFF             F1,BUFFER,LENGTH
-                END                -

MACRO DEFINITION TABLE
------------------------------
address       opcode      operand
1000             RDBUFF         &INDEV,&BUFADR,&RECLTH
1001             CLEAR          Z
1002             STC            =X'?1'
1003             MEND
NAME TABLE
--------------
name    start    end
RDBUFF     1000       1003

ARGUMENT TABLE
--------------------
no     argument
1       F1
2       BUFFER
3       LENGTH
MACRO EXPANDED VERSION
-------------------------------
label    opcode    operand
COPY        START       0
.CLOOP      RDBUFF      F1,BUFFER,LENGTH
-          CLEAR       Z
-          STC         =X'F1'
-          END         -
Result
Thus above program executed and output verified.

0 comments:

Post a Comment