3
1

Added pass 1 and 2 codes in testing branch.

This commit is contained in:
K 2024-09-30 10:49:17 +05:30
parent a4f3f5d7ba
commit e21b005f20
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
4 changed files with 533 additions and 0 deletions

88
Codes/PASS I Micro.txt Normal file
View File

@ -0,0 +1,88 @@
class pass1macro:
def __init__(self,filename):
self.filename = filename
self.lines = []
with open(filename, "r") as input_file:
for line in input_file:
self.lines.append(line.rstrip().split(" "))
def pass1(self):
self.mdt = []
self.mnt = {}
self.pntab = {}
self.kptab = {}
self.kptr = 100
self.mdtptr = 0
isInMacro = False
i = 0
macro_name = ""
while i < len(self.lines):
if self.lines[i][0] == "MACRO" and not isInMacro:
isInMacro = True
i += 1
macro_name = self.lines[i][0]
params = self.lines[i][1].split(",")
startptr = self.kptr
self.pntab[macro_name] = {}
kp = 0
for param in params:
if "=" in param:
kp += 1
key = param.split("=")[0]
if len(param.split("=")) == 2:
val = param.split("=")[1]
else:
val = ""
self.kptab[self.kptr] = (key, val)
self.kptr += 1
self.pntab[macro_name][len(self.pntab[macro_name]) + 1] = param.split("=")[0]
self.mnt[macro_name] = (len(self.pntab[macro_name])-kp, kp, self.mdtptr, startptr)
elif self.lines[i][0] == "MEND" and isInMacro:
macro_name = ""
isInMacro = False
self.mdt.append(self.lines[i][0])
self.mdtptr += 1elif isInMacro:
inst = " ".join(self.lines[i])
for key in self.pntab[macro_name]:
inst = inst.replace(self.pntab[macro_name][key], f"(P,{key})")
self.mdt.append(inst)
self.mdtptr += 1
i += 1
print("Macro Name Table:")
# print("Name PP KP MDTP KPDTP")
# for key,val in self.mnt.items():
# print(f"{key} {val[0]} {val[1]} {val[2] }")
print(self.mnt)
print("Keyword Parameter Name Table:")
print(self.kptab)
print("Parameter Name Table:")
print(self.pntab)
print("Macro Definition Table:")
print(self.mdt)
obj = pass1macro("prog1.asm")
obj.pass1()
Testcase:
MACRO
ONE &O,&N,&E=AREG
MOVER &E,&O
ADD &E,&N
MOVEM &E,&O
MEND
MACRO
TWO &T,&W,&O=DREG
MOVER &O,&T
ADD &O,&W
MOVEM &O,&T
MEND
START
READ O
READ T
ONE O,9
TWO T,7
STOP
O DS 1
T DS 1
END
Output:

202
Codes/PASS2MACRO.txt Normal file
View File

@ -0,0 +1,202 @@
INPUT FILES:
mnt.txt
ONE 2 1 1 1
TWO 2 1 5 2
mdt.txt
MOVER (P,3) (P,1)
ADD (P,3) (P,2)
MOVEM (P,3) (P,1)
MEND
MOVER (P,3) (P,1)
ADD (P,3) (P,2)
MOVEM (P,3) (P,1)
MOVER CREG, (P,1)
ADD CREG,9
MOVEM CREG, (P,1)
MEND
ir.txt
START
READ O
READ T
TWO T, 7
PRINT O
PRINT T
STOP
O DS 1
T DS 1
END
kpdt.txt
1 E AREG
2 O DREG
CODE:
package macroP2;
import java.io.BufferedReader;import
import
import
import
import
java.io.FileReader;
java.io.FileWriter;
java.io.IOException;
java.util.HashMap;
java.util.Vector;
public class macrop2 {
public static void main (String[] args) throws IOException {
BufferedReader mntb=new BufferedReader(new FileReader("mnt.txt"));
BufferedReader mdtb=new BufferedReader(new FileReader("mdt.txt"));
BufferedReader kpdtb=new BufferedReader(new FileReader("kpdt.txt"));
BufferedReader irb=new BufferedReader(new FileReader("ir.txt"));
FileWriter fr=new FileWriter("pass2.txt");
HashMap<String,MNTEntry>mnt=new HashMap<String,MNTEntry>();
HashMap<Integer,String>aptab=new HashMap<Integer,String>();
HashMap<String,Integer>aptabinverse=new HashMap<String,Integer>();
Vector<String>mdt=new Vector<String>();
Vector<String>kpdt=new Vector<String>();
String line;
int mdtp,kpdtp,pp,kp,paramNo;
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(" ");
mnt.put(parts[0], new
MNTEntry(parts[0],Integer.parseInt(parts[1]),Integer.parseInt(parts[2]),Integer.pa
rseInt(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(" ");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 []split=parts[i].split("=");
String name=split[0].replace("&", "");
aptab.put(aptabinverse.get(name),split[1]);
paramNo++;
}
int i=mdtp-1;
while(!mdt.get(i).equalsIgnoreCase("MEND"))
{
String splits[]=mdt.get(i).split(" ");
fr.write("+");
for(int k=0;k<splits.length;k++)
{
if(splits[k].contains("(P,"))
{
splits[k]=splits[k].replaceAll("[^0-9]", "");
String
value=aptab.get(Integer.parseInt(splits[k]));
fr.write(value+"\t");
}
else
{
fr.write(splits[k]+"\t");
}
}
fr.write("\n");
i++;
}
}
else
{
}
aptab.clear();
aptabinverse.clear();
fr.write(line+"\n");
}
fr.close();
mntb.close();
mdtb.close();
kpdtb.close();
irb.close();
System.out.println("Macro Pass2 done!");
}
}MNTEntry
package macroP2;
public class MNTEntry {
int pp,kp,mdtp,kpdtp;
String S;
public MNTEntry(String s,int pp,int kp,int mdtp,int kpdtp) {
this.S=s;
this.pp=pp;
this.kp=kp;
this.mdtp=mdtp;
this.kpdtp=kpdtp;
}
public String getname() {
return S;
}
public int getpp() {
return pp;
}
}
public void setpp(int data) {
this.pp=data;
}
public void setkp(int data) {
this.kp=data;
}
public void setmdtp(int data) {
this.mdtp=data;
}
public void setkpdtp(int data) {
this.kpdtp=data;
}
public int getkp() {
return kp;
}
public int getmdtp() {
return mdtp;
}
public int getkpdtp() {
return kpdtp;
}OUTPUT:
pass2.txt
START
READ O
READ T
+MOVER
DREG
+ADD DREG 7
+MOVEM
DREG
+MOVER
CREG,
+ADD CREG,9
+MOVEM
CREG,
PRINT O
PRINT T
STOP
O DS 1
T DS 1
END
T
T
T
T

118
Codes/PASSS2MACRO.txt Normal file
View File

@ -0,0 +1,118 @@
Title : Design suitable data structures and implement Pass 1 of two pass macro-processor
CODE
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<string> mdt;
vector<string> mnt;
vector <char> pnt[4];
fstream file;
int pointer=0;
file.open("macro.txt",ios::in);
string s = "",str="";
int c = 0,r=0,a=-1,b=0;
while(getline(file,s)){
if(s=="MACRO"){
b=1;
c=1;
a++;
continue;
}
for(auto it:s){
if(r==1){
int z=0;
for(auto x:pnt[a]){
if(x==it){
z=1;
}
}
if(z==0){
pnt[a].push_back(it);
}
r=0;
}
if(it=='&'){
r=1;}
}
if(b==1){
str="";
int v=0;
char t=s[v];
while(t!=' '){
str=str+t;
v++;
t=s[v];
}
mnt.push_back(str);
b=0;
}
if(c==1){
mdt.push_back(s);
}
if(s=="MEND"){
c=0;
}
}
file.close();
cout<<"MDT"<<endl;
for(auto it:mdt){
cout<<it<<endl;
}
cout<<endl;
for(int i = 0 ; i<=a ; i++){
cout<<"PNT"<<i<<endl;
for(auto it:pnt[i]){
cout<<it<<endl;
}
cout<<endl;
}
cout<<"MNT"<<endl;
for(auto it:mnt){
cout<<it<<endl;
}
return 0;
}Input File : macro.txt
MACRO
ONE &O,&N,&E=AREG
MOVER &E,&O
ADD &E,&N
MOVEM &E,&O
MEND
MACRO
TWO &T,&W,&O=DREG
MOVER &O,&T
ADD &O,&W
MOVEM &O,&T
MEND
START
READ O
READ T
ONE O,9
TWO T,7
STOP
O DS 1
T DS 1
END
OUTPUT :
MDT
ONE &O,&N,&E=AREG
MOVER &E,&O
ADD &E,&N
MOVEM &E,&O
MEND
TWO &T,&W,&O=DREG
MOVER &O,&T
ADD &O,&W
MOVEM &O,&T
MEND
PNT0
O
N
E
PNT1
T
WO
MNT
ONE
TWO

125
Codes/Pass I Assembler.txt Normal file
View File

@ -0,0 +1,125 @@
Design suitable data structures and implement pass1 of a two pass asmbler
import java.util.*;
import java.io.*;
class TableRow{
String name;
int index;
int address;
TableRow(String name,int address){
super();
this.name=name;
this.address=address;
this.index=0;
}
TableRow(String name,int index,int address){
this.address=address;
this.name=name;
this.index=index;
}
public void setName(String name) {
this.name=name;
}
public void setIndex(int index) {
this.index=index;
}
public void setAddress(int address) {
this.address=address;
}
public String getName() {
return this.name;
}
public int getIndex() {
return this.index;
}
public int getAddress() {
return this.address;
}
}
class Initialize{
HashMap<String,Integer> AD;
HashMap<String,Integer> IS;
HashMap<String,Integer> CC;
HashMap<String,Integer> RG;
HashMap<String,Integer> DL;
Initialize(){
AD=new HashMap<>();
IS=new HashMap<>();CC=new HashMap<>();
RG=new HashMap<>();
DL=new HashMap<>();
AD.put("START",1);
AD.put("END",2);
AD.put("ORIGIN",3);
AD.put("EQU",4);
AD.put("LTORG",5);
DL.put("DC", 1);
DL.put("DS", 2);
RG.put("AREG", 1);
RG.put("BREG", 2);
RG.put("CREG", 3);
RG.put("DREG", 4);
CC.put("LT", 1);
CC.put("LTE",2);
CC.put("EQ", 3);
CC.put("NEQ",4);
CC.put("GT",5);
CC.put("GTE",6);
IS.put("STOP",0);
IS.put("ADD",1);
IS.put("SUB",2);
IS.put("MULT",3);
IS.put("MOVER",4);
IS.put("MOVEM",5);
IS.put("COMP",6);
IS.put("BC",7);
IS.put("DIV",8);
IS.put("READ", 9);
IS.put("PRINT",10);
}
}
class assembler {
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
FileWriter pass1=new FileWriter("pass1.txt");
FileWriter symtab=new FileWriter("symtab.txt");
Initialize in=new Initialize();
int symindex=0;
int index=0;
String line;int flag=0;
while((line=br.readLine())!=null) {
String parts[]=line.split("\\s+");
for(int i=0;i<parts.length;i++) {
parts[i]=parts[i].replaceAll(",","");
if((in.AD.containsKey(parts[i]))==false) {
if((in.CC.containsKey(parts[i]))==false) {
if((in.DL.containsKey(parts[i]))==false){
if((in.IS.containsKey(parts[i]))==false) {
if((in.RG.containsKey(parts[i]))==false) {
symtab.write(parts[i]+"\t"+ +
+symindex+"\n");
}
}
}
}
}
}
}
symtab.close();
br.close();
pass1.close();
}
}
INPUT:
START 100
L1 MOVER AREG, X
MOVEM AREG, Y
ORIGIN L1+3
NEXT ADD AREG, X
SUB BREG, Y
BC LT, L1
ORIGIN NEXT+5
MULT CREG, Z
STOP
X DS 2
Y DS 1
Z DC '9'
END