-
Notifications
You must be signed in to change notification settings - Fork 0
/
softwareProject.java
1387 lines (1181 loc) · 49.9 KB
/
softwareProject.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.lang.*;
import java.io.*;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* This program acts as an assembler for ARM V8.
*
* @author Jordan Cartwright
* @author Jordan McKosky
* @version 12/12/16
*/
public class softwareProject
{
//instance variables
int errorCount = 0;
//name of the input file
String inputFileName = null;
//name of the exported file
String outFileName = null;
//name of the log file
String logFileName = null;
//ArrayList of instructions that need to be translated to Binary
protected ArrayList<String> inputCode = new ArrayList<String>();
//ArrayList holding the binary code to that gets exported to a txt file
protected ArrayList<String> binaryCode = new ArrayList<String>();
//ArrayList of Error Messages for the log file
protected ArrayList<String> errorMessages = new ArrayList<String>();
//The Current opcode of the instruction that the assembler is translating
protected String opcode = null;
//The Current format of hte instruction that the assembler is translating
protected String format = null;
//The whole instruction that the assembler is currently trying to translate
protected String currentInstruction = null;
//This is a line of binary that represents the instruction that is being added
//to the output text file.
protected String instructionToAddToText;
//current rd
protected String rd = null;
//curent rd
protected String rn = null;
//current rt
protected String rt = null;
//current rm
protected String rm = null;
//current shamt
protected String shamt = null;
//current immediate
protected String immediate = null;
//current address used in D format instruction
protected String address = null;
//current branch address used in B format instructions
protected String bAddress = null;
//current cond branch address used in CB format instructions.
protected String condBRAddr = null;
//This int value is solely used to determine the branch address
protected int currentLineNumber = 0;
//This String will save the current command being translated
protected String currentCommand = null;
//This keeps the current instruction's conditional branch type
//if necessary
protected String condBranchType = null;
//will help determine the desired branch address that compares
//the labels
protected String jumpToLabel = null;
//This boolean will help determine if line starts with a comment,
//meaning the whole line is a comment
protected boolean fullCommentLine = true;
//helps with error detection with branching
protected boolean badBranch = false;
//testing a loop to translate multiple files
boolean run = true;
//boolean variable that helps determine if a file was found/inputted correctly
boolean fileNotFound = false;
/**
* Constructor for objects of class newSoftware
* This is used to set defaults and manipulate variables from the Driver
*/
public softwareProject()
{
//default File name to ensure the file name was being changed
inputFileName = "Default";
outFileName = "Name Was Not Changed";
logFileName = "Log";
//weather or not the Driver should continue to run
run = true;
//dictates what the driver will do if the file is not found
//defaulted to false because we didnt check for a file yet
fileNotFound = false;
}
/**
* This method copies the File to the arrayList, inputCode.
*/
public void copyFile (File f) {
Scanner sc = null;
try {
sc = new Scanner(f);
//alerts the user that their file is being loaded for translation
System.out.println("\t" + "Loading File...");
int lineCount = 1;
while (sc.hasNext()) {
String copyLine = sc.nextLine();
if (copyLine.trim().length() > 0) {
//if line has text...
//add it to an ArrayList
inputCode.add(copyLine);
//output the imported file to the screen to update progress of the import
System.out.println("\t" +" "+ lineCount + "\t" + copyLine);
lineCount++;
}
}
sc.close();
} catch (FileNotFoundException e) {
//if the file is not found, trigger the fileNotFound variable
fileNotFound = true;
}
}
/**
* This method is used to write the binary translation into a text file.
*/
public void writeFile(){
try {
PrintWriter out = new PrintWriter(outFileName + ".txt","UTF-8");
int lineCount = 1;
//looks through ArrayList of all binary codes
for(int i = 0; i < binaryCode.size(); i++ ){
//command to write desired text to a file
//we output the array list of binary values
out.println(binaryCode.get(i));
//this just neatly displays out the translated binary code in the program
System.out.println("\t" +" "+ lineCount + "\t" + binaryCode.get(i));
lineCount++;
}
out.close();
}
catch (Exception e) {
System.out.print(e.getMessage());
}
}
/**
* This method is used to write the log file for error reports.
*/
public void writeLogFile(){
try {
PrintWriter out = new PrintWriter(logFileName +".txt","UTF-8");
//format of the file
out.println("Log Report (" + inputFileName +")"); //header
out.println("\t" + errorCount + " Errors Detected");
out.println(); //line break
if(errorCount > 0) {
//looks through ArrayList of all binary codes
for(int i = 0; i < errorMessages.size(); i = i+2 ){
//command to write desired text to a file
out.println(errorMessages.get(i));
//we output the array list of error codes
out.println("\t" + errorMessages.get(i+1));
out.println(); //line brake
}
}
else {
out.println("Sucessfully Translated " + binaryCode.size() + " lines of code");
}
out.close();
}
catch (Exception e) {
System.out.print(e.getMessage());
}
}
/**
* This method is the first step in translating the instructions. It reads in each line of the input file
* and then determines if the line is labeled, a comment, contians a comment, purely and instruction, basically disecting up each line from the file, and then
* determines its command (if one is present in that line), which then calls
* the checkCommand method to determine the correct opcode and leads to formating the instruction to binary.
*/
public void assembleFile (File f) {
Scanner sc = null;
//re-reading in the file for the second time and translating each instruction,
//line by line.
try {
sc = new Scanner(f);
//alerts the user that the file has begun the translation process
System.out.println("\t" + "Translating File...");
while (sc.hasNext()) {
String line = sc.nextLine();
//System.out.println(line);
if (line.trim().length() > 0) {
//if line has text...
//If the current instruction has a ":"
//This if statment disects up the line if a ":" is present anywhere...even if it is part of a comment
if(line.contains(":")){
String[] parts = line.split(": ");
//just a label---no code
if(parts.length==1){
String[] partsNew = line.split(":\t");
if(partsNew.length == 1){
//currentLineNumber++;
String[] partsNew2 = line.split(";");
if(partsNew2.length == 1){
continue;
}
else{
//If the ":" is part of a comment and the line has an actual instruction to undergo
currentInstruction = partsNew2[0];
String[] partsNew3 = line.split(" ");
String command = partsNew3[0];
currentCommand = command;
fullCommentLine = commentFirstChecker(); //THIS IS ADDED and for loop****
if(fullCommentLine == true){
currentLineNumber++;
continue;
}
checkCommand( command );
}
}
else{
String afterBreak = partsNew[1];
String[] parts2 = afterBreak.split(" ");
String command = parts2[0];
//command.substring(1);
currentCommand = command;
currentInstruction = afterBreak;
fullCommentLine = commentFirstChecker();
if(fullCommentLine == true){
currentLineNumber++;
continue;
}
checkCommand( command );
}
}
//Following else statment divides up the line if a label is present and not in a comment
else{
String afterBreak = parts[1];
String[] parts2 = afterBreak.split(" ");
String command = parts2[0];
//command.substring(1);
currentCommand = command;
currentInstruction = afterBreak;
fullCommentLine = commentFirstChecker();
if(fullCommentLine == true){
currentLineNumber++;
continue;
}
checkCommand( command );
}
}
//The Current instruction does not contain a label or ":"
else{
String command = null;
String[] parts = line.split(" ");
command = parts[0];
currentInstruction = line;
currentCommand = command;
//checks to make sure it is not a commented line
fullCommentLine = commentFirstChecker();
if(fullCommentLine == true){
currentLineNumber++;
continue;
}
checkCommand( command );
}
currentLineNumber++;
}
}
sc.close();
} catch (FileNotFoundException e) {
fileNotFound = true;
}
}
/**
* This method checks the command and determines the correct opcode and format of the
* instruction. It then calls the method, createInstruction to translate the instruction
* from Assembly Language to binary.
*/
public void checkCommand(String command){
//need to get rid of tabs/whitspace, etc.
//if they are first in currentCommand, this loop sets currentCommand and command to the new one without tabs present
//at the start of the line.
StringTokenizer st = new StringTokenizer(currentInstruction, " X,;/\t\t");
while (st.hasMoreTokens()) {
String currentToken = st.nextToken();
//System.out.println(currentToken);
if( currentToken.contains("\t") == false){
currentCommand = currentToken;
command = currentCommand;
break;
}
}
//List of commands this method checks for and gives the corresponding opcode, format, then calls
//the createInstruction method to fully translate the instruction to binary.
if (command.equals("ADD")){
opcode = "10001011000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals( "ADDI")){
opcode = "1001000100";
format = "I";
createInstruction( currentInstruction );
}
else if( command.equals("ADDIS")){
opcode = "1011000100";
format = "I";
createInstruction( currentInstruction );
}
else if( command.equals("ADDS")){
opcode = "10101011000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("AND")){
opcode = "10001010000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("ANDI")){
opcode = "1001001000";
format = "I";
createInstruction( currentInstruction );
}
else if( command.equals("ANDIS")){
opcode = "1111001000";
format = "I";
createInstruction( currentInstruction );
}
else if( command.equals("ANDS")){
opcode = "11101010000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("B")){
opcode = "000101";
format = "B";
createInstruction( currentInstruction );
}
//************ALL 16 conditionals****
else if( command.contains("B.")==true){
opcode = "01010100";
format = "CB";
createInstruction( currentInstruction );
}
else if( command.equals("BL")){
opcode = "100101";
format = "B";
createInstruction( currentInstruction );
}
else if( command.equals("BR")){
opcode = "11010110000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("CBNZ")){
opcode = "10110101";
format = "CB";
createInstruction( currentInstruction );
}
else if( command.equals("CBZ")){
opcode = "10110100";
format = "CB";
createInstruction( currentInstruction );
}
else if( command.equals("EOR")){
opcode = "11001010000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("EORI")){
opcode = "1101001000";
format = "I";
createInstruction( currentInstruction );
}
else if( command.equals("LDUR")){
opcode = "11111000010";
format = "D";
createInstruction( currentInstruction );
}
else if( command.equals("LSL")){
opcode = "11010011011";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("LSR")){
opcode = "11010011010";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("ASR")){
opcode = "10010011010";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("ORR")){
opcode = "10101010000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("ORRI")){
opcode = "1011001000";
format = "I";
createInstruction( currentInstruction );
}
else if( command.equals("STUR")){
opcode = "11111000000";
format = "D";
createInstruction( currentInstruction );
}
else if( command.equals("SUB")){
opcode = "11001011000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("SUBI")){
opcode = "1101000100";
format = "I";
createInstruction( currentInstruction );
}
else if( command.equals("SUBIS")){
opcode = "1111000100";
format = "I";
createInstruction( currentInstruction );
}
else if( command.equals("SUBS")){
opcode = "11101011000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("CMP")){
//this is actually the SUBS operator
//CMP X1,X2
//SUBS XZR,X1,X2
opcode = "11101011000";
format = "R";
createInstruction( currentInstruction );
//checked
}
else if( command.equals("CMPI")){
//this is actually the SUBIS operator
//CMPI X1, #3
//SUBI XZR, X1, #3
opcode = "1111000100";
format = "I";
createInstruction( currentInstruction );
//checked
}
else if( command.equals("MOV")){
//this is actually the ADD
//MOV X2, X1
//ADD X2, X1, XZR
opcode = "10001011000";
format = "R";
createInstruction( currentInstruction );
//checked
}
else if( command.equals("NOP")){
//do nothing
// ADD XZR, XZR, XZR work
opcode = "10001011000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("NEG")){
//NEG X2, X3
//Really means
//SUB X2, XZR, X3
opcode = "11001011000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("RET")){
//RET = BR LR
opcode = "11010110000";
format = "R";
createInstruction( currentInstruction );
}
//ADDITIONAL INSTRUCTIONS
else if( command.equals("MOVK")){
//MOVK Xd, #imm
//Two zeros are added at end of opcode to fill bit space
//Dan said these last two bits can be anything
//foramt IM = IW (per Dan)
opcode = "11110010100";
format = "IM";
createInstruction( currentInstruction );
}
else if( command.equals("MOVZ")){
//MOVZ Xd, #imm
//Two zeros are added at end of opcode to fill bit space
//Dan said these last two bits can be anything
//format IM = IW (per Dan)
format = "IM";
createInstruction( currentInstruction );
}
else if( command.equals("MUL")){
opcode = "10011011000";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("SDIV")){
opcode = "10011010110";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("SMULH")){
opcode = "10011011010";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("UDIV")){
opcode = "10011010110";
format = "R";
createInstruction( currentInstruction );
}
else if( command.equals("UMULH")){
opcode = "10011011110";
format = "R";
createInstruction( currentInstruction );
}
//ERROR checker for an illegal/mispelled/invalid command
else{
instructionToAddToText = "ERROR: Invalid Instruction";
binaryCode.add(instructionToAddToText);
errorMessages.add("Error: Line " + (currentLineNumber + 1));
errorMessages.add("'" + currentCommand + "'" + " is an invalid ARMv8 Instruction");
errorCount++;
}
}
/**
* This method translates the Assembly Instruction to Binary. It does so by checking the instructions format, then
* dividing up the instruction to its correct order, in respects to its binary representation, and translates it to binary.
*/
public void createInstruction(String currentInstruction){
//R Format
if( format.equals("R")){
//Error checker
if(currentInstruction.contains("#") && currentInstruction.contains("LSL") == false && currentInstruction.contains("LSR") == false
&& currentInstruction.contains("ASR") == false){
//if the line of code does contain a #
//this is not an R format instruction
instructionToAddToText = "ERROR: Invalid 'R' Format - Immediate Detected";
errorMessages.add("Error: Line " + (currentLineNumber + 1));
errorMessages.add("'" +currentCommand+ "'" + " is a R Format instruction that only takes register inputs, Immediate values are invalid");
errorCount++;
}
else if(currentInstruction.contains(",") == true && currentInstruction.contains("#") == false){
StringTokenizer st = new StringTokenizer(currentInstruction, " X,;/\t");
int i = 0;
while (st.hasMoreTokens()) {
String currentToken = st.nextToken();
//System.out.println(currentToken);
if( i == 1) rd = currentToken;
if( i == 2) rn = currentToken;
if( i == 3){
rm = currentToken;
break;
}
i++;
}
//Checks for some special R format commands
if(currentCommand.equals("CMP")){
rm = rn;
rn = rd;
rd = "ZR";
}
else if( currentCommand.equals("MOV")){
rm = "31";
}
else if( currentCommand.equals("NEG")){
rm = rn;
rn = "ZR";
}
//Translates registers SP, LR,ZR and FP to their decimal values
checkForSpecReg();
//converting decimal to binary with correct
//number of leading zeros.
int tempInt = 0;
tempInt = Integer.parseInt(rd);
rd = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rd);
rd = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rn);
rn = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rn);
rn = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rm);
rm = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rm);
rm = String.format("%05d", tempInt);
if(currentCommand.equals("MUL")){
shamt = "011111";
}
else if(currentCommand.equals("UDIV")){
shamt = "000011";
}
else if(currentCommand.equals("SDIV")){
shamt = "000010";
}
else{
shamt = "000000";
}
//instruction ready to add to txt file
instructionToAddToText = opcode + rm + shamt + rn + rd;
}
//LSL and LSR and ASR commands
else if(currentInstruction.contains("#")==true){
StringTokenizer st = new StringTokenizer(currentInstruction, " X,;/\t#");
int i = 0;
while (st.hasMoreTokens()) {
String currentToken = st.nextToken();
//System.out.println(currentToken);
if( i == 1) rd = currentToken;
if( i == 2) rn = currentToken;
if( i == 3){
immediate = currentToken;
break;
}
i++;
}
checkForSpecReg();
//converting decimal to binary with correct
//number of leading zeros.
int tempInt = 0;
tempInt = Integer.parseInt(rd);
rd = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rd);
rd = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rn);
rn = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rn);
rn = String.format("%05d", tempInt);
tempInt = Integer.parseInt(immediate);
immediate = Integer.toBinaryString(tempInt);
//Error testing for negative immediate
try{
tempInt = Integer.parseInt(immediate);
immediate = String.format("%06d", tempInt);
shamt = immediate;
rm = "00000";
//instruction ready to add to txt file
instructionToAddToText = opcode + rm + shamt + rn + rd;
}
catch(NumberFormatException e){
instructionToAddToText = "ERROR: Negative immediate detected";
errorMessages.add("Error: Line " + (currentLineNumber + 1));
errorMessages.add("Negative valued immediates are not legal for " +"'"+ currentCommand +"'"+ " instructions");
errorCount++;
}
}
//Checks for specific R Formats
if( currentCommand.equals("NOP")){
rm = "ZR";
rd = "ZR";
rn = "ZR";
checkForSpecReg();
int tempInt = 0;
tempInt = Integer.parseInt(rd);
rd = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rd);
rd = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rn);
rn = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rn);
rn = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rm);
rm = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rm);
rm = String.format("%05d", tempInt);
shamt = "000000";
//instruction ready to add to txt file
instructionToAddToText = opcode + rm + shamt + rn + rd;
}
//If command is RET...translate to BR LR
else if((currentCommand.equals("RET"))|| currentCommand.equals("BR")){
if(currentCommand.equals("BR")){
//Rt from instruction is Rn accoring to the format on the
//green reference sheet.
StringTokenizer st = new StringTokenizer(currentInstruction, " X,;/\t");
int i = 0;
while (st.hasMoreTokens()) {
String currentToken = st.nextToken();
//System.out.println(currentToken);
if( i == 1) {
rn = currentToken;
break;
}
i++;
}
rm = "00000";
rd = "00000";
}
else if(currentCommand.equals("RET")){
//RET = BR LR
rn = "LR";
rm = "00000";
rd = "00000";
}
checkForSpecReg();
int tempInt = 0;
tempInt = Integer.parseInt(rd);
rd = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rd);
rd = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rn);
rn = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rn);
rn = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rm);
rm = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rm);
rm = String.format("%05d", tempInt);
shamt = "000000";
//instruction ready to add to txt file
instructionToAddToText = opcode + rm + shamt + rn + rd;
}
//adds Binary to output txt file
binaryCode.add(instructionToAddToText);
}
//I Format
else if( format.equals("I")){
if(currentInstruction.contains("#") == false){
//if the line of code does not contain a #
//this is not an I format instruction
instructionToAddToText = "ERROR: Invalid 'I' Format - not an immediate value";
errorMessages.add("Error: Line " + (currentLineNumber + 1));
errorMessages.add("'" +currentCommand+ "'" + " is an I Format instruction that requires an immediate with '#' to compile, registers are not valid");
errorCount++;
}
else {
//Breaks up and disects the line
StringTokenizer st = new StringTokenizer(currentInstruction, " X,;/\t#");
int i = 0;
while (st.hasMoreTokens()) {
String currentToken = st.nextToken();
//System.out.println(currentToken);
if( i == 1) rd = currentToken;
if( i == 2) rn = currentToken;
if( i == 3) {
immediate = currentToken;
break;
}
i++;
}
//if command is CMPI
if( currentCommand.equals("CMPI")){
immediate = rn;
rn = rd;
rd = "ZR";
}
checkForSpecReg();
//converting decimal to binary with correct
//number of leading zeros.
int tempInt = 0;
tempInt = Integer.parseInt(rd);
rd = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rd);
rd = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rn);
rn = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rn);
rn = String.format("%05d", tempInt);
//Errors out if the value is to large to fit in 12 bits signed
//aslo checks if it is negative
try{
//only do this if the immediate is not negative
if(immediate.contains("-") == false) {
tempInt = Integer.parseInt(immediate);
immediate = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(immediate);
immediate = String.format("%012d", tempInt);
instructionToAddToText = opcode + immediate + rn + rd;
}
else {
instructionToAddToText = "ERROR: Negative immediate value detected";
errorMessages.add("Error: Line " + (currentLineNumber + 1));
errorMessages.add("Negative Immediate value not allowed with " + currentCommand + " command");
errorCount++;
}
}
catch(NumberFormatException e){
instructionToAddToText = "ERROR: Immediate value is to large";
errorMessages.add("Error: Line " + (currentLineNumber + 1));
errorMessages.add("Immediate " + "'" + tempInt + "'" +" exceeds 12-bit signed MAX_VALUE");
errorCount++;
}
}
//adds Binary to output txt file
binaryCode.add(instructionToAddToText);
}
//D Format
else if (format.equals("D")){
if(currentInstruction.contains("[") == false || currentInstruction.contains("]") == false){
instructionToAddToText = "ERROR: Invalid 'D' Format - Missing or incorrect Brackets";
errorMessages.add("Error: Line " + (currentLineNumber + 1));
errorMessages.add("Brackets are required for D format Instructions");
errorCount++;
}
else if(currentInstruction.contains("#") == false){
instructionToAddToText = "ERROR: Invalid 'D' Format - no immediate value detected";
errorMessages.add("Error: Line " + (currentLineNumber + 1));
errorMessages.add("D Format instructions require an immediate value");
errorCount++;
}
else {
//Disects instruction line and finds necessary parts
StringTokenizer st = new StringTokenizer(currentInstruction, " X,;/\t#[]");
int i = 0;
while (st.hasMoreTokens()) {
String currentToken = st.nextToken();
//System.out.println(currentToken);
if( i == 1) rt = currentToken;
if( i == 2) rn = currentToken;
if( i == 3){
address = currentToken;
break;
}
i++;
}
checkForSpecReg();
//converting decimal to binary with correct
//number of leading zeros.
int tempInt = 0;
tempInt = Integer.parseInt(rt);
rt = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rt);
rt = String.format("%05d", tempInt);
tempInt = Integer.parseInt(rn);
rn = Integer.toBinaryString(tempInt);
tempInt = Integer.parseInt(rn);
rn = String.format("%05d", tempInt);
tempInt = Integer.parseInt(address);
address = Integer.toBinaryString(tempInt);
if(tempInt < 0){
address = address.substring(address.length()-9);
}
else{
tempInt = Integer.parseInt(address);
address = String.format("%09d", tempInt);
}
String tinyOpcode = "00";
//instruction ready to add to txt file
instructionToAddToText = opcode + address + tinyOpcode + rn + rt;
}
//adds Binary to output
binaryCode.add(instructionToAddToText);
}
//B Format
else if (format.equals("B")){
StringTokenizer st = new StringTokenizer(currentInstruction, " X,;/\t#[]");
int i = 0;
while (st.hasMoreTokens()) {
String currentToken = st.nextToken();