0% found this document useful (0 votes)
37 views9 pages

Two-Pass Macro Processor Implementation

The document outlines the design and implementation of a two-pass macro processor, detailing its objectives, theoretical background, and processing algorithms. It includes information on the Macro Definition Table (MDT), Macro Name Table (MNT), and Argument List Array (ALA), along with a software code example and observations from the implementation. Additionally, it discusses the features of macros and differentiates between macros and procedures/functions.

Uploaded by

mystics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

Two-Pass Macro Processor Implementation

The document outlines the design and implementation of a two-pass macro processor, detailing its objectives, theoretical background, and processing algorithms. It includes information on the Macro Definition Table (MDT), Macro Name Table (MNT), and Argument List Array (ALA), along with a software code example and observations from the implementation. Additionally, it discusses the features of macros and differentiates between macros and procedures/functions.

Uploaded by

mystics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PART A

(PART A : TO BE REFFERED BY STUDENTS)

Experiment No. 10 (A)


Aim : To Design and Implement two pass Macro Processor

Objective: Develop a program to implement two pass macro processor:


a. To generate Macro definition Table(MDT)
b. To generate Macro Name table(MNT)
c. To generate Argument List Array(ALA)
d. To generate expanded source code
Outcome: Students are able to design and implement two pass Macro Processor.

Theory:

A macro processor is a program that copies a stream of text from one place to another,
making a systematic set of replacements as it does so. Macro processors are often embedded
in other programs, such as assemblers and compilers. Sometimes they are standalone
programs that can be used to process any kind of text.

Macro processors have been used for language expansion (defining new language
constructs that can be expressed in terms of existing language components), for systematic
text replacements that require decision making, and for text reformatting (e.g. conditional
extraction of material from an HTML file).

The following four major tasks are done by macro processor.


1. Recognize macro definition by identifying MACRO and MEND pseudo-ops.
2. Save these definitions which are required for macro expansion process
3. Recognize macro calls by identifying macro name which appears as operation
mnemonic.
4. Finally expand macro call and substitute arguments for the dummy arguments.
Databases Used

Pass1 Databases:
1. The input source program
2. The output source deck to be used by pass 2
3. The Macro Definition Table (MDT) : this table stores the macro definition.
4. The Macro name Table(MNT) : This table stores macro names which are defined.
5. The Macro Definition Table Counter (MDTC), which used to indicate next available
entry in MDT.
6. The Macro Name Table Counter (MNTC), which used to indicate next available entry
in MNT.
7. Argument list Array (ALA), which stores the index markers for dummy arguments.

Pass 2 Databases:
1. Copy of input source program
2. The output for assembler: the expanded source code
3. The Macro Definition Table (MDT) :created by pass1.
4. The Macro name Table(MNT) : This created by pass1
5. The Macro Definition Table Pointer (MDTP), which used to indicate next line used in
macro expansion.
6. Argument list Array (ALA), which is used to substitute arguments for index markers.

Format of databases:

We will use following example to discuss the format of all databases.


MACRO
&LAB ADDM &ARG1, &ARG2, &ARG3
A 1,&ARG1
A 2,&ARG2
A 3,&ARG3
MEND
………………………………
LOOP ADDM D1, D2, D3
………………………………..

1. Argument List Array (ALA)


This is an array which stores all arguments used in macro definition. Each argument is assigned
an index marker.
Consider following macro call,
LOOP ADDM D1, D2, D3
The ALA for this would be:
8 bytes per entry
Index Argument
0 “Loopbbbb”
1 “D1bbbbbb”
2 “D2bbbbbb”
3 “D3bbbbbb”

2. Macro Definition Table (MDT)


This table stores each line of macro definition in it except the line for MACRO pseudo- op.The
MDT for above example is :
80 bytes per entry
Index Card
…… …………….
10 &LAB ADDM &ARG1, &ARG2, &ARG3
11 A 1,&ARG1
12 A 2,&ARG2
13 A 3,&ARG3
14 MEND
……. ………
3. Macro Name Table (MNT)

Each entry in MNT has following fields:


Index:
Name: It is a macro name
MDT Index: This is an index from MDT which indicates the line number from which
macro definition is stored in MDT.

The MNT for above example is :


8 bytes per entry

index Name MDT index

…… ………………. ……………….

3 “ADDMbbbb” 10

….. …………….. ………….

Algorithm

Pass 1 processing:
1. Initialize MDTC and MNTC as MDTC=MNTC=1
2. Read a line from input source card.
3. If it is MACRO pseudo-op then
3.1 Read the next line which will be the macro name line. Enter the macro name in
MNT with current value of MDTC.
3.2 Increment the value of MNTC by 1
3.3 Then the argument list array is prepared for the arguments found in the macro
name line.
3.4 The macro name cared is also inserted in MDT.
3.5 Increment the value of MDTC by 1
3.6 Read the next line from source card
3.7 Substitute the index markers for arguments from ALA prepared in previous step
and then enter this line into MDT
3.8 Increment the value of MDTC by 1
3.9 Check whether it is MEND pseudo-op then
3.9.1 Go to step 2.
else
3.9.2 Go to step 3.6
4. Else, if it is not MACRO pseudo-op then simply write the line in the copy of source
deck prepared for pass2.
5. Check if it is END pseudo-op then
5.1 Go to step 6
else
5.2 Go to step 2.
6. Stop

Pass 2 processing:
1. Read a line from copy of source deck prepared by pass1.
2. Search the MNT for match with the op-code.
3. If macro name found in MNT then
3.1 Initialize MDTP with the value of MDT index from the MNT entry. Thus MDTP
holds the location from which macro definition is stored in MDT.
3.2 Argument list array is prepared for the arguments found in macro call.
3.3 Increment value of MDTP by 1.
3.4 Read the next line from MDT and substitute the arguments from macro call for
the dummy arguments.
3.5 If it is MEND pseudo-op then
3.5.1 Go to step 1.
else
3.5.2 Write this line into expanded source code.
3.5.3 Increment the value of MDTP by 1
3.5.4 Go to step 3.4 in order to process remaining statements from MDT.
4. Else if match for macro name is not found in MNT then write the line into expanded
source code.
5. If it is END pseudo-op then
1.1 Supply this expanded source code copy to assembler.
1.2 Go to step 6
else
5.3 Go to step 1.

7. STOP
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

B.1 Software Code written by student:


#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("macro.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();
}

B.2 Input and Output:


macro.asm
MACRO SAMPLE1
&P,&Q
MOVER ARG,&P
MUL ARG,&Q
MEND
START
READ A
READ B
SAMPLE1 A,B
END
Output:

B.3 Observations and learning:


Implementing a two-pass macro processor is a complex task that involves a deep
understanding of compiler design principles, data structures, and algorithms. Learning
from this process includes gaining insights into efficient symbol resolution, error
handling, and the intricacies of macro expansion. The ability to design modular and
maintainable code is crucial for the long-term viability of the macro processor.

B.4 Conclusion:
We successfully learned and implemented two pass Macro Processor.

B.5 Question of Curiosity:


A. List the features of Macro.
Ans:
Features of Macro :
1. Text Replacement:
● Substitution: Macros allow developers to define shorthand notations that are
replacedwith a block of code during preprocessing.
● Code Expansion: Macros are expanded by the preprocessor, resulting in the
inclusionof the macro's code wherever it is invoked.
2. Code Generation:
● Custom Code Blocks: Macros enable the creation of reusable code blocks that
can beinserted at multiple locations in the source code.
● Abstraction: Macros provide a level of abstraction, allowing programmers to
define complex operations with a single macro invocation.
3. Parameterized Macros:
● Parameter Passing: Macros can take parameters, allowing them to be more
flexible and adaptable to different use cases.
● Argument Substitution: Parameters in macros can be substituted with actual
values during macro expansion.
4. Conditional Compilation:
● Conditional Macros: Macros can be used to conditionally include or exclude
portions of code based on compile-time conditions.
● Compile-Time Flags: Macros are often used to define compile-time flags that
controlthe compilation of specific code sections.
5. Error Handling:
● Diagnostic Macros: Macros can be designed to include diagnostic information
or error-handling code for better debugging.
● Logging: Macros are utilized for logging and tracing information during
program execution.

B. Differentiate Between Macro and Procedure/Function


Ans:
Macro Procedure

It is a series of rules or programmable patterns It is a sequence of instructions that a programmer


that decrypts a specified input sequence into can repeatedly call to execute a specified
a predefined output sequence. purpose.

It needs a large memory. It needs less memory.

When a macro is called, a new machine code is Only one instance of the machine code is
created. generated during the procedure.

It is utilized for a small number of instructions, It is utilized for a large number of instructions,
usually less than ten. usually more than ten.

It doesn't need CALL and RET instructions. It needs CALL and RET instructions.
The execution speed of a macro is faster. The execution speed of a procedure is slower than
a macro.

You might also like