-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPass1 Assembler.txt
More file actions
406 lines (372 loc) · 10.1 KB
/
Pass1 Assembler.txt
File metadata and controls
406 lines (372 loc) · 10.1 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
Make 3 class files of PassOne, TableRow, INSTtable.
Make input.txt, IC.txt (for intermediate code), LITTAB.txt,
SYMTAB.txt, POOLTAB.txt.
Set path for respective files in PassOne class and run PassOne
------------------------------------------------------------------------------------------------------
Create input.txt and add following ---------------------------------------------------------------------
START 100
A DS 3
L1 MOVER AREG, B
ADD AREG, C
MOVEM AREG, ='2'
MOVEM AREG, ='3'
D EQU A+1
LTORG
L2 PRINT D
MOVEM AREG, ='4'
MOVEM AREG, ='5'
ORIGIN L2+1
LTORG
B DC '19
C DC '17
END
------------------------------------------------------------------------------------------------------------------
create Java class file INSTtable ----------------------------------------------------------------------------------
import java.util.HashMap;
public class INSTtable {
HashMap<String, Integer> AD,RG,IS,CC,DL;
public INSTtable()
{
AD=new HashMap<>();
CC = new HashMap<>();
IS = new HashMap<>();
RG = new HashMap<>();
DL=new HashMap<String, Integer>();
DL.put("DC", 01);
DL.put("DS", 02);
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);
CC.put("LT",1);
CC.put("LE",2);
CC.put("EQ",3);
CC.put("GT",4);
CC.put("GE",5);
CC.put("ANY",6);
AD.put("START",1);
AD.put("END",2);
AD.put("ORIGIN",3);
AD.put("EQU",4);
AD.put("LTORG",5);
RG.put("AREG",1);
RG.put("BREG",2);
RG.put("CREG",3);
RG.put("DREG",4);
}
public String getType(String s)
{
s=s.toUpperCase();
if(AD.containsKey(s))
return "AD";
else if(IS.containsKey(s))
return "IS";
else if(CC.containsKey(s))
return "CC";
else if(DL.containsKey(s))
return "DL";
else if(RG.containsKey(s))
return "RG";
return "";
}
public int getCode(String s)
{
s = s.toUpperCase();
if(AD.containsKey(s))
return AD.get(s);
else if(IS.containsKey(s))
return IS.get(s);
else if(CC.containsKey(s))
return CC.get(s);
else if(DL.containsKey(s))
return DL.get(s);
else if(RG.containsKey(s))
return RG.get(s);
return -1;
}
}
------------------------------------------------------------------------------------------------------------------
create java class file TableRow-----------------------------
public class TableRow {
String symbol;
int addess,index;
public String getSymbol() {
return symbol;
}
public TableRow(String symbol, int addess) {
super();
this.symbol = symbol;
this.addess = addess;
index=0;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public TableRow(String symbol, int addess, int index) {
super();
this.symbol = symbol;
this.addess = addess;
this.index = index;
}
public int getAddess() {
return addess;
}
public void setAddess(int addess) {
this.addess = addess;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
------------------------------------------------------------------------------------------------------------------
Create java class file PassOne -------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class PassOne {
int lc=0;
int libtab_ptr=0,pooltab_ptr=0;
int symIndex=0,litIndex=0;
LinkedHashMap<String, TableRow> SYMTAB;
ArrayList<TableRow> LITTAB;
ArrayList<Integer> POOLTAB;
private BufferedReader br;
public PassOne()
{
SYMTAB =new LinkedHashMap<>();
LITTAB=new ArrayList<>();
POOLTAB=new ArrayList<>();
lc=0;
POOLTAB.add(0);
}
public static void main(String[] args) {
PassOne one=new PassOne();
try
{
one.parseFile();
}
catch (Exception e) {
System.out.println("Error: "+e);// TODO: handle exception
}
}
public void parseFile() throws Exception
{String prev="";
String line,code;
br = new BufferedReader(new FileReader("input.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("IC.txt"));
INSTtable lookup=new INSTtable();
while((line=br.readLine())!=null)
{
String parts[]=line.split("\\s+");
if(!parts[0].isEmpty()) //processing of label
{
if(SYMTAB.containsKey(parts[0]))
SYMTAB.put(parts[0], new TableRow(parts[0], lc, SYMTAB.get(parts[0]).getIndex()));
else
SYMTAB.put(parts[0],new TableRow(parts[0], lc, ++symIndex));
}
if(parts[1].equals("LTORG"))
{
int ptr=POOLTAB.get(pooltab_ptr);
for(int j=ptr;j<libtab_ptr;j++)
{
lc++;
LITTAB.set(j, new TableRow(LITTAB.get(j).getSymbol(),lc));
code="(DL,01)\t(C,"+LITTAB.get(j).symbol+")";
bw.write(code+"\n");
}
pooltab_ptr++;
POOLTAB.add(libtab_ptr);
}
if(parts[1].equals("START"))
{
lc=expr(parts[2]);
code="(AD,01)\t(C,"+lc+")";
bw.write(code+"\n");
prev="START";
}
else if(parts[1].equals("ORIGIN"))
{
lc=expr(parts[2]);
String splits[]=parts[2].split("\\+"); //Same for - SYMBOL //Add code
code="(AD,03)\t(S,"+SYMTAB.get(splits[0]).getIndex()+")+"+Integer.parseInt(splits[1]);
bw.write(code+"\n");
}
//Now for EQU
if(parts[1].equals("EQU"))
{
int loc=expr(parts[2]);
//below If conditions are optional as no IC is generated for them
if(parts[2].contains("+"))
{
String splits[]=parts[2].split("\\+");
code="(AD,04)\t(S,"+SYMTAB.get(splits[0]).getIndex()+")+"+Integer.parseInt(splits[1]);
}
else if(parts[2].contains("-"))
{
String splits[]=parts[2].split("\\-");
code="(AD,04)\t(S,"+SYMTAB.get(splits[0]).getIndex()+")-"+Integer.parseInt(splits[1]);
}
else
{
code="(AD,04)\t(C,"+Integer.parseInt(parts[2]+")");
}
bw.write(code+"\n");
if(SYMTAB.containsKey(parts[0]))
SYMTAB.put(parts[0], new TableRow(parts[0],loc,SYMTAB.get(parts[0]).getIndex())) ;
else
SYMTAB.put(parts[0], new TableRow(parts[0],loc,++symIndex));
}
if(parts[1].equals("DC"))
{
lc++;
int constant=Integer.parseInt(parts[2].replace("'",""));
code="(DL,01)\t(C,"+constant+")";
bw.write(code+"\n");
}
else if(parts[1].equals("DS"))
{
int size=Integer.parseInt(parts[2].replace("'", ""));
code="(DL,02)\t(C,"+size+")";
bw.write(code+"\n");
/*if(prev.equals("START"))
{
lc=lc+size-1;//System.out.println("here");
}
else
*/ lc=lc+size;
prev="";
}
if(lookup.getType(parts[1]).equals("IS"))
{
code="(IS,0"+lookup.getCode(parts[1])+")\t";
int j=2;
String code2="";
while(j<parts.length)
{
parts[j]=parts[j].replace(",", "");
if(lookup.getType(parts[j]).equals("RG"))
{
code2+=lookup.getCode(parts[j])+"\t";
}
else
{
if(parts[j].contains("="))
{
parts[j]=parts[j].replace("=", "").replace("'", "");
LITTAB.add(new TableRow(parts[j], -1,++litIndex));
libtab_ptr++;
code2+="(L,"+(litIndex)+")";
}
else if(SYMTAB.containsKey(parts[j]))
{
int ind=SYMTAB.get(parts[j]).getIndex();
code2+= "(S,0"+ind+")";
}
else
{
SYMTAB.put(parts[j], new TableRow(parts[j],-1,++symIndex));
int ind=SYMTAB.get(parts[j]).getIndex();
code2+= "(S,0"+ind+")";
}
}
j++;
}
lc++;
code=code+code2;
bw.write(code+"\n");
}
if(parts[1].equals("END"))
{
int ptr=POOLTAB.get(pooltab_ptr);
for(int j=ptr;j<libtab_ptr;j++)
{
lc++;
LITTAB.set(j, new TableRow(LITTAB.get(j).getSymbol(),lc));
code="(DL,01)\t(C,"+LITTAB.get(j).symbol+")";
bw.write(code+"\n");
}
pooltab_ptr++;
POOLTAB.add(libtab_ptr);
code="(AD,02)";
bw.write(code+"\n");
}
}
bw.close();
printSYMTAB();
//Printing Literal table
PrintLITTAB();
printPOOLTAB();
}
void PrintLITTAB() throws IOException
{
BufferedWriter bw=new BufferedWriter(new FileWriter("LITTAB.txt"));
System.out.println("\nLiteral Table\n");
//Processing LITTAB
for(int i=0;i<LITTAB.size();i++)
{
TableRow row=LITTAB.get(i);
System.out.println(i+"\t"+row.getSymbol()+"\t"+row.getAddess());
bw.write((i+1)+"\t"+row.getSymbol()+"\t"+row.getAddess()+"\n");
}
bw.close();
}
void printPOOLTAB() throws IOException
{
BufferedWriter bw=new BufferedWriter(new FileWriter("POOLTAB.txt"));
System.out.println("\nPOOLTAB");
System.out.println("Index\t#first");
for (int i = 0; i < POOLTAB.size(); i++) {
System.out.println(i+"\t"+POOLTAB.get(i));
bw.write((i+1)+"\t"+POOLTAB.get(i)+"\n");
}
bw.close();
}
void printSYMTAB() throws IOException
{
BufferedWriter bw=new BufferedWriter(new FileWriter("SYMTAB.txt"));
//Printing Symbol Table
java.util.Iterator<String> iterator = SYMTAB.keySet().iterator();
System.out.println("SYMBOL TABLE");
while (iterator.hasNext()) {
String key = iterator.next().toString();
TableRow value = SYMTAB.get(key);
System.out.println(value.getIndex()+"\t" + value.getSymbol()+"\t"+value.getAddess());
bw.write(value.getIndex()+"\t" + value.getSymbol()+"\t"+value.getAddess()+"\n");
}
bw.close();
}
public int expr(String str)
{
int temp=0;
if(str.contains("+"))
{
String splits[]=str.split("\\+");
temp=SYMTAB.get(splits[0]).getAddess()+Integer.parseInt(splits[1]);
}
else if(str.contains("-"))
{
String splits[]=str.split("\\-");
temp=SYMTAB.get(splits[0]).getAddess()-(Integer.parseInt(splits[1]));
}
else
{
temp=Integer.parseInt(str);
}
return temp;
}
}