diff --git a/Codes/PASS I Micro.txt b/Codes/PASS I Micro.txt new file mode 100644 index 0000000..16db3ce --- /dev/null +++ b/Codes/PASS I Micro.txt @@ -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: diff --git a/Codes/PASS2MACRO.txt b/Codes/PASS2MACRO.txt new file mode 100644 index 0000000..2d46d34 --- /dev/null +++ b/Codes/PASS2MACRO.txt @@ -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"); +HashMapmnt=new HashMap(); +HashMapaptab=new HashMap(); +HashMapaptabinverse=new HashMap(); +Vectormdt=new Vector(); +Vectorkpdt=new Vector(); +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 +#include +using namespace std; +int main(){ +vector mdt; +vector mnt; +vector 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"< AD; +HashMap IS; +HashMap CC; +HashMap RG; +HashMap 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