-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoPassAssembler.java
127 lines (108 loc) · 4.04 KB
/
TwoPassAssembler.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
// import java.util.HashMap;
// public class TwoPassAssembler {
// public static void main(String[] args) {
// String[] sourceCode = {
// "START 180",
// "READ M",
// "READ N",
// "LOOP MOVER AREG, M",
// " MOVER BREG, N",
// " COMP BREG, ='200'",
// " BC GT, LOOP",
// "BACK SUB AREG, M",
// " COMP AREG, ='500'",
// " BC LT, BACK",
// "STOP",
// "M DS 1",
// "N DS 1",
// " END"
// // "START 100",
// // "A",
// // "DC 10",
// // "MOVER AREG",
// // "B",
// // "MOVEM BREG, = '1'",
// // "ADD AREG, ='2'",
// // "SUB BREG, ='1'",
// // "B DC 20",
// // "LTORG",
// // "MOVER AREG, NUM",
// // "MOVER CREG, LOOP",
// // "ADD BREG, = '1'",
// // "NUM DS 5",
// // "LOOP DC 10",
// // "END"
// };
// HashMap<String, Integer> symbolTable = new HashMap<>();
// // First pass to generate symbol table
// int locationCounter = -1; // Starting address
// for (String line : sourceCode) {
// String[] tokens = line.split("\\s+"); // single whitwspace
// String label = tokens[0];
// if (label.equals("END")) break; // End of program
// if (label.equals("START")) {
// locationCounter = Integer.parseInt(tokens[1]) - 1; // Subtract 1 since we increment at the beginning of the loop
// }
// else if (label.equals("DS")) {
// int dsValue = Integer.parseInt(tokens[1]);
// locationCounter += dsValue;
// }
// else /* if (!label.equals("DS") && !label.equals("START")) */
// {
// locationCounter++;
// symbolTable.put(label, locationCounter);
// }
// }
// // Print symbol table
// System.out.println("Symbol Table:");
// System.out.println("Label\tAddress");
// for (String label : symbolTable.keySet()) {
// System.out.println(label + "\t" + symbolTable.get(label));
// }
// }
// }
import java.util.HashMap;
public class TwoPassAssembler{
public static void main(String[]args){
String[] sourceCode = {
"START 180",
" READ M",
" READ N",
"LOOP MOVER AREG, M",
" MOVER BREG, N",
" COMP BREG, ='200'",
" BC GT, LOOP",
"BACK SUB AREG, M",
" COMP AREG, ='500",
" BC LT, BACK",
" STOP",
"M DS 1",
"N DS 1",
" END"
};
HashMap<String,Integer> symboletbl = new HashMap<>();
int locationCounter = 0;
for(String line : sourceCode){
String [] tokens = line.split("\\s+");
String label = tokens[0];
if(label.equals("START")){
locationCounter = Integer.parseInt(tokens[1])-1;
}
else if(label.equals("END")) break;
else if(label.equals("DS")){
int dsValue = Integer.parseInt(tokens[1]);
locationCounter += dsValue;
}
else if(!label.equals("START") && !label.equals("DS")){
locationCounter++;
symboletbl.put(label,locationCounter);
}
}
System.out.println("Label\tAddress");
for(String symboleName: symboletbl.keySet()){
System.out.print(symboleName);
System.out.print("\t");
System.out.println(symboletbl.get(symboleName));
}
}
}