Tuesday, September 15, 2020

Macro Preprocessor

 Assignment 4 : Macro Processor

An assembly language macro is a facility for extending the set of operations provided in an assembly language.

A programmer can define his own set of macros only once and can use them many times.

A macro definition consists of a name , a set of formal parameters and a body of code. When a macro name  along with a set of actual parameters is used, it is replaced by body of macro and it is called macro expansion.

Macro processor is a software that takes as input a program containing macro definitions and calls and generates an assembly language program which is free of macro definitions and where macro calls have been properly expanded.

Macro processor has two main steps

 i) Processing macro definitions

 ii) macro expansion

In the first step each macro definition is processed to extract information and is stored in well defined data structures.

In macro expansion each macro call is expanded using appropriate information from the tables.

Data Structures

Macro name table:-

                It stores the name of the macro and other information such as no of positional parameters, keyword parameters etc. It is used as a lookup table when a macro call is identified. It also contains pointers to all other tables where relevant information is stored

struct mnttab

{ // structure of MNT table

 char name[30];

 int pp;

int kp;

int kpdptr;

 int mdtptr;

}mnt[10];

Parameter name table :-

                It contains names of formal parameters including positional and keyword parameters

char pnt[10][30]; or char **pnt;

 keyword parameter default value table:-

                It contains keyword parameters and their default values

 Macro definition Table :-

                It contains the model statements of all macros . They are kept in partially processed (IC) form so that expansion is easier.

 

Actual parameter Table:-

                It contains actual parameters   i.e. values that will replace formal parameters during the expansion

 

Pointers to various tables :- (MDT)

                int mdtptr=0; ….

 

Slot 1

a) What are the two main tasks of a macro processor?

Answer :-

Macro processor has two main steps

 i) Processing macro definitions

 ii) macro expansion

 

b) What tables are used by macro processor to store extracted information?

Answer:-

1)MNT

2)MDT

3)PNTAB

4) KPDTAB

5) EVTAB

6)EVNTAB

7)SSTAB

8)SSNTAB

9) APTAB

ii)   Create a file named first.asm containing following macro definitions

MACRO

COPY &ONE, &TWO, &REG=BREG

MOVER &REG, &ONE

MOVEM &REG, &TWO

MEND

MACRO

CHANGE &FIRST, &SECOND, &REG=AREG, &OP=ADD

MOVER &REG, &FIRST

&OP &REG, &SECOND

MOVEM &REG, &FIRST

MEND

iii) Write a command line macro processor program that takes above file as command line argument and prints the macro names, and names of parameters. 

Program :- 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct MNT

{

char mname[20];

int mdtp,pp;

}mnt;

struct MDT

{

char op[20],value[50];

}mdt[10];

char pnt[10][10];  int mdt_cnt,pnt_cnt;


int search(char *s)

{

int i;

for(i=0;i<pnt_cnt;i++)

if(strcmp(pnt[i],s)==0)

return i;

return -1;

}

void print_mnt()

{

printf("#\tmname\tmdtp\tpp\n");

printf("1\t%s\t%d\t%d\n",mnt.mname,mnt.mdtp,mnt.pp);

}

void print_pnt()

{

int i;

printf("#\tpname\n");

for(i=0;i<pnt_cnt;i++)

printf("%d\t%s\n",i+1,pnt[i]);

}

void print_mdt()

{

int i;

printf("#\topcode\tvalue\n");

for(i=0;i<mdt_cnt;i++)

printf("%d\t%s\t%s\n",i+1,mdt[i].op,mdt[i].value);

}

void make_pnt(char *s)

{

char temp[10];

int i=0,j=0;

strcat(s,",");

while(s[i]!='\0')

{

if(s[i]==',')

{

temp[j]='\0';

j=0;

strcpy(pnt[pnt_cnt++],temp);

}

else

temp[j++]=s[i];

i++;

}

}

int main()

{

FILE *fp;

char fname[20],buff[80],t1[20],t2[20],t3[20];

int i,j;

printf("Enter source file:");

scanf("%s",fname);

fp = fopen(fname,"r");

if(fp==NULL)

{

printf("File %s not found.\n",fname);

exit(1);

}

while(fgets(buff,80,fp)!=NULL)

{

sscanf(buff,"%s %s %s",t1,t2,t3);

if(strcmp(t1,"MACRO")==0)

{

fgets(buff,80,fp);

sscanf(buff,"%s %s",t1,t2);

strcpy(mnt.mname,t1);

make_pnt(t2);

mnt.pp = pnt_cnt;

mnt.mdtp = 1;

}

else if(strcmp(t1,"MEND")==0)

{

strcpy(mdt[mdt_cnt++].op,t1);

}

else

{

if(t1[0]=='&')

{

i = search(t1);

sprintf(mdt[mdt_cnt].op,"(P, %d)",i+1);

}

else

{

strcpy(mdt[mdt_cnt].op,t1);

}

t2[strlen(t2)-1]='\0';

i = search(t2);

j = search(t3);

sprintf(mdt[mdt_cnt++].value, "(P, %d), (P, %d)",i,j+1);

}

}

fclose(fp);

print_mnt();

print_pnt();

print_mdt();

return 0;

}

******************OUTPUT******************

Enter source file:inpmac.asm

# mname mdtp pp

1 CHANGE 1 4

# pname

1 &ONE

2

3 &FIRST

4

# opcode value

1 MOVER (P, -1), (P, 1)

2 MOVEM (P, -1), (P, 0)

3 MEND

4 MOVER (P, -1), (P, 3)

5 (P, 0) (P, -1), (P, 0)

6 MOVEM (P, -1), (P, 3)

7 MEND


Slot 2 

i) Extend the macro processor program 

a) Define appropriate data structure for all the tables

 b) Write code for extracting information from 

Program :-

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

struct MNT

{

char mname[10];

int mdtp,kpdtp,pp,kp;

}mnt;

struct KPDT

{

char key[10],def[10];

}kpdt[5];

char pnt[10][10];

int pnt_cnt,kpdt_cnt;

void print_mnt()

{

printf("#\tmname\tmdtp\tkpdtp\t#pp\t#kp\n");

printf("1\t%s\t%d\t%d\t%d\t%d\n",

     mnt.mname,mnt.mdtp,mnt.kpdtp,mnt.pp,mnt.kp);

}

void print_kpdt()

{

int i;

printf("#\tkey\tdef\n");

for(i=0;i<kpdt_cnt;i++)

printf("%d\t%s\t%s\n",

i+1,kpdt[i].key,kpdt[i].def);

}

void print_pnt()

{

int i;

printf("#\tpname\n");

for(i=0;i<pnt_cnt;i++)

printf("%d\t%s\n",i+1,pnt[i]);

}

void make_pnt_kpdt(char *s)

{

int i=0,j=0;

char temp[10];

strcat(s,",");

while(s[i]!='=')

{

if(s[i]==',')

{

temp[j]='\0';

j=0;

strcpy(pnt[pnt_cnt++],temp);

}

else

{

temp[j++]=s[i];

}

i++;

}

while(s[i]!='\0')

{

if(s[i]=='=')

{

temp[j]='\0';

j=0;

strcpy(pnt[pnt_cnt++],temp);

strcpy(kpdt[kpdt_cnt].key,temp);

}

else if(s[i]==',')

{

temp[j]='\0';

j=0;

strcpy(kpdt[kpdt_cnt++].def,temp);

}

else

{

temp[j++]=s[i];

}

i++;

}

mnt.pp = pnt_cnt-kpdt_cnt;

mnt.kp = kpdt_cnt;

}

int main()

{

FILE *fp;

char fname[20],buff[80],t1[20],t2[20],t3[20];

int i,j;

printf("Enter source file name:");

scanf("%s",fname);

fp=fopen(fname,"r");

if(fp==NULL)

{

printf("File %s not found\n",fname);

exit(1);

}

while(fgets(buff,80,fp)!=NULL)

{

sscanf(buff,"%s %s %s",t1,t2,t3);

if(strcmp(t1,"MACRO")==0)

{

fgets(buff,80,fp);

sscanf(buff,"%s %s",t1,t2);

strcpy(mnt.mname,t1);

mnt.mdtp = mnt.kpdtp = 1;

make_pnt_kpdt(t2);

break;

}

}

fclose(fp);

print_mnt();

print_kpdt();

print_pnt();

return 0;

}

/*******************Inpmac.asm****************
MACRO
INCR &X,&Y,&OP=ADD,&REG=AREG
MOVER &REG, &X
&OP &REG, &Y
MOVEM &REG, &X
MEND

/*****************OUTPUT*****************

Enter source file name:inpmac.asm

# mname mdtp kpdtp #pp #kp

1 INCR 1 1 2 2

# key def

1 &OP ADD

2 &REG AREG

# pname

1 &X

2 &Y

3 &OP

4 &REG





No comments:

Post a Comment