29/04/2026, 18:18 Assembler.
java
[Link]
1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import [Link];
6 //.function([Link]("+"))
7
8 public class Assembler {
9 public static void main(String[] args) {
10 Scanner scanner = new Scanner([Link]);
11
12 // ASSEMBLER PROJECT
13 // The file name is [Link]
14 ArrayList<String> untranslatedCode = new ArrayList<String>();
15 ArrayList<String> translatedCode = new ArrayList<String>();
16 ArrayList<ArrayList<String>> translatedTable = new ArrayList<ArrayList<String>>();
17 try(Scanner fileScanner = new Scanner([Link]("[Link]"))){
18 while([Link]()){
19 String untranslatedRow = [Link]();
20 [Link](untranslatedRow);
21 }
22 }
23 catch(Exception e){
24 [Link]("the file couldnt be accessed");
25 [Link]("Error is "+ e);
26 }
27 ArrayList<String> refinedCode = new ArrayList<String>();
28 // we will now call the refining function.
29 // to ignore the comments.
30 refinedCode = refiningFunction(untranslatedCode);
31 ArrayList<String> untranslatedCode_new= refinedCode;
32 // printingFunction(untranslatedCode_new
);
33 // Now that we have a untranslated code stored in our data structure we have to
34 // make a translation function
35 // We have to handle the Label/Loop variables beforehand;
36 // find some way to transmit the data to the tranlationFunction
37 // Lets send a ArrayList<String>
38 // find some way to transmit the data to the tranlationFunction
39 // Lets send a ArrayList<String>
40 ArrayList<String> labelVariables = new ArrayList<String>();
41 labelVariables = labelGenerationFunction(untranslatedCode_new
);
42 translatedTable = translationFunction(untranslatedCode_new
, labelVariables);
43 translatedCode = [Link](0); // CODE --> 0
44 //We need to display our whole variable table AND we also need its value stored in main memory
here
45 ArrayList<String> variableTable = [Link](1);
46
47 printingFunction(translatedCode);
48 // displayFunction(variableTable);
49
50 /*
51 We have ran into the spacing issue
52 We have to make sure that there are no extra spaces before pressing enter
53 Lets write the code for that character != ' '
54 i.e. if(character == whitespace){break;}
55 i.e. I got a string and i gotta pass that String through a refining function
56 */
57
58 [Link]();
59 }
60 public static ArrayList<String> labelGenerationFunct
ion(ArrayList<String> untranslatedCode){
61 ArrayList<String> loopVariables = new ArrayList<String>();
62 int i = 0;
63 int counter = 0; //This is the label counter
64 // Used to keep track of the right lines at which label variables point to
65 while(i <= [Link]() -1){
66 char fistCharacter = [Link](i).charAt(0);
67 if(fistCharacter == '('){
68 String labelName = insideBracketFunctio
n([Link](i));
69 int location = i;
70 location = location - counter;
71 String variableValue = [Link](location);
72 String variableName = labelName + ";" + variableValue;
73 [Link](variableName);
74 counter++;
75 }
localhost:37865/b7dcf75a-276d-4abe-9283-fb12f77b22a2/ 1/7
29/04/2026, 18:18 [Link]
76 i++;
77 }
78 return loopVariables;
79 }
80 public static void printingFunction(ArrayList<String> toPrint){
81 for(String row : toPrint){
82 [Link](row);
83 }
84 }
85 public static void displayFunction(ArrayList<String> variable){
86 [Link]("____________________
____________________
____________________
‐
____________________
__");
87 [Link]("| VARIABLE NAME |" + " VALUE |" + " BINARY-VALUE
|");
88
[Link]("‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾");
89 String s1 = "VARIABLE NAME ";
90 int size1 = [Link]();
91 String s2 = "VALUE ";
92 int size2 = [Link]();
93 String s3 = "BINARY-VALUE ";
94 int size3 = [Link]();
95 for(int i = 0; i <= [Link]() -1; i++){
96 String[] variableData = [Link](i).split(";");
97 String variableName = variableData[0];
98 int length1 = size1 - [Link]();
99 String whiteSpaces1 = whiteSpaceFunction(length1);
100 // variableName = variableName + whiteSpaces1;
101 String variableValue = variableData[1];
102 int value = [Link](variableValue);
103 String binaryValue = binaryFunction(value);
104 [Link]("| "+ variableName + whiteSpaces1 + "| ");
105 int length2 = size2 - [Link]();
106 String whiteSpaces2 = whiteSpaceFunction(length2);
107 [Link](variableValue + whiteSpaces2 + "| ");
108 int length3 = size3 - [Link]();
109 String whiteSpaces3 = whiteSpaceFunction(length3);
110 [Link](binaryValue + whiteSpaces3 + "|\n");
111
[Link]("‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾");
112 }
113 }
114 public static String whiteSpaceFunction(int length1){
115 String whiteSpaces = "";
116 for(int i = 1; i <= length1; i++){
117 whiteSpaces = whiteSpaces + " ";
118 }
119 return whiteSpaces;
120 }
121 public static ArrayList<ArrayList<String>> translationFunction(ArrayList<String> untranslatedCode,
ArrayList<String> loopVariables){
122 ArrayList<String> translatedCode = new ArrayList<String>();
123 ArrayList<String> variables = new ArrayList<String>();
124 ArrayList<ArrayList<String>> translatedTable = new ArrayList<ArrayList<String>>();
125 int i = 0;
126 int variableCounter = 16;
127 variables = predefinedAddition();
128 for(int j = 0; j <= [Link]() -1; j++){
129 [Link]([Link](j));
130 }
131 while(i <= [Link]() -1){
132 char fistCharacter = [Link](i).charAt(0);
133 char secondCharacter = [Link](i).charAt(1);
134 if(fistCharacter == '('){ //Lets deal with loops first 2 if's
135 /*
136 String labelName = insideBracketFunctio
n([Link](i));
137 int location = i;
138 String variableValue = [Link](location);
139 String variableName = labelName + ";" + variableValue;
140 [Link](variableName);
141 */
142 i = i + 1;
143 continue;
144 }
145
146 if(fistCharacter == '@' && (secondCharacter == '0' || secondCharacter == '1' ||
secondCharacter == '2' ||
147 secondCharacter == '3' || secondCharacter == '4' || secondCharacter == '5' || secondCharacter
== '6' ||
localhost:37865/b7dcf75a-276d-4abe-9283-fb12f77b22a2/ 2/7
29/04/2026, 18:18 [Link]
148 secondCharacter == '7' || secondCharacter == '8' || secondCharacter == '9')){
149 //checking if it is a number or a variable at second place.
150 String translatedRow = binaryFunction([Link](i));
151 [Link](translatedRow);
152 }
153 else if(fistCharacter == '@' && (secondCharacter != '0' || secondCharacter != '1' ||
154 secondCharacter != '2' ||secondCharacter != '3' || secondCharacter != '4' ||
155 secondCharacter != '5' || secondCharacter != '6' || secondCharacter != '7' ||
156 secondCharacter != '8' || secondCharacter != '9')){
157 String[] temporary = [Link](i).split("@");
158 String translatedRow;
159 String variableName = temporary[1];
160
161 if(variableCheckFunctio
n(variables, variableName) == false){
162 variableName = variableName + ";" + [Link](variableCounter); //CSV
163 // CSV --> Comma Separated Values
164 [Link](variableName);
165 translatedRow = variableTranslationF
unction([Link]([Link]()-1));
166 variableCounter++; // we start from sixteen
167 }
168
169 else{
170 translatedRow = alreadyExistingFunct
ion(variables, variableName);
171 }
172 [Link](translatedRow);
173 }
174 else{
175 Assembler assembler = new Assembler();
176 [Link] controls = [Link] ControlBits([Link](i));
177 String translatedRow = [Link];
178 [Link](translatedRow);
179 }
180 i++;
181 }
182 [Link](translatedCode); // 0 --> CODE
183 [Link](variables); // 1 --> TABLE
184 return translatedTable;
185 }
186 public static String insideBracketFunctio
n(String label){
187 int i = 0;
188 char characterValue = [Link](i);
189 String name = "";
190 while(true){
191 i++;
192 characterValue = [Link](i);
193 if(characterValue == ')'){
194 break;
195 }
196 name = name + [Link](i);
197 }
198 return name;
199 }
200 public static boolean variableCheckFunctio
n(ArrayList<String> variables, String varibleName){
201 boolean checker = false;
202 for(int i = 0; i <= [Link]() -1; i++){
203 String temporary = [Link](i);
204 String[] temporary1 = [Link](";");
205 String temporary2 = temporary1[0];
206 if([Link](varibleName)){
207 checker = true;
208 }
209 }
210 return checker;
211 }
212 public static String alreadyExistingFunct
ion(ArrayList<String> variables, String variableName){
213 String translatedRow;
214 int value = 0;
215 for(int i = 0; i <= [Link]() -1; i++){
216 String[] temporary = [Link](i).split(";");
217 String temporary1 = temporary[0];
218 String value1 = temporary[1];
219 if([Link](variableName)){
220 value = [Link](value1);
221 break;
222 }
223 }
224 translatedRow = binaryFunction(value);
225 return translatedRow;
localhost:37865/b7dcf75a-276d-4abe-9283-fb12f77b22a2/ 3/7
29/04/2026, 18:18 [Link]
226 }
227
228 public static String variableTranslationF
unction(String untranslatedRow){
229 String translatedRow;
230 String[] temporary = [Link](";");
231 translatedRow = temporary[1];
232 String translatedBinaryRow = binaryFunction([Link](translatedRow));
233 return translatedBinaryRow;
234 }
235
236 public static ArrayList<String> refiningFunction(ArrayList<String> untranslatedCode){
237 ArrayList<String> refinedCode = new ArrayList<String>();
238 int i = 0;
239 while(i <= [Link]() -1){
240 if([Link](i).equals("") != true){ // now we can ignore blankLines
241 if([Link](i).charAt(0) != '/'){
242 // we are using this function to ignore the comments in the code
243 String row = RefineryFunction([Link](i));
244 /* if([Link]("A") || [Link]("D") || [Link]("M") ||
245 [Link]("@") || [Link]("(") || [Link]("=") ||
246 [Link]("!") || [Link]("&") || [Link]("|") ||
247 [Link]("1") || [Link]("0") || [Link]("-1")||
248 [Link]("J") || [Link]("N") || [Link]("-") ||
249 [Link]([Link]("+"))){ */
250 [Link](row);
251 // REVEALATION - Empty Lines are handled Automaticallly}
252 }
253 }
254 i++;
255 }
256 return refinedCode;
257 }
258 public static String RefineryFunction(String original){
259 String updated = "";//NULL is unpredictable
260 int i = 0;
261 while(i <= [Link]() -1){
262 // If the Code was using ; it would have been Easy.
263 // This also means no white-space use in the code allowed
264 // f.y.i. empty lines are also not allowed .contains(''|''|''|'')
265 // Revealation: Empty lines are handled automatically , idk man really idk it works!
266 // Alright Let me also allow empty lines
267 if([Link](i) == ' '){
268 break;
269 }
270 updated = updated + [Link](i);
271 i++;
272 }
273 return updated;
274 }
275 public static String binaryFunction(String untranslatedRow){
276
277 String translatedRow = "";
278 String[] temporarySplitter = [Link]("@");
279 String valueStr = temporarySplitter[1];
280 int value = [Link](valueStr);
281 int quotient = (value / 2);
282 int remainder = (value % 2);
283 int counter = 1;
284 value = quotient;
285 //int placeCounter = 0; //first we are operating at the base of 10
286 //int placeValue = (int)[Link](10, placeCounter); //typeCasting double --> integer
287 //int binaryValue = remainder*placeValue;
288 translatedRow = [Link](remainder) + translatedRow;
289 while(quotient != 0){
290 quotient = value/2;
291 remainder = value%2;
292 value = quotient;
293 translatedRow = [Link](remainder) + translatedRow;
294 counter++;
295 }
296 String zeros = "";
297 for(int i = 1; i <= 16-(counter+1); i++){
298 zeros = zeros + "0";
299 }
300 translatedRow = "0" + zeros + translatedRow;
301 return translatedRow;
302 }
303
localhost:37865/b7dcf75a-276d-4abe-9283-fb12f77b22a2/ 4/7
29/04/2026, 18:18 [Link]
304 public static String binaryFunction(int value){
305 String binaryTranslation = "";
306 int quotient = (value / 2);
307 int remainder = (value % 2);
308 int counter = 1;
309 value = quotient;
310 binaryTranslation = [Link](remainder) + binaryTranslation;
311 while(quotient != 0){
312 quotient = value/2;
313 remainder = value%2;
314 value = quotient;
315 binaryTranslation = [Link](remainder) + binaryTranslation;
316 counter++;
317 }
318 String zeros = "";
319 for(int i = 1; i <= 16-(counter+1); i++){
320 zeros = zeros + "0";
321 }
322 binaryTranslation = "0" + zeros + binaryTranslation;
323 return binaryTranslation;
324 }
325 public static ArrayList<String> predefinedAddition(){
326 ArrayList<String> keyWords = new ArrayList<String>();
327 String[] keywords = {"R0;0", "R1;1", "R2;2", "R3;3", "R4;4", "R5;5", "R6;6", "R7;7",
328 "R8;8", "R9;9", "R10;10", "R11;11", "R12;12", "R13;13", "R14;14",
329 "R15;15", "SCREEN;16384", "KBD;24576", "SP;0", "LCL;1", "ARG;2",
330 "THIS;3", "THAT;4" };
331
332 for(int i = 0; i <= [Link] -1; i++){
333 [Link](keywords[i]);
334 }
335 return keyWords;
336 }
337
338 public class ControlBits{
339 private String[] opCode = {"0", "1"}; //a
340 private String[] computation = {"0;101010", "1;111111", "-1;111010", "D;001100", "A;110000",
341 "!D;001101", "!A;110001", "-D;001111", "-A;110011",
342 "D+1;011111", "A+1;110111", "D-1;001110", "A-1;110010",
343 "D+A;000010", "D-A;010011", "A-D;000111", "D&A;000000",
344 "D|A;010101" }; //cccccc
345 private String[] destination = {"NULL;000", "M;001", "D;010", "DM;011", "A;100", "AM;101",
346 "AD;110", "ADM;111"}; //ddd
347 private String[] jumper = {"NULL;000", "JGT;001", "JEQ;010", "JGE;011", "JLT;100",
348 "JNE;101", "JLE;110", "JMP;111"}; //jjj
349 public String Command;
350 public ControlBits(String Command){
351 [Link] = commandFunction(Command);
352 }
353 public String commandFunction(String Command){
354 String finalCommand = "";
355 boolean checkSemicolon = [Link](";");
356 boolean checkM;
357 // boolean checkM = [Link]("M"); misleading{just here so I can learn from my
errors}
358 boolean checkEqual = [Link]("=");
359 String Destination;
360 String Jumper;
361 String Computation;
362 String opCode;
363 if(checkSemicolon && checkEqual){
364 String[] initial = [Link](";");
365 String jumper = initial[1];
366 Jumper = jumperFunction(jumper);
367 String other = initial[0];
368 String[] information = [Link]("=");
369 String destination = information[0];
370 Destination = destinationFunction(destination);
371 String computation = information[1];
372 checkM = [Link]("M");
373 Computation = computationFunction(computation, checkM);
374 opCode = "0"; //default
375 if(checkM){
376 opCode = "1";
377 }
378 finalCommand = "111" + opCode + Computation + Destination + Jumper; // 111accccccdddjjj
379 }
380 else if(checkSemicolon == true && checkEqual == false){
381 String[] information = [Link](";");
localhost:37865/b7dcf75a-276d-4abe-9283-fb12f77b22a2/ 5/7
29/04/2026, 18:18 [Link]
382 String computation = information[0];
383 String jumper = information[1];
384 Destination = "000"; //NULL
385 checkM = [Link]("M");
386 Computation = computationFunction(computation, checkM);
387 Jumper = jumperFunction(jumper);
388 opCode = "0";
389 if(checkM){
390 opCode = "1";
391 }
392 finalCommand = "111" + opCode + Computation + Destination + Jumper; // 111accccccdddjjj
393 }
394 else if(checkSemicolon == false && checkEqual == true){
395 String[] information = [Link]("=");
396 String destination = information[0];
397 String computation = information[1];
398 checkM = [Link]("M");
399 Jumper = "000"; //i.e. NULL
400 Destination = destinationFunction(destination);
401 Computation = computationFunction(computation, checkM);
402 opCode = "0";
403 if(checkM){
404 opCode = "1";
405 }
406 finalCommand = "111" + opCode + Computation + Destination + Jumper; // 111accccccdddjjj
407 }
408 return finalCommand;
409 }
410 public String jumperFunction(String jumper){
411 String binaryTranslation="";
412 for(int i = 0; i <= [Link] -1; i++){
413 String[] information = [Link][i].split(";");
414 String instruction = information[0];
415 String value = information[1];
416 if([Link](instruction)){
417 binaryTranslation = value;
418 break;
419 }
420 }
421 return binaryTranslation;
422 }
423 public String destinationFunction(String destination){
424 String binaryTranslation="";
425 for(int i = 0; i <= [Link] -1; i++){
426 String[] information = [Link][i].split(";");
427 String instruction = information[0];
428 String value = information[1];
429 if([Link](instruction)){
430 binaryTranslation = value;
431 break;
432 }
433 }
434 return binaryTranslation;
435 }
436 public String computationFunction(String computation, boolean checkM){
437 String computation_new;
438 String binaryTranslation = "";
439 if(checkM){
440 computation_new = replaceFunction(computation);
441 }
442 else{
443 computation_new = computation;
444 }
445 String inverseString = "";
446 if(computation_new.contains("+") || computation_new.contains("-") ||
447 computation_new.contains("&") || computation_new.contains("|")){
448 inverseString = inverseFunction(computation_new);
449 }
450 for(int i = 0; i <= [Link] -1; i++){
451 String[] information = [Link][i].split(";");
452 String instruction = information[0];
453 String value = information[1];
454 if([Link]("-")){
455 if([Link](computation_new)){
456 binaryTranslation = value;
457 break;
458 }
459 }
localhost:37865/b7dcf75a-276d-4abe-9283-fb12f77b22a2/ 6/7
29/04/2026, 18:18 [Link]
460 else{
461 if([Link](computation_new) || [Link](inverseString)){
462 binaryTranslation = value;
463 break;
464 }
465 }
466 }
467 return binaryTranslation;
468 }
469 public String inverseFunction(String computation_new){
470 String inverseString; // A+D --> D+A; +,|,&,A-D --> D-A
471 String middle = "";
472 if(computation_new.contains("+")){
473 middle = "+";
474 }
475 else if(computation_new.contains("-")){
476 middle = "-";
477 }
478 else if(computation_new.contains("&")){
479 middle = "&";
480 }
481 else if(computation_new.contains("|")){
482 middle = "|";
483 }
484 // middle = "\\" +middle; Regex
485 String[] twoSides = computation_new.split([Link](middle));
486 String side1 = twoSides[0];
487 String side2 = twoSides[1];
488 inverseString = side2 + middle + side1;
489 return inverseString;
490 }
491 public String replaceFunction(String computation){
492 String replacement = "";
493 int mIndicator = 77;
494 String[] storage = new String[2];
495 storage[0] = ""; // Be very very careful with NULL values
496 storage[1] = "";
497 int m = 0; // storage parts
498 for(int i = 0; i <= [Link]() -1; i++){
499 if([Link](i) == 'M'){
500 // storage[m] = storage[m] + [Link]([Link](i));
501 mIndicator = 777;
502 m++;
503 continue;
504 }
505 else{
506 storage[m] = storage[m] + [Link]([Link](i));
507 }
508 }
509 if(mIndicator == 777){
510 replacement = storage[0] + "A" + storage[1];
511 }
512 else{
513 replacement = computation;
514 }
515 return replacement;
516 }
517 }
518 }
519
520
localhost:37865/b7dcf75a-276d-4abe-9283-fb12f77b22a2/ 7/7