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

LPCC Assignment7

Uploaded by

novon34521
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)
18 views9 pages

LPCC Assignment7

Uploaded by

novon34521
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/ 9

Name: Shreyas Satish Jagadale

Roll No: 322030


GR No: 22110649
Class: TY-B
Batch: B2

Assignment - 7
Title: Design suitable data structures & implement pass-I of a two-pass Macro processor

Theory: A two-pass macro processor is a program used in assembly language programming to


handle macros, which are shorthand notations representing a sequence of instructions. Pass-I of a
two-pass macro processor involves scanning the source code to identify and expand macros,
while generating an intermediate representation for further processing in Pass-II. To implement
Pass-I efficiently, suitable data structures need to be designed.

Firstly, a symbol table is essential to store information about macros encountered during Pass-I.
This table typically contains entries for each macro, including its name, parameters, and the
location of its definition in the source code. This allows for quick retrieval and manipulation of
macro information during processing.

Secondly, a macro definition table is necessary to store the expanded form of each macro
encountered in the source code. This table maps each macro name to its expanded sequence of
instructions. As macros can be defined multiple times in different parts of the source code, this
table ensures consistency in macro expansion throughout the program.

Thirdly, a macro call stack can be utilized to handle nested macro invocations. As macros may
contain other macros within their definitions, a stack-based approach allows for the proper
handling of nested expansions. Each time a macro is called, its parameters and expansion are
pushed onto the stack, and popped off once the expansion is complete.

Additionally, a location counter is maintained to keep track of the current position in the source
code being processed. This counter is incremented as instructions are read, allowing for accurate
determination of the location of macro invocations and expansions.

Finally, a suitable input buffer is required to efficiently read and process the source code. This
buffer stores a portion of the source code being analyzed, allowing for sequential scanning and
parsing during Pass-I. As the buffer is processed, macro invocations are identified and expanded,
and the resulting intermediate representation is generated for further processing in Pass-II.

Algorithm For Nested Macro:

1. Open the source file for reading.


2. Create file writers for MNT (Macro Name Table), MDT (Macro Definition Table), KPDT
(Keyword Parameter Definition Table), PNT (Parameter Name Table), and intermediate
file.
3. Initialize necessary variables like Macroname, mdtp, kpdtp, paramNo, pp, kp, and flag.
4. Read the source file line by line.
5. Split each line into parts based on whitespace.
6. If the first part of the line is "MACRO":
a. Set flag to 1.
b. Read the next line to get the macro name and its parameters.
c. Process parameters and update pntab (Parameter Name Table) and KPDT
(Keyword Parameter Definition Table).
d. Write macro details to MNT (Macro Name Table).
e. Increment mdtp and adjust kpdtp.
7. If the first part of the line is "MEND":
a. Write the line to MDT (Macro Definition Table).
b. Reset flag, kp, pp, paramNo.
c. Write parameters to PNT (Parameter Name Table).
d. Clear pntab.
8. If flag is 1:
a. Process each part of the line:
i. If it contains "&", replace "&" with empty string and write parameter index
to MDT.
ii. Otherwise, write the part directly to MDT.
b. Increment mdtp.
9. If flag is not 1, write the line to the intermediate file.
10. Close all file readers and writers.
11. Print a message indicating the completion of Pass-I processing.
Code:

Nested Macro: Main.java

import java.io.BufferedReader;

import java.io.FileReader; import

java.io.FileWriter; import
java.io.IOException; import
java.util.Iterator; import
java.util.LinkedHashMap; public class
Main { public static void
main(String[] args) throws
IOException{
BufferedReader br=new BufferedReader(new FileReader("macro_input.asm"));

FileWriter mnt=new FileWriter("mnt.txt");


FileWriter mdt=new FileWriter("mdt.txt");
FileWriter kpdt=new FileWriter("kpdt.txt");
FileWriter pnt=new FileWriter("pntab.txt");
FileWriter ir=new FileWriter("intermediate.txt");
LinkedHashMap<String, Integer> pntab=new LinkedHashMap<>();
String line;
String Macroname = null;
int mdtp=1,kpdtp=0,paramNo=1,pp=0,kp=0,flag=0;
while((line=br.readLine())!=null)
{

String parts[]=line.split("\\s+"); if(parts[0].equalsIgnoreCase("MACRO"))


{ flag=1; line=br.readLine();
parts=line.split("\\s+");
Macroname=parts[0];
if(parts.length<=1)
{

mnt.write(parts[0]+"\t"+pp+"\t"+kp+"\t"+mdtp+"\t"+(kp==0?kpdtp:(kpdtp+1))+"\n"); continue;
} for(int i=1;i<parts.length;i++) //processing of
parameters
{
parts[i]=parts[i].replaceAll("[&,]", "");
//System.out.println(parts[i]); if(parts[i].contains("=")) {
++kp;
String keywordParam[]=parts[i].split("=");
pntab.put(keywordParam[0], paramNo++);
if(keywordParam.length==2)
{
kpdt.write(keywordParam[0]+"\t"+keywordParam[1]+"\n");
}
else
{ kpdt.write(keywordParam[0]+"\t-\n"); }
}
else
{ pntab.put(parts[i], paramNo++); pp+
+;
}
}

mnt.write(parts[0]+"\t"+pp+"\t"+kp+"\t"+mdtp+"\t"+(kp==0?kpdtp:(kpdtp+1))+"\n");
kpdtp=kpdtp+kp;
//System.out.println("KP="+kp);

}
else if(parts[0].equalsIgnoreCase("MEND"))
{ mdt.write(line+"\n");
flag=kp=pp=0;
mdtp++;
paramNo=1;
pnt.write(Macroname+":\t");
Iterator<String> itr=pntab.keySet().iterator();
while(itr.hasNext())
{ pnt.write(itr.next()+"\t");
} pnt.write("\
n");
pntab.clear();
} else
if(flag==1)
{ for(int i=0;i<parts.length;i++)
{ if(parts[i].contains("&"))
{ parts[i]=parts[i].replaceAll("[&,]", "");
mdt.write("(P,"+pntab.get(parts[i])+")\t");
}
else
{ mdt.
write
(part
s[i]+
"\t");
}
} mdt.write("\n");
mdtp++; } else
{ ir.write(line+"\n");
}
} br.close();
mdt.close()
;
mnt.close()
; ir.close();
pnt.close();
kpdt.close();
System.out.println("MAcro PAss1 Processing done. :)"); }

Macro_input.asm

MACRO
M1 &X, &Y, &A=AREG, &B=
MOVER &A, &X
ADD &A, ='1'
MOVER &B, &Y
ADD &B, ='5'
MEND
MACRO
M2 &P, &Q, &U=CREG, &V=DREG
MOVER &U, &P
MOVER &V, &Q
ADD &U, ='15'
ADD &V, ='10' MEND
START 100
M1 10, 20, &B=CREG
M2 100, 200, &V=AREG, &U=BREG
END

Output:
Conclusion:

From this assignment I understood about the data structures of pass-1 of a two pass macro
processor. I’ve learned the significance of efficient symbol, macro, and parameter tables in
managing macro processing tasks. In this assignment I have implemented data structures of pass1
of a two pass macro processor.

You might also like