0% found this document useful (0 votes)
20 views6 pages

SPCC Exp2

System programs and compiler construction experiments

Uploaded by

sanjanabhosle27
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)
20 views6 pages

SPCC Exp2

System programs and compiler construction experiments

Uploaded by

sanjanabhosle27
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
You are on page 1/ 6

EXPERIMENT NO.

Aim of the Experiment: - Implementation of Two pass Macro Processor

Lab Outcome: - Implement two pass macro processor.

Date of Performance: - _____

Date of Submission: - _________

Implementation Understanding Punctuality & Total


(05) (05) Discipline (05) Marks
(15)

______________________________

Practical Incharge
EXPERIMENT NO-2

Aim: Design and implement of Two pass Macro Processor

Theory:

A macro is an abbreviation for a sequence of operations. The macro processor


effectively constitutes a separate language processor with its own language. It
provides a name to the sequence of statements (called macro name) that are
repeated and ever needed it make use of the name in the main program. The macro
definition consists of the following parts:

1. Start of definition MACRO

2. Macro name [ ]

3. Sequence of statements -----------

-----------

4. End of definition MEND

In the definition of the MACRO and MEND are the pseudo code that represents the
start and the end of the macro definition. Once the macro is defined then the name
of macro instruction now acts as a mnemonic in assembly language that is
equivalent to sequence of the statements. Whenever the macro instruction is
encountered, the whole of the macro gets substituted in place of the macro
instruction. This process of substitution is called as macro expansion. This
expansion takes place as the definition of the macro instruction is saved in the
macro processer and is used whenever the macro instruction is encountered. A
macro call represents the expansion of the macro in the source program.

There are four basic tasks that any macro processor is associated with are
as follows

1. Recognizing macro definition: We recognize the macro definition in a program


by the statements that appear in between MACRO and corresponding MEND. The
definition of macro includes these two pseudo codes.

2. Save the definition: The definitions of all the macro are saved onto a table so that
they can be expanded whenever in future they are called.

3. Recognize the calls: A macro call is similar to a machine instruction whose work
is predefined i.e. the definition is stored in some table.

4. Expand calls and substitute arguments: Each time a macro call is encountered a
table consisting of macro name is searched and all the code in the macro definition
except the MACRO, MEND and the macro name card is expanded in the main
program. The actual source program now looks like as it was intended by the
programmer.
Implementation Single Pass Algorithm

Definition of a macro within other macro is possible in case of one pass macro
processor. Here the inner macro is defined only after the outer one has been called:
in order to provide for any use of the inner macro, we would have to repeat the both
the macro definition and the macro call passes. This can be assumed by considering
that the macros are never called before they are defined. Here we make use of
additional data structures like macro definition indicator (MDI) and macro
definition level counter (MDLC). The MDI and MDLC are the switches used to
keep track of macro calls and macro definition.

The MDI has status “ON” during the expansion of macro call and the value “OFF”
all the other times. When its value is “ON” the cards are read from the MDT and
when it is “OFF” the cards are read from the input source card. The use of MDLC is
used keep track of the level of macros while defining the macros. Initially it is zero
and it is incremented each time a MACRO code is found within a macro. The
reverse process happens in case of MEND i.e. the valued of MDLC is decremented
by one each time it encounters a MEND and the process continues till the MDLC is
zero i.e. the completion of macro definition.

Algorithm:

1. Initialize MDTC and MNTC to 1, MDI to “OFF” and MDLC to 0.


2. READ
3. Search MNT for match with operation code.
4. If macro name found
a. MDI<-“ON”.
b. MDTP<-MDT index from MNT entry.
c. Setup macro call ALA.
d. GOTO 2.
5. Else if MACRO pseudo code
a. Then READ. //macro name line.
b. Enter macro name and current value of MDTC in MNT entry number
MNTC.
c. Increment MNTC <- MNTC+1.
d. Prepare macro definition ALA.
e. Enter macro name card into MDT.
f. MDTC<-MDTC+1.
g. MDLC<-MDLC+1.
h. READ.
i. Substitute index notation for arguments in definition.
j. Enter line into MDT.
k. MDTC<-MDTC+1
l. If MACRO pseudo code
i. MDLC<-MDLC+1
ii. GOTO 5.h.
m. Else If MEND pseudo code
i. Then MDLC<-MDLC-1
1. If MDLC=0 the
a. Then GOTO 2.
2. Else GOTO 5.h.
n. Else GOTO 5.h.
6. Write into expanded source card file.
7. If END pseudo code
a. Then Supply expanded source file to assembler processing.
8. Else GOTO 2

READ: (Macro call expansion or read a next instruction form the source
input card)
1. If MDI =”OFF”, then
a. Read next source card from input file.
b. Return to MAIN algorithm.
2. Else increment MDT pointer to next entry MDTP<-MDTP+1.
3. Get next card from MDT.
4. Substitute arguments from macro call.
5. If MEND pseudo code
a. Then if MDLC=0,
i. Then MDI<-“OFF”.
ii. GOTO 1.a.
b. Else return to MAIN algorithm.
6. Else if AIF or AGO present
a. Then process AIF or AGO and update MDTP.
b. Return to MAIN algorithm.
7. Else return to MAIN algorithm.

Program:

#include<stdio.h>
#include<string.h>
struct mdt
{
char lab[10];
char opc[10];
char oper[10];
}d[10];
void main()
{
char label[10],opcode[10],operand[10];
char macroname[10];
int i,lines;
FILE *f1,*f2,*f3;

f1 = fopen("MACIN.txt","r");
f2 = fopen("MACOUT.txt","w");
f3 = fopen("MDT.txt","w");
fscanf(f1,"%s %s %s",label,opcode,operand);

while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"MACRO")==0)
{
strcpy(macroname,label);
fscanf(f1,"%s%s%s",label,opcode,operand);
lines = 0;
while(strcmp(opcode,"MEND")!=0)
{
fprintf(f3,"%s\t%s\t%s\n",label,opcode,operand);
strcpy(d[lines].lab,label);
strcpy(d[lines].opc,opcode);
strcpy(d[lines].oper,operand);
fscanf(f1,"%s %s %s",label,opcode,operand);
lines++;
}
}
else if(strcmp(opcode,macroname)==0)
{

for(i=0;i<lines;i++)
{
fprintf(f2,"%s\t%s\t%s\n",d[i].lab,d[i].opc,d[i].oper);

}
}
else
fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(f1,"%s%s%s",label,opcode,operand);
}
fprintf(f2,"%s\t%s\t%s\n",label,opcode,operand);
fclose(f1);
fclose(f2);
fclose(f3);
printf("FINISHED");
}

Input:
CALC START 1000
SUM MACRO **
** LDA #5
** ADD #10
** sTA 2000
** MEND **
** LDA LENGTH
** COMP ZERO
** JEQ LOOP
** SUM **
LENGTH WORD S
ZERO WORD S
LOOP SUM **
** END **
Input File:

Output:

Conclusion: Hence, implemented two pass macro processor successfully.

You might also like