-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assembler.java
517 lines (495 loc) · 14.5 KB
/
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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package project;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Assembler {
public static String assemble(File input, File output) throws FileNotFoundException {
String returnValue = "success";
Scanner sc = new Scanner(input);
ArrayList<String> inText = new ArrayList<>();
ArrayList<String> errors = new ArrayList<>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
inText.add(line);
}
sc.close();
// copied all the lines in the file into inText array
// checking for the first three errors
int lineNum1 = 0, lineNum2 = 0, lineNum3 = 0;
// first error if there is a blank line
int blankLine = 0;
boolean error1 = false;
boolean blank = false;
for (int i = 0; i < inText.size(); i++) {
lineNum1 = i;
String line = inText.get(i);
if (line.trim().length() == 0) {
// there is a blank line
blank = true;
blankLine = lineNum1;
}
if (line.trim().length() > 0 && blank == true) {
returnValue = "Error: line " + (blankLine + 1) + " is a blank line";
errors.add(returnValue);
error1 = true;
lineNum1 = blankLine;
break;
}
}
boolean error2 = false;
if (error1) {
// the first error occurred, try to find errors before that
int errorLine = lineNum1;
for (int i = 0; i < errorLine; i++) {
lineNum2 = i;
String line = inText.get(i);
if (line.charAt(0) == ' ' || line.charAt(0) == '\t') {
returnValue = "Error: line " + (lineNum2 + 1) + " starts with white space";
errors.add(returnValue);
error2 = true;
break;
}
}
} else {
// error1 didn't occur
for (int i = 0; i < inText.size(); i++) {
lineNum2 = i;
String line = inText.get(i);
if (!blank) {
if (line.charAt(0) == ' ' || line.charAt(0) == '\t') {
returnValue = "Error: line " + (lineNum2 + 1) + " starts with white space";
errors.add(returnValue);
error2 = true;
break;
}
}
}
}
// checking for error3
boolean error3 = false;
int errorLine = 0;
boolean error1o2 = false;
if (error2) {
error1o2 = true;
} else if (error1) {
error1o2 = true;
}
if (error1o2) {
if (error2) {
errorLine = lineNum2;
} else if (error1) {
errorLine = lineNum1;
}
for (int i = 0; i < errorLine; i++) {
lineNum3 = i;
String line = inText.get(i);
if (line.trim().toUpperCase().equals("DATA")) {
if (!line.trim().equals("DATA")) {
returnValue = "Error: line " + (lineNum3 + 1) + " does not have DATA in upper case";
errors.add(returnValue);
error3 = true;
break;
}
}
}
} else {
for (int i = 0; i < inText.size(); i++) {
lineNum3 = i;
String line = inText.get(i);
if (line.trim().toUpperCase().equals("DATA")) {
if (!line.trim().equals("DATA")) {
returnValue = "Error: line " + (lineNum3 + 1) + " does not have DATA in upper case";
errors.add(returnValue);
error3 = true;
break;
}
}
}
}
// check if there are no error to inText
boolean inError = false;
if (error1) {
inError = true;
} else if (error2) {
inError = true;
} else if (error3) {
inError = true;
}
ArrayList<String> code = new ArrayList<>();
ArrayList<String> data = new ArrayList<>();
if (!inError) {
// there are no errors so far...
int dataLine = 0;
// populating code array
for (int i = 0; i < inText.size(); i++) {
dataLine = i;
String line = inText.get(i);
if (line.trim().equals("DATA")) {
break;
} else {
if (line.trim().length() > 0) {
code.add(line);
}
}
}
// populating data array
for (int i = dataLine + 1; i < inText.size(); i++) {
String line = inText.get(i);
if (line.trim().length() > 0) {
data.add(line);
}
}
ArrayList<String> outText = new ArrayList<>();
// checking for errors in code
boolean codeError = false;
int codeNum = 0;
for (int i = 0; i < code.size(); i++) {
codeNum = i;
String line = code.get(i);
String[] parts = line.trim().split("\\s+");
if (InstructionMap.sourceCodes.contains(parts[0].toUpperCase())) {
if (!(InstructionMap.sourceCodes.contains(parts[0]))) {
returnValue = "Error: line " + (codeNum + 1)
+ " does not have the instruction mnemonic in upper case";
errors.add(returnValue);
codeError = true;
break;
}
}
// if there is no error ...
if (!codeError) {
if (!InstructionMap.sourceCodes.contains(parts[0].toUpperCase())) {
returnValue = "Error: line " + (codeNum + 1) + " has a bad mnemonic instruction";
errors.add(returnValue);
codeError = true;
break;
}
if (InstructionMap.noArgument.contains(parts[0])) {
if (parts.length != 1) {
returnValue = "Error: line " + (codeNum + 1) + " has an illegal argument";
errors.add(returnValue);
codeError = true;
break;
}
} else {
if (parts.length == 1) {
if (line.trim().length() != 0) {
returnValue = "Error: line " + (codeNum + 1) + " is missing an argument";
errors.add(returnValue);
codeError = true;
break;
}
}
if (parts.length >= 3) {
returnValue = "Error: line " + (codeNum + 1) + " has more than one argument";
errors.add(returnValue);
codeError = true;
break;
}
if (parts.length == 2) {
if (parts[1].startsWith("#")) {
if (!(InstructionMap.immediateOK.contains(parts[0]))) {
returnValue = "Error: line " + (codeNum + 1) + " argument is not immediate.";
errors.add(returnValue);
codeError = true;
break;
} else {
parts[0] = parts[0] + "I";
parts[1] = parts[1].substring(1);
int arg = 0;
try {
arg = Integer.parseInt(parts[1], 16);
} catch (NumberFormatException e) {
returnValue = "Error: line " + (codeNum + 1)
+ " does not have a numeric argument";
errors.add(returnValue);
codeError = true;
break;
}
}
} else if (parts[1].startsWith("&")) {
if (!(InstructionMap.indirectOK.contains(parts[0]))) {
returnValue = "Error: line " + (codeNum + 1) + " argument is not indirect.";
errors.add(returnValue);
codeError = true;
break;
} else {
parts[0] = parts[0] + "N";
parts[1] = parts[1].substring(1);
int arg = 0;
try {
arg = Integer.parseInt(parts[1], 16);
} catch (NumberFormatException e) {
returnValue = "Error: line " + (codeNum + 1)
+ " does not have a numeric argument";
errors.add(returnValue);
codeError = true;
break;
}
}
}
}
}
}
}
boolean dataError = false;
if (!codeError) {
int dataNum = 0;
for (int i = 0; i < data.size(); i++) {
dataNum = i + dataLine;
String line = data.get(i);
String[] parts = line.trim().split("\\s+");
if (parts.length != 2) {
if (parts.length == 1) {
if (!line.trim().equals("DATA")) {
returnValue = "Error: line " + (dataNum + 1) + " is missing an argument";
errors.add(returnValue);
dataError = true;
break;
}
} else {
returnValue = "Error: line " + (dataNum + 1) + " has more than one argument";
errors.add(returnValue);
dataError = true;
break;
}
} else {
if (!line.trim().equals("DATA")) {
int arg = 0;
try {
arg = Integer.parseInt(parts[0], 16);
arg = Integer.parseInt(parts[1], 16);
} catch (NumberFormatException e) {
returnValue = "Error: line " + (dataNum + 1) + " does not have a numeric argument";
errors.add(returnValue);
dataError = true;
break;
}
}
}
}
}
// check if there is no error in code
boolean perfect = true;
if (codeError) {
perfect = false;
} else if (dataError) {
perfect = false;
} else if (error1) {
perfect = false;
} else if (error2) {
perfect = false;
} else if (error3) {
perfect = false;
}
if (perfect) {
for (String s : code) {
String[] parts = s.split("\\s+");
if (parts.length == 1) {
int opcode = InstructionMap.opcode.get(parts[0]);
outText.add(Integer.toHexString(opcode).toUpperCase() + " 0");
} else {
if (parts[1].startsWith("#")) {
parts[0] = parts[0] + "I";
parts[1] = parts[1].substring(1);
if (parts[0].equals("JUMPI"))
parts[0] = "JMPI";
if (parts[0].equals("JMPZI"))
parts[0] = "JMZI";
} else if (parts[1].startsWith("&")) {
parts[0] = parts[0] + "N";
parts[1] = parts[1].substring(1);
if (parts[0].equals("JUMPN"))
parts[0] = "JMPN";
}
int opcode = InstructionMap.opcode.get(parts[0]);
outText.add(Integer.toHexString(opcode).toUpperCase() + " " + parts[1]);
}
}
outText.add("-1");
outText.addAll(data);
if (returnValue.equals("success")) {
try (PrintWriter outp = new PrintWriter(output)) {
for (String str : outText) {
outp.println(str);
}
outp.close();
} catch (FileNotFoundException e) {
returnValue = "Error: unable to open " + output;
}
}
}
// "you can process them as in Assignment 9"
} else {
// there is an error, so get line of where error occurred
// which line occurred first
if (lineNum1 < lineNum2 && lineNum1 < lineNum3) {
errorLine = lineNum1;
} else if (lineNum2 < lineNum1 && lineNum2 < lineNum3) {
errorLine = lineNum2;
} else if (lineNum3 < lineNum1 && lineNum3 < lineNum2) {
errorLine = lineNum3;
}
// copy inText to code up to errorLine
int dataLine = 0;
boolean made2Data = false;
for (int i = 0; i < errorLine; i++) {
dataLine = i;
String line = inText.get(i);
if (line.trim().equals("DATA")) {
made2Data = true;
break;
} else {
code.add(line);
}
}
if(made2Data){
for (int i = dataLine; i < errorLine; i++) {
String line = inText.get(i);
if (line.trim().length() != 0) {
data.add(line);
}
}
}
boolean codeError = false;
int codeNum = 0;
for (int i = 0; i < code.size(); i++) {
codeNum = i;
String line = code.get(i);
String[] parts = line.trim().split("\\s+");
if (InstructionMap.sourceCodes.contains(parts[0].toUpperCase())) {
if (!(InstructionMap.sourceCodes.contains(parts[0]))) {
returnValue = "Error: line " + (codeNum + 1)
+ " does not have the instruction mnemonic in upper case";
errors.add(returnValue);
codeError = true;
break;
}
}
// if there is no error ...
if (!codeError) {
if (!InstructionMap.sourceCodes.contains(parts[0].toUpperCase())) {
returnValue = "Error: line " + (codeNum + 1) + " has a bad mnemonic instruction";
errors.add(returnValue);
codeError = true;
break;
}
if (InstructionMap.noArgument.contains(parts[0])) {
if (parts.length != 1) {
returnValue = "Error: line " + (codeNum + 1) + " has an illegal argument";
errors.add(returnValue);
codeError = true;
break;
}
} else {
if (parts.length == 1) {
if (!(line.trim().length() == 0)) {
returnValue = "Error: line " + (codeNum + 1) + " is missing an argument";
errors.add(returnValue);
codeError = true;
break;
}
}
if (parts.length >= 3) {
returnValue = "Error: line " + (codeNum + 1) + " has more than one argument";
errors.add(returnValue);
codeError = true;
break;
}
if (parts.length == 2) {
if (parts[1].startsWith("#")) {
if (!(InstructionMap.immediateOK.contains(parts[0]))) {
returnValue = "Error: line " + (codeNum + 1) + " argument is not immediate.";
errors.add(returnValue);
codeError = true;
break;
} else {
parts[0] = parts[0] + "I";
parts[1] = parts[1].substring(1);
int arg = 0;
try {
arg = Integer.parseInt(parts[1], 16);
} catch (NumberFormatException e) {
returnValue = "Error: line " + (codeNum + 1)
+ " does not have a numeric argument";
errors.add(returnValue);
codeError = true;
break;
}
}
} else if (parts[1].startsWith("&")) {
if (!(InstructionMap.indirectOK.contains(parts[0]))) {
returnValue = "Error: line " + (codeNum + 1) + " argument is not indirect.";
errors.add(returnValue);
codeError = true;
} else {
parts[0] = parts[0] + "N";
parts[1] = parts[1].substring(1);
int arg = 0;
try {
arg = Integer.parseInt(parts[1], 16);
} catch (NumberFormatException e) {
returnValue = "Error: line " + (codeNum + 1)
+ " does not have a numeric argument";
errors.add(returnValue);
codeError = true;
break;
}
}
}
}
}
}
}
boolean dataError = false;
int dataNum = 0;
for (int i = 0; i < data.size(); i++) {
dataNum = i + dataLine;
String line = data.get(i);
String[] parts = line.trim().split("\\s+");
if (parts.length != 2) {
if (parts.length == 1) {
if (!line.trim().equals("DATA")) {
returnValue = "Error: line " + (dataNum + 1) + " is missing an argument";
errors.add(returnValue);
dataError = true;
break;
}
} else {
returnValue = "Error: line " + (dataNum + 1) + " has more than one argument";
errors.add(returnValue);
dataError = true;
break;
}
} else {
int arg = 0;
try {
arg = Integer.parseInt(parts[0], 16);
arg = Integer.parseInt(parts[1], 16);
} catch (NumberFormatException e) {
returnValue = "Error: line " + (dataNum + 1) + " does not have a numeric argument";
errors.add(returnValue);
dataError = true;
break;
}
}
}
}
return returnValue;
}
// public static void main(String[] args) throws FileNotFoundException {
// System.out.println("Actual " + assemble(new File("05e.pasm"), new File("101rt.pexe")));
// }
public static void main(String[] args) throws FileNotFoundException {
for (int i = 3; i <= 26; i++) {
String s = "";
if (i < 10) {
s += "0";
}
s += i + "e";
System.out.println(s + " Actual " + assemble(new File(s + ".pasm"), new File(s + ".pexe")));
}
}
}