-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pass-2 Assembler.java
192 lines (143 loc) · 2.9 KB
/
Pass-2 Assembler.java
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Program to implement Pass-2 assembler.
import java.io.*;
import java.util.*;
class Pass-2_Assembler
{
public static void main(String args[])throws Exception
{
String s;
String s1[]=new String[70];
String s2[]=new String[70];
String s3[]=new String[70];
FileWriter f1=new FileWriter("output3.txt");
BufferedWriter b1=new BufferedWriter(f1);
FileReader f2=new FileReader("intermedi1.txt");
BufferedReader b2=new BufferedReader(f2);
FileReader f3=new FileReader("symbol2.txt");
BufferedReader b3=new BufferedReader(f3);
FileReader f4=new FileReader("literal2.txt");
BufferedReader b4=new BufferedReader(f4);
int m=0;
while((s=b2.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
s1[m]=st.nextToken();
m++;
}
}
int m1=0;
while((s=b3.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
s2[m1]=st.nextToken();
m1++;
}
}
int m2=0;
while((s=b4.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(s);
while(st.hasMoreTokens())
{
s3[m2]=st.nextToken();
m2++;
}
}
for(int i=0;i<m;i++)
{
if("AD".equals(s1[i]) && "01".equals(s1[i+1]))
{
b1.write("");
}
if("R1".equals(s1[i])||"R2".equals(s1[i])||"R3".equals(s1[i])||"R4".equals(s1[i]))
{
b1.write("\t"+s1[i]);
b1.write(" ");
}
if("IS".equals(s1[i]))
{
b1.write(s1[i-1]+"\t");
b1.write(s1[i+1]);
}
if("L".equals(s1[i]))
{
for(int j=0;j<m2;j++)
{
if(s1[i+1].equals(s3[j]))
{
b1.write("\t");
b1.write(s3[j+2]+"\n");
}
}
}
if("S".equals(s1[i]))
{
for(int j=0;j<m1;j++)
{
if(s1[i+1].equals(s2[j]))
{
b1.write("\t");
b1.write(s2[j+2]+"\n");
}
}
}
if("DL".equals(s1[i])&&"01".equals(s1[i+1]))
{
b1.write(s1[i-1]+"\t");
b1.write("-"+"\t");
b1.write("-");
b1.write("\t");
b1.write("00"+s1[i+3]+"\n");
}
if(s1[i].equals("AD") && s1[i+1].equals("02"))
{
b1.write(s1[i-1]+"\t");
b1.write("-"+"\t");
b1.write("-");
b1.write("\t");
b1.write("00"+s1[i+3]+"\n");
}
}
b1.close();
b2.close();
b3.close();
b4.close();
}
}
// Input Files:
// intermedi1.txt
// AD 01 C 100
// 100 IS 05 R1 L 1
// 101 IS 06 R2 S 1
// 102 IS 03 R1 L 2
// 103 AD 04 R1 L 2
// 104 IS 06 R1 S 1
// 105
// 106 IS 02
// 107 DL 01
// 108 DL 02
// 109 AD 02 C 1
// 110 AD 02 C 2
// symbol2.txt
// L1 100
// X 107
// Y 108
// literal2.txt
// 1 =7
// 2 =2
// *************************************OUTPUT********************************
// output3.txt
// 100 05 R1 2
// 101 06 R2
// 102 03 R1 null
// R1 null
// 104 06 R1
// 106 02
// 107 - - 00DL
// 109 - - 001
// 110 - - 002
// This code is contributed by Prof. Anand Gharu