A Java Program for Pass Two Assembler
//===============================================
// Name : PassTwo.java
// Author : Rucha Tambe
// Version :
// Copyright : Your copyright notice
// Description : Pass Two Assembler in Java
//================================================
Inputs:
sym.txt
A 100
L1 103
B 109
C 100
D 101
L2 107
L1 103
B 109
C 100
D 101
L2 107
lit.txt
12 106
5 110
5 110
pool.txt
0
1
1
PassTwo.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
public class PassTwo {
ArrayList<Integer> symaddr=new ArrayList<>();
ArrayList<String> littab=new ArrayList<>();
ArrayList<Integer> litaddr=new ArrayList<>();
ArrayList<Integer> pooltab=new ArrayList<>();
public void readFile() throws Exception {
BufferedReader br=new BufferedReader(new FileReader("sym.txt"));
String line=" ";
while((line=br.readLine())!=null) {
String split[]=line.split("\\s+");
symaddr.add(Integer.parseInt(split[1]));
}
BufferedReader br2=new BufferedReader(new FileReader("lit.txt"));
while((line=br2.readLine())!=null) {
String split[]=line.split("\\s+");
littab.add(split[0]);
litaddr.add(Integer.parseInt(split[1]));
}
BufferedReader br3=new BufferedReader(new FileReader("pool.txt"));
while((line=br3.readLine())!=null) {
pooltab.add(Integer.parseInt(line));
}
}
public void generateMachineCode() throws Exception {
String line=" ";
BufferedReader br=new BufferedReader(new FileReader("ic.txt"));
int p=0;
while((line=br.readLine())!=null) {
String split[]=line.split("\\s+");
String sp[]=split[0].replace("(","").replace(")", "").split("\\,");
if(sp[0].equals("DL")) {
String arr[]=split[1].replace("(", "").replace(")", "").split("\\,");
System.out.println("+00 0 "+String.format("%03d", Integer.parseInt(arr[1])));
}
else if(sp[0].equals("IS")) {
System.out.print(sp[1]+" ");
int i=1;
while(i<split.length) {
String arr[]=split[i].replace("(", "").replace(")","").split("\\,");
if(arr[0].equals("S")) {
System.out.println(symaddr.get(Integer.parseInt(arr[1])));
}
else if(arr[0].equals("L")) {
System.out.println(litaddr.get(Integer.parseInt(arr[1])));
}
else if(arr[0].equals("C")) {
System.out.println((Integer.parseInt(arr[1])));
}
else {
System.out.print(arr[0]+" ");
}
i++;
}
}
else if(sp[0].equals("AD") && (sp[1].equals("04") || sp[1].equals("05"))){
if(sp[1].equals("05")) {
for(int i=pooltab.get(p);i<pooltab.get(p+1);i++) {
System.out.println("+00 0 "+Integer.parseInt(littab.get(i)));
}
}
else {
for(int i=pooltab.get(p);i<littab.size();i++) {
System.out.println("+00 0"+Integer.parseInt(littab.get(i)));
}
}
p++;
}
}
}
public static void main(String[] args) throws Exception {
PassTwo p=new PassTwo();
p.readFile();
p.generateMachineCode();
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
public class PassTwo {
ArrayList<Integer> symaddr=new ArrayList<>();
ArrayList<String> littab=new ArrayList<>();
ArrayList<Integer> litaddr=new ArrayList<>();
ArrayList<Integer> pooltab=new ArrayList<>();
public void readFile() throws Exception {
BufferedReader br=new BufferedReader(new FileReader("sym.txt"));
String line=" ";
while((line=br.readLine())!=null) {
String split[]=line.split("\\s+");
symaddr.add(Integer.parseInt(split[1]));
}
BufferedReader br2=new BufferedReader(new FileReader("lit.txt"));
while((line=br2.readLine())!=null) {
String split[]=line.split("\\s+");
littab.add(split[0]);
litaddr.add(Integer.parseInt(split[1]));
}
BufferedReader br3=new BufferedReader(new FileReader("pool.txt"));
while((line=br3.readLine())!=null) {
pooltab.add(Integer.parseInt(line));
}
}
public void generateMachineCode() throws Exception {
String line=" ";
BufferedReader br=new BufferedReader(new FileReader("ic.txt"));
int p=0;
while((line=br.readLine())!=null) {
String split[]=line.split("\\s+");
String sp[]=split[0].replace("(","").replace(")", "").split("\\,");
if(sp[0].equals("DL")) {
String arr[]=split[1].replace("(", "").replace(")", "").split("\\,");
System.out.println("+00 0 "+String.format("%03d", Integer.parseInt(arr[1])));
}
else if(sp[0].equals("IS")) {
System.out.print(sp[1]+" ");
int i=1;
while(i<split.length) {
String arr[]=split[i].replace("(", "").replace(")","").split("\\,");
if(arr[0].equals("S")) {
System.out.println(symaddr.get(Integer.parseInt(arr[1])));
}
else if(arr[0].equals("L")) {
System.out.println(litaddr.get(Integer.parseInt(arr[1])));
}
else if(arr[0].equals("C")) {
System.out.println((Integer.parseInt(arr[1])));
}
else {
System.out.print(arr[0]+" ");
}
i++;
}
}
else if(sp[0].equals("AD") && (sp[1].equals("04") || sp[1].equals("05"))){
if(sp[1].equals("05")) {
for(int i=pooltab.get(p);i<pooltab.get(p+1);i++) {
System.out.println("+00 0 "+Integer.parseInt(littab.get(i)));
}
}
else {
for(int i=pooltab.get(p);i<littab.size();i++) {
System.out.println("+00 0"+Integer.parseInt(littab.get(i)));
}
}
p++;
}
}
}
public static void main(String[] args) throws Exception {
PassTwo p=new PassTwo();
p.readFile();
p.generateMachineCode();
}
}
No comments:
Post a Comment