-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPass2.java
More file actions
144 lines (111 loc) · 3.86 KB
/
Copy pathPass2.java
File metadata and controls
144 lines (111 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
public class Pass2 {
public static void main(String[] args) {
try {
String f = "IC.txt";
FileReader fw = new FileReader(f);
BufferedReader IC_file = new BufferedReader(fw);
String f1 = "SYMTAB.txt";
FileReader fw1 = new FileReader(f1);
BufferedReader symtab_file = new BufferedReader(fw1);
symtab_file.mark(500);
String f2 = "LITTAB.txt";
FileReader fw2 = new FileReader(f2);
BufferedReader littab_file = new BufferedReader(fw2);
littab_file.mark(500);
String littab[][]=new String[10][2] ;
Hashtable<String, String> symtab = new Hashtable<String, String>();
String str;
int z=0;
while ((str = littab_file.readLine()) != null) {
littab[z][0]=str.split("\t")[0];
littab[z][1]=str.split("\t")[1];
z++;
}
while ((str = symtab_file.readLine()) != null) {
symtab.put(str.split("\t")[0], str.split("\t")[1]);
}
String f4 = "MACHINE_CODE.txt";
FileWriter fw4 = new FileWriter(f4);
BufferedWriter machine_code_file = new BufferedWriter(fw4);
String sCurrentLine;
sCurrentLine = IC_file.readLine();
int locptr=0;
locptr=Integer.parseInt(sCurrentLine.split("\t")[3]);
while ((sCurrentLine = IC_file.readLine()) != null) {
String s0 = sCurrentLine.split("\t")[0];
System.out.print("The value of so: "+s0);
String s1 = sCurrentLine.split("\t")[1];
//if((!s0.equals("AD")) && (!s1.equals("02")))
machine_code_file.write(locptr+"\t"); //always write the LC
String s2=" ";
if (sCurrentLine.split("\t").length>2)
s2=sCurrentLine.split("\t")[3];
if (s0.equals("IS")) {
machine_code_file.write(s1 + "\t");
if (sCurrentLine.split("\t").length == 5) {
machine_code_file.write(sCurrentLine.split("\t")[2] + "\t");
if (sCurrentLine.split("\t")[3].equals("L")) {
int add = Integer.parseInt(sCurrentLine.split("\t")[4]);
machine_code_file.write(littab[add-1][1]);
}
if (sCurrentLine.split("\t")[3].equals("S")) {
int add1 = Integer.parseInt(sCurrentLine.split("\t")[4]);
int i = 1;
String l1;
for (Map.Entry m : symtab.entrySet()) {
if (i == add1) {
machine_code_file.write((String) m.getValue());
}
i++;
}
}
} else {
machine_code_file.write("0\t000");
}
}
if(s0.equals("DL")&&s1.equals("02")){ //if it is DS stmt
machine_code_file.write("00\t0\t00"+s2);
}
if(s0.equals("AD")&&s1.equals("02"))//if it is "END" stmt
{
String s;
littab_file.reset();
int y=0;
while((s = littab_file.readLine()) != null) {
String x=s.split("\t")[0];
String add=" ";
for(int i=0;i<z;i++)
{
if(x.equals(littab[i][0]))
{
add=littab[i][1];
}
}
//machine_code_file.write("Addre3ss to the literals: \n");
if(y>0)
machine_code_file.write(add+"\t");
y++;
machine_code_file.write("00\t0\t00" + s.split("'")[1]+"\n");
}
}
locptr++;
machine_code_file.write("\n");
}
IC_file.close();
symtab_file.close();
littab_file.close();
machine_code_file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}