-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
290 lines (268 loc) · 11.3 KB
/
main.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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;
public class main {
public static Hashtable<String, Integer> symbolTable = new Hashtable<String, Integer>();
public static Hashtable<String, String> A0BitsTable = new Hashtable<String, String>();
public static Hashtable<String, String> A1BitsTable = new Hashtable<String, String>();
public static Hashtable<String, String> DBitsTable = new Hashtable<String, String>();
public static Hashtable<String, String> JBitsTable = new Hashtable<String, String>();
public static void main(String[] args) throws IOException {
// Creating a scanner to get file input (assembly instructions) from user
System.out.println(
"Type in file path. In visual studio you can just right click a .asm file in the test files folder and select copy path (shift alt c). This will copy it to your clipboard so you can just paste it below.");
Scanner input = new Scanner(System.in);
// Storing filename as a string
String fileName = input.next();
if (!fileName.contains(".asm")) {
input.close();
throw new IllegalArgumentException("Please submit a file with a .asm extension.");
}
// Creates file and the writer to write to the file
String hackFile = createNewFile(fileName);
FileWriter writer = new FileWriter(hackFile);
// Handles everything with opening the file
ArrayList<String> instructions = new ArrayList<String>();
instructions = fileHandlesOpening(fileName);
Hashtable<String, Integer> firstPassTable = firstPass(instructions);
Hashtable<String, Integer> holder = new Hashtable<String, Integer>();
String bits, a, d, cInstruct, dBit, cBit, jBit = "";
int eql, semi = -1;
int lineTracker = 0;
for (int i = 0; i < instructions.size(); i++) {
lineTracker++;
// Translate A instructions
String newInst = instructions.get(i).replaceAll("\\s+", "");
if (newInst.startsWith("@")) {
// sending the a instruction to be converted to binary and written to .hack file
// Cut off the @ symbol
newInst = newInst.substring(1);
// jump address
if (firstPassTable.containsKey("(" + newInst + ")")) {
int decimal = firstPassTable.get("(" + newInst + ")");
bits = Integer.toBinaryString(decimal);
while (bits.length() < 16) {
bits = "0" + bits;
}
}
// Check if a value
else if (newInst.matches("[0-9]+")) {
// Convert to an int for decimal->binary conversion
int decimal = Integer.parseInt(newInst);
// Convert back to a string and add on all the remaining 0's
bits = Integer.toBinaryString(decimal);
while (bits.length() < 16) {
bits = "0" + bits;
}
}
// else this is a user symbol
else {
if (symbolTable.containsKey(newInst)) {
int decimal = symbolTable.get(newInst);
bits = Integer.toBinaryString(decimal);
while (bits.length() < 16) {
bits = "0" + bits;
}
} else {
if (holder.containsKey(newInst)) {
int decimal = holder.get(newInst);
bits = Integer.toBinaryString(decimal);
while (bits.length() < 16) {
bits = "0" + bits;
}
} else {
int address = holder.size() + 16;
if (address >= 16384) {
throw new IllegalStateException("Out of Memory. Line #" + lineTracker);
}
holder.put(newInst, address);
bits = Integer.toBinaryString(address);
while (bits.length() < 16) {
bits = "0" + bits;
}
}
}
}
writer.write(bits);
writer.write(System.getProperty("line.separator"));
} else if (newInst.startsWith("(")) {
continue;
} else {
dBit = "";
cBit = "";
jBit = "";
cInstruct = "";
eql = newInst.indexOf("=");
semi = newInst.indexOf(";");
// d=c:j
if (eql != -1 && semi != -1) {
dBit = newInst.substring(0, eql);
cBit = newInst.substring(eql + 1, semi);
jBit = newInst.substring(semi + 1);
}
// c:j
else if (eql == -1 && semi != -1) {
cBit = newInst.substring(0, semi);
jBit = newInst.substring(semi + 1);
}
// d=c
else if (eql != -1 && semi == -1) {
dBit = newInst.substring(0, eql);
cBit = newInst.substring(eql + 1);
}
// d
else {
d = newInst;
}
if (DBitsTable.containsKey(dBit) && (A1BitsTable.containsKey(cBit) || A0BitsTable.containsKey(cBit))
&& JBitsTable.containsKey(jBit)) {
if (A0BitsTable.containsKey(cBit)) {
a = "0";
cBit = A0BitsTable.get(cBit);
} else {
a = "1";
cBit = A1BitsTable.get(cBit);
}
cInstruct = "111" + a + cBit + DBitsTable.get(dBit) + JBitsTable.get(jBit);
}
writer.write(cInstruct);
writer.write(System.getProperty("line.separator"));
}
}
// Closes the Scanner and Writer
writer.close();
input.close();
System.out.println("Operations complete... .hack was written successfully");
}
static {
// put all predefined symbols into a Hashtable
symbolTable.put("SP", 0);
symbolTable.put("LCL", 1);
symbolTable.put("ARG", 2);
symbolTable.put("THIS", 3);
symbolTable.put("THAT", 4);
symbolTable.put("R0", 0);
symbolTable.put("R1", 1);
symbolTable.put("R2", 2);
symbolTable.put("R3", 3);
symbolTable.put("R4", 4);
symbolTable.put("R5", 5);
symbolTable.put("R6", 6);
symbolTable.put("R7", 7);
symbolTable.put("R8", 8);
symbolTable.put("R9", 9);
symbolTable.put("R10", 10);
symbolTable.put("R11", 11);
symbolTable.put("R12", 12);
symbolTable.put("R13", 13);
symbolTable.put("R14", 14);
symbolTable.put("R15", 15);
symbolTable.put("SCREEN", 16384);
symbolTable.put("KBD", 24576);
// When a=0
A0BitsTable.put("0", "101010");
A0BitsTable.put("1", "111111");
A0BitsTable.put("-1", "111010");
A0BitsTable.put("D", "001100");
A0BitsTable.put("A", "110000");
A0BitsTable.put("!D", "001101");
A0BitsTable.put("!A", "110001");
A0BitsTable.put("-D", "001111");
A0BitsTable.put("-A", "110011");
A0BitsTable.put("D+1", "011111");
A0BitsTable.put("A+1", "110111");
A0BitsTable.put("D-1", "001110");
A0BitsTable.put("A-1", "110010");
A0BitsTable.put("D+A", "000010");
A0BitsTable.put("D-A", "010011");
A0BitsTable.put("A-D", "000111");
A0BitsTable.put("D&A", "000000");
A0BitsTable.put("D|A", "010101");
// When a=1
A1BitsTable.put("M", "110000");
A1BitsTable.put("!M", "110001");
A1BitsTable.put("-M", "110011");
A1BitsTable.put("M+1", "110111");
A1BitsTable.put("M-1", "110010");
A1BitsTable.put("D+M", "000010");
A1BitsTable.put("D-M", "010011");
A1BitsTable.put("M-D", "000111");
A1BitsTable.put("D&M", "000000");
A1BitsTable.put("D|M", "010101");
// put all dst posibilities into a Hashtable
DBitsTable.put("", "000");
DBitsTable.put("M", "001");
DBitsTable.put("D", "010");
DBitsTable.put("MD", "011");
DBitsTable.put("A", "100");
DBitsTable.put("AM", "101");
DBitsTable.put("AD", "110");
DBitsTable.put("AMD", "111");
// put all jmp posibilities into a Hashtable
JBitsTable.put("", "000");
JBitsTable.put("JGT", "001");
JBitsTable.put("JEQ", "010");
JBitsTable.put("JGE", "011");
JBitsTable.put("JLT", "100");
JBitsTable.put("JNE", "101");
JBitsTable.put("JLE", "110");
JBitsTable.put("JMP", "111");
}
public static Hashtable<String, Integer> firstPass(ArrayList<String> instructions) {
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
int counter = 0;
for (int i = 0; i < instructions.size(); i++) {
if (instructions.get(i).startsWith("(") && instructions.get(i).endsWith(")")) {
table.put(instructions.get(i), counter);
} else {
counter++;
}
}
return table;
}
public static String createNewFile(String fileName) {
fileName = fileName.replace(".asm", ".hack");
try {
File file = new File(fileName);
if (file.createNewFile())
System.out.println("File was created successfully!");
else
System.out.println("Error, file already exists. Instructions have been RE-WRITTEN in the file!");
} catch (IOException ioe) {
ioe.printStackTrace();
}
return fileName;
}
public static ArrayList<String> fileHandlesOpening(String fileName) {
// Open and sort through lines, ignoring comments and empty space
BufferedReader reader;
ArrayList<String> instructions = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while (line != null) {
if (!line.isEmpty()) {
if (line.startsWith("//") == false) {
if (line.contains("//")) {
int offset = line.indexOf("//");
line = line.substring(0, offset);
instructions.add(line);
} else {
instructions.add(line);
}
}
}
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return instructions;
}
}