Skip to content

Instantly share code, notes, and snippets.

@Vivek-abstract
Created April 9, 2018 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vivek-abstract/4ae7163a88d4ee794859ed6db414dca5 to your computer and use it in GitHub Desktop.
Save Vivek-abstract/4ae7163a88d4ee794859ed6db414dca5 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class MacroPass2 {
public static void main(String[] args) {
String[][] replacements = {{"&ARG0", "#0"},
{"&ARG1", "#1"},
{"&ARG2", "#2"},
{"&ARG3", "#3"}};
String line=null;
try {
FileReader fileReader = new FileReader("prog.txt");
BufferedReader fr = new BufferedReader(fileReader);
fr.mark(0); //Set checkpoint for later reset
int t=1;
String[] mdt = new String[100];
String[] mnt = new String[100];
String[] ala = new String[100];
String elements[] = new String[100];
int mdtc = 0, mntc = 0, flag = 0, k = 0;
while((line = fr.readLine()) != null) {
if(line.equals("MACRO")) {
do {
line = fr.readLine();
if(flag == 0) {
elements= line.split("[ ,]");
mnt[mntc++] = elements[0] + " " + mdtc;
flag = 1;
}
if(line != null) {
for(String[] replacement: replacements) {
line = line.replace(replacement[0], replacement[1]);
}
mdt[mdtc++] = line;
} else {
break;
}
} while(!line.equals("MEND"));
}
flag = 0;
}
//Pass 1 is finished now we start pass 2
FileReader fileReader2 = new FileReader("prog.txt");
BufferedReader fr2 = new BufferedReader(fileReader2);
while((line = fr2.readLine()) != null) {
if(line.equals("MACRO")) {
//Skip definition
while(!line.equals("MEND")) {
line = fr2.readLine();
}
} else {
String temp[] = line.split("[ ,]");
if(contains(mnt, mntc, temp[0]) == true) {
k = 0;
for(int j = 1; j < temp.length; j++) {
//Current instruction is a macro instruction so update ALA with arguments
ala[k++] = temp[j];
}
}
}
}
System.out.println("\n***** MDT: *****\n");
for(int i = 0; i < mdtc; i++) {
System.out.println(i + "\t" + mdt[i]);
}
System.out.println("\n***** MNT: *****\n");
for(int i = 0; i < mntc; i++) {
System.out.println(i + "\t" + mnt[i]);
}
System.out.println("\n***** ALA *****\n");
for(int i = 0; i < k; i++) {
System.out.println("#" + i + "\t" + ala[i]);
}
fr.close();
}
catch (Exception e) {
System.out.println(e);
}
}
public static boolean contains(String mnt[], int mntc, String macroName) {
for(int i = 0; i < mntc; i++) {
String temp[] = mnt[i].split(" ");
if(temp[0].equals(macroName)) {
return true;
}
}
return false;
}
}
/*
INPUT: prog.txt
MACRO
INCR &ARG1,&ARG2,&ARG3
A 1,&ARG1
A 2,&ARG2
A 3,&ARG3
MEND
INCR DATA1,DATA2,DATA3
OUTPUT:
***** MDT: *****
0 INCR #1,#2,#3
1 A 1,#1
2 A 2,#2
3 A 3,#3
4 MEND
***** MNT: *****
0 INCR 0
***** ALA *****
#0 DATA1
#1 DATA2
#2 DATA3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment