You are on page 1of 11

System Programming and Operating System TE Computer Engineering (2021-22)

Name: Omkar Thange


Roll No.: 19121014
Subject: System Programming and Operating System
_______________________________________________________________________________

Assignment No.: 03

Title:
Design suitable data structures and implement Pass-II of a two pass macro processor using
OOP features in Java/C++. The output of Pass-I (MNT, MDT, ALA & Intermediate code file without
any macro definitions) should be input for Pass-II.

Objectives:
1. To identify and design different data structure used in macro-processor implementation
2. To apply knowledge in implementation of pass-2 of two pass microprocessor.

Hardware Requirement:
PC/Laptop

Software Requirement:
1. Notepad 2. JDK 3. CMD

Theory:

1. Explain design steps of two pass macro processor, types of statements, data structures
required and flowcharts.

Pass II:
a. The copy of the input from Pass I.
b. The output-expanded source to be given to assembler.
c. MDT, created by Pass I.
d. MNT, created by Pass I.
e. Macro-Definition Table Pointer (MDTP), used to indicate the next line of text to be
used during macro-expansion.
f. Argument List Array (ALA), used to substitute macro-call arguments for the index
markers in the stored macro-defn.

In pass2 we perform recognize macro call and perform macro expansion


1. COPY FILE
It is a file it contains the output given from PASS1
2. MNT
It is used for recognizing macro name
3. MDT
It is used to perform macro EXPANSION
4. MDTP
It is used to point to the index of MDT.The starting index is given by MNT
5. ALA

Department of Computer Engineering, GCOEARA Page 1


System Programming and Operating System TE Computer Engineering (2021-22)

It is used to replace the index notation by it actual value


6. ESC
It is used to contain the expanded macro call which is given to the assembler
for further processing

Input:
Intermediate.txt

Department of Computer Engineering, GCOEARA Page 2


System Programming and Operating System TE Computer Engineering (2021-22)

START 100
M1 10, 20, &B=CREG
M2 100, 200, &V=AREG, &U=BREG
END

Mdt.txt
MOVER (P, 3) (P, 1)
ADD (P, 3) ='1'
MOVER (P, 4) (P, 2)
ADD (P, 4) ='5'
MEND
MOVER (P, 3) (P, 1)
MOVER (P, 4) (P, 2)
ADD (P, 3) ='15'
ADD (P, 4) ='10'
MEND

Mnt.txt
M1 2 2 1 1
M2 2 2 6 3

Program Code (Java):

MacroP2.java
import java.io.BufferedReader;

Department of Computer Engineering, GCOEARA Page 3


System Programming and Operating System TE Computer Engineering (2021-22)

import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Vector;

public class MacroP2 {

public static void main(String[] args) throws Exception {


BufferedReader irb=new BufferedReader(new FileReader("intermediate.txt"));
BufferedReader mdtb=new BufferedReader(new FileReader("mdt.txt"));
BufferedReader kpdtb=new BufferedReader(new FileReader("kpdt.txt"));
BufferedReader mntb=new BufferedReader(new FileReader("mnt.txt"));

FileWriter fr=new FileWriter("pass2.txt");

HashMap<String, MNTEntry> mnt=new HashMap<>();


HashMap<Integer, String> aptab=new HashMap<>();
HashMap<String,Integer> aptabInverse=new HashMap<>();

Vector<String>mdt=new Vector<String>();
Vector<String>kpdt=new Vector<String>();

int pp,kp,mdtp,kpdtp,paramNo;
String line;
while((line=mdtb.readLine())!=null)
{
mdt.addElement(line);
}
while((line=kpdtb.readLine())!=null)
{
kpdt.addElement(line);
}
while((line=mntb.readLine())!=null)
{
String parts[]=line.split("\\s+");
mnt.put(parts[0], new MNTEntry(parts[0], Integer.parseInt(parts[1]),
Integer.parseInt(parts[2]), Integer.parseInt(parts[3]), Integer.parseInt(parts[4])));

while((line=irb.readLine())!=null)
{
String []parts=line.split("\\s+");
if(mnt.containsKey(parts[0]))
{
pp=mnt.get(parts[0]).getPp();
kp=mnt.get(parts[0]).getKp();
kpdtp=mnt.get(parts[0]).getKpdtp();

Department of Computer Engineering, GCOEARA Page 4


System Programming and Operating System TE Computer Engineering (2021-22)

mdtp=mnt.get(parts[0]).getMdtp();
paramNo=1;
for(int i=0;i<pp;i++)
{
parts[paramNo]=parts[paramNo].replace(",", "");
aptab.put(paramNo, parts[paramNo]);
aptabInverse.put(parts[paramNo], paramNo);
paramNo++;
}
int j=kpdtp-1;
for(int i=0;i<kp;i++)
{
String temp[]=kpdt.get(j).split("\t");
aptab.put(paramNo,temp[1]);
aptabInverse.put(temp[0],paramNo);
j++;
paramNo++;
}

for(int i=pp+1;i<parts.length;i++)
{
parts[i]=parts[i].replace(",", "");
String splits[]=parts[i].split("=");
String name=splits[0].replaceAll("&", "");
aptab.put(aptabInverse.get(name),splits[1]);
}
int i=mdtp-1;
while(!mdt.get(i).equalsIgnoreCase("MEND"))
{
String splits[]=mdt.get(i).split("\\s+");
fr.write("+");
for(int k=0;k<splits.length;k++)
{
if(splits[k].contains("(P,"))
{
splits[k]=splits[k].replaceAll("[^0-9]", "");//not
containing number
String
value=aptab.get(Integer.parseInt(splits[k]));
fr.write(value+"\t");
}
else
{
fr.write(splits[k]+"\t");
}
}
fr.write("\n");
i++;

Department of Computer Engineering, GCOEARA Page 5


System Programming and Operating System TE Computer Engineering (2021-22)

aptab.clear();
aptabInverse.clear();
}
else
{
fr.write(line+"\n");
}

fr.close();
mntb.close();
mdtb.close();
kpdtb.close();
irb.close();
}
}

MNTEntry.java
public class MNTEntry {
String name;
int pp,kp,mdtp,kpdtp;

public MNTEntry(String name, int pp, int kp, int mdtp, int kpdtp) {
super();
this.name = name;
this.pp = pp;
this.kp = kp;
this.mdtp = mdtp;
this.kpdtp = kpdtp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPp() {
return pp;
}
public void setPp(int pp) {
this.pp = pp;
}
public int getKp() {
return kp;

Department of Computer Engineering, GCOEARA Page 6


System Programming and Operating System TE Computer Engineering (2021-22)

}
public void setKp(int kp) {
this.kp = kp;
}
public int getMdtp() {
return mdtp;
}
public void setMdtp(int mdtp) {
this.mdtp = mdtp;
}
public int getKpdtp() {
return kpdtp;
}
public void setKpdtp(int kpdtp) {
this.kpdtp = kpdtp;
}

Output:
Pass2.txt

START 100
+MOVER AREG 10
+ADD AREG ='1'
+MOVER CREG 20
+ADD CREG ='5'
+MOVER BREG 100
+MOVER AREG 200
+ADD BREG ='15'
+ADD AREG ='10'

Department of Computer Engineering, GCOEARA Page 7


System Programming and Operating System TE Computer Engineering (2021-22)

END

Algorithm/Flowchart(Pass 2 Macro processor):

Algorithm:-

1. Read next source card( copied by pass1).


2. Search MNT for match with operation code.
3. If Macro name found then
a. MDTP<-MDT index form MNT entry.
b. Setup ALA
c. Increment MDTP.
d. Get line form MDT.
e. Substitute arguments from macro call.
f. If MEND then GOTO 1 else GOTO 3.c.

Department of Computer Engineering, GCOEARA Page 8


System Programming and Operating System TE Computer Engineering (2021-22)

4. Else write into expanded source card file


5. If END then, supply expanded source file to assembler processing, else GOTO 1.

Flowchart:

Frequently Asked Questions:

1. What is macro expansion?

Macro Expansion:
– Replacement of macro call by corresponding sequence of Instructions is called as
macro expansion

Source Expanded Source

Macro
INCR
A 1,Data

Department of Computer Engineering, GCOEARA Page 9


System Programming and Operating System TE Computer Engineering (2021-22)

A 2,Data
A 3,Data
MEND
:
:
INCR A 1,Data
A 2,Data
A 3,Data
:
:
INCR A 1,Data
END A 2,Data
A 3,Data

2. Define purpose of pass-2 of two pass macro processor.

In Pass-II the macro calls are identified and the arguments are placed in the appropriate
place and the macro calls are replaced by macro definitions.

3. What is positional arguments?

Positional argument:
Argument are matched with dummy arguments according to order in which they appear.
E.g INCR A, B, C
• “A” replaces first dummy argument.
• “B” replaces second dummy argument
• “C‟ replaces third dummy argument

4. What is the use of MDT-index field in MNT?

Each MNT entry consist of a pointer (index) to the entry in MDT that corresponds to the
beginning of the macro- definition (MDT index).

5. What is the use of MNT table while processing macro call?

MNT table is used to store:


1. A character string (the macro name) &
2. A pointer (index) to the entry in MDT that corresponds to the beginning of the macro-
definition(MDT index)

Conclusion:

Department of Computer Engineering, GCOEARA Page 10


System Programming and Operating System TE Computer Engineering (2021-22)

We have successfully completed implementation of Pass-II of macro processor.

Department of Computer Engineering, GCOEARA Page 11

You might also like