Professional Documents
Culture Documents
Assignment 3 (SPOS)
Assignment 3 (SPOS)
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.
Input:
Intermediate.txt
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
MacroP2.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Vector;
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();
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++;
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;
}
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'
END
Algorithm:-
Flowchart:
Macro Expansion:
– Replacement of macro call by corresponding sequence of Instructions is called as
macro expansion
Macro
INCR
A 1,Data
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
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.
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
Each MNT entry consist of a pointer (index) to the entry in MDT that corresponds to the
beginning of the macro- definition (MDT index).
Conclusion: