0% found this document useful (0 votes)
6 views8 pages

Assembler Java

The document contains a Java program named Assembler.java that implements an assembler for translating assembly code into machine code. It reads an assembly file, processes the code to ignore comments and blank lines, and translates the code into binary format using various functions. The program also handles variable definitions and predefined symbols, and includes error handling for file access issues.

Uploaded by

Dhruv Sud
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Assembler Java

The document contains a Java program named Assembler.java that implements an assembler for translating assembly code into machine code. It reads an assembly file, processes the code to ignore comments and blank lines, and translates the code into binary format using various functions. The program also handles variable definitions and predefined symbols, and includes error handling for file access issues.

Uploaded by

Dhruv Sud
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

28/04/2026, 13:14 Assembler.

java

[Link]

1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5
6 public class Assembler {
7 public static void main(String[] args) {
8 Scanner scanner = new Scanner([Link]);
9
10 // ASSEMBLER PROJECT
11 // The file name is [Link]
12 ArrayList<String> untranslatedCode = new ArrayList<String>();
13 ArrayList<String> translatedCode = new ArrayList<String>();
14 try(Scanner fileScanner = new Scanner([Link]("[Link]"))){
15 while([Link]()){
16 String untranslatedRow = [Link]();
17 [Link](untranslatedRow);
18 }
19 }
20 catch(Exception e){
21 [Link]("the file couldnt be accessed");
22 [Link]("Error is "+ e);
23 }
24 ArrayList<String> refinedCode = new ArrayList<String>();
25 // we will now call the refining function.
26 // to ignore the comments.
27 refinedCode = refiningFunction(untranslatedCode);
28 ArrayList<String> untranslatedCode_new­= refinedCode;
29 printingFunction(untranslatedCode_new­
);
30 //Now that we have a untranslated code stored in our data structure we
have to
31 // make a translation function
32 translatedCode = translationFunction(untranslatedCode_new­
);
33 printingFunction(translatedCode);
34
35
36
37 [Link]();
38 }
39
40 public static void printingFunction(ArrayList<String> toPrint){
41 for(String row : toPrint){
42 [Link](row);
43 }
44 }
45
46 public static ArrayList<String> translationFunction(ArrayList<String>
untranslatedCode){
47 ArrayList<String> translatedCode = new ArrayList<String>();
48 ArrayList<String> variables = new ArrayList<String>();
49 int i = 0;
50 int variableCounter = 16;
localhost:43363/ca14ccd3-f028-421e-bc7c-1204481f0eba/ 1/8
28/04/2026, 13:14 [Link]

51 variables = predefinedAddition();
52 while(i <= [Link]() -1){
53 char fistCharacter = [Link](i).charAt(0);
54 char secondCharacter = [Link](i).charAt(1);
55 if(fistCharacter == '('){ //Lets deal with loops first 2 if's
56 String labelName = insideBracketFunctio­
n([Link](i));
57 int location = i;
58 String variableValue = [Link](location);
59 String variableName = labelName + ";" + variableValue;
60 [Link](variableName);
61 }
62
63 if(fistCharacter == '@' && (secondCharacter == '0' || secondCharacter
== '1' || secondCharacter == '2' ||
64 secondCharacter == '3' || secondCharacter == '4' || secondCharacter ==
'5' || secondCharacter == '6' ||
65 secondCharacter == '7' || secondCharacter == '8' || secondCharacter ==
'9')){
66 //checking if it is a number or a variable at second place.
67 String translatedRow = binaryFunction([Link](i));
68 [Link](translatedRow);
69 }
70 else if(fistCharacter == '@' && (secondCharacter != '0' ||
secondCharacter != '1' ||
71 secondCharacter != '2' ||secondCharacter != '3' || secondCharacter !=
'4' ||
72 secondCharacter != '5' || secondCharacter != '6' || secondCharacter !=
'7' ||
73 secondCharacter != '8' || secondCharacter != '9')){
74 String[] temporary = [Link](i).split("@");
75 String translatedRow;
76 String variableName = temporary[1];
77
78 if(variableCheckFunctio­
n(variables, variableName) == false){
79 variableName = variableName + ";" +
[Link](variableCounter); //CSV
80 // CSV --> Comma Separated Values
81 [Link](variableName);
82 translatedRow = variableTranslationF­

unction([Link]([Link]()-1));
83 variableCounter++; // we start from sixteen
84 }
85
86 else{
87 translatedRow = alreadyExistingFunct­
ion(variables,
variableName);
88 }
89 [Link](translatedRow);
90 }
91 else{
92 Assembler assembler = new Assembler();
93 [Link] controls = [Link]
ControlBits([Link](i));
94 String translatedRow = [Link];
95 [Link](translatedRow);
localhost:43363/ca14ccd3-f028-421e-bc7c-1204481f0eba/ 2/8
28/04/2026, 13:14 [Link]

96 }
97 i++;
98 }
99 return translatedCode;
100 }
101 public static String insideBracketFunctio­
n(String label){
102 int i = 0;
103 char characterValue = [Link](i);
104 String name = "";
105 while(true){
106 i++;
107 characterValue = [Link](i);
108 if(characterValue == ')'){
109 break;
110 }
111 name = name + [Link](i);
112 }
113 return name;
114 }
115 public static boolean variableCheckFunctio­
n(ArrayList<String> variables,
String varibleName){
116 boolean checker = false;
117 for(int i = 0; i <= [Link]() -1; i++){
118 String temporary = [Link](i);
119 String[] temporary1 = [Link](";");
120 String temporary2 = temporary1[0];
121 if([Link](varibleName)){
122 checker = true;
123 }
124 }
125 return checker;
126 }
127 public static String alreadyExistingFunct­
ion(ArrayList<String> variables,
String variableName){
128 String translatedRow;
129 int value = 0;
130 for(int i = 0; i <= [Link]() -1; i++){
131 String[] temporary = [Link](i).split(";");
132 String temporary1 = temporary[0];
133 String value1 = temporary[1];
134 if([Link](variableName)){
135 value = [Link](value1);
136 break;
137 }
138 }
139 translatedRow = binaryFunction(value);
140 return translatedRow;
141 }
142
143 public static String variableTranslationF­
unction(String untranslatedRow){
144 String translatedRow;
145 String[] temporary = [Link](";");
146 translatedRow = temporary[1];

localhost:43363/ca14ccd3-f028-421e-bc7c-1204481f0eba/ 3/8
28/04/2026, 13:14 [Link]

147 String translatedBinaryRow =


binaryFunction([Link](translatedRow));
148 return translatedBinaryRow;
149 }
150
151 public static ArrayList<String> refiningFunction(ArrayList<String>
untranslatedCode){
152 ArrayList<String> refinedCode = new ArrayList<String>();
153 int i = 0;
154 while(i <= [Link]() -1){
155 if([Link](i).equals("") != true){ // now we can ignore
blankLines
156 if([Link](i).charAt(0) != '/'){
157 // we are using this function to ignore the comments in the
code
158 String row = [Link](i);
159 [Link](row);
160 }
161 }
162 i++;
163 }
164 return refinedCode;
165 }
166
167 public static String binaryFunction(String untranslatedRow){
168
169 String translatedRow = "";
170 String[] temporarySplitter = [Link]("@");
171 String valueStr = temporarySplitter[1];
172 int value = [Link](valueStr);
173 int quotient = (value / 2);
174 int remainder = (value % 2);
175 int counter = 1;
176 value = quotient;
177 //int placeCounter = 0; //first we are operating at the base of 10
178 //int placeValue = (int)[Link](10, placeCounter); //typeCasting double -
-> integer
179 //int binaryValue = remainder*placeValue;
180 translatedRow = [Link](remainder) + translatedRow;
181 while(quotient != 0){
182 quotient = value/2;
183 remainder = value%2;
184 value = quotient;
185 translatedRow = [Link](remainder) + translatedRow;
186 counter++;
187 }
188 String zeros = "";
189 for(int i = 1; i <= 16-(counter+1); i++){
190 zeros = zeros + "0";
191 }
192 translatedRow = "0" + zeros + translatedRow;
193 return translatedRow;
194 }
195

localhost:43363/ca14ccd3-f028-421e-bc7c-1204481f0eba/ 4/8
28/04/2026, 13:14 [Link]

196 public static String binaryFunction(int value){


197 String binaryTranslation = "";
198 int quotient = (value / 2);
199 int remainder = (value % 2);
200 int counter = 1;
201 value = quotient;
202 binaryTranslation = [Link](remainder) + binaryTranslation;
203 while(quotient != 0){
204 quotient = value/2;
205 remainder = value%2;
206 value = quotient;
207 binaryTranslation = [Link](remainder) + binaryTranslation;
208 counter++;
209 }
210 String zeros = "";
211 for(int i = 1; i <= 16-(counter+1); i++){
212 zeros = zeros + "0";
213 }
214 binaryTranslation = "0" + zeros + binaryTranslation;
215 return binaryTranslation;
216 }
217 public static ArrayList<String> predefinedAddition(){
218 ArrayList<String> keyWords = new ArrayList<String>();
219 String[] keywords = {"R0;0", "R1;1", "R2;2", "R3;3", "R4;4", "R5;5",
"R6;6", "R7;7",
220 "R8;8", "R9;9", "R10;10", "R11;11", "R12;12",
"R13;13", "R14;14",
221 "R15;15", "SCREEN;16384", "KBD;24576", "SP;0",
"LCL;1", "ARG;2",
222 "THIS;3", "THAT;4" };
223
224 for(int i = 0; i <= [Link] -1; i++){
225 [Link](keywords[i]);
226 }
227 return keyWords;
228 }
229
230 public class ControlBits{
231 private String[] opCode = {"0", "1"}; //a
232 private String[] computation = {"0;101010", "1;111111", "-1;111010",
"D;001100", "A;110000",
233 "!D;001101", "!A;110001", "-D;001111", "-
A;110011",
234 "D+1;011111", "A+1;110111", "D-1;001110",
"A-1;110010",
235 "D+A;000010", "D-A;010011", "A-D;000111",
"D&A;000000",
236 "D|A;010101" }; //cccccc
237 private String[] destination = {"NULL;000", "M;001", "D; 010", "DM;011",
"A;100", "AM;101",
238 "AD;110", "ADM;111"}; //ddd
239 private String[] jumper = {"NULL;000", "JGT;001", "JEQ;010",
"JGE;011", "JLT;100",
240 "JNE;101", "JLE;110", "JMP;111"}; //jjj
241 public String Command;
localhost:43363/ca14ccd3-f028-421e-bc7c-1204481f0eba/ 5/8
28/04/2026, 13:14 [Link]

242 public ControlBits(String Command){


243 [Link] = commandFunction(Command);
244 }
245 public String commandFunction(String Command){
246 String finalCommand = "";
247 boolean checkSemicolon = [Link](";");
248 boolean checkM = [Link]("M");
249 boolean checkEqual = [Link]("=");
250 String Destination;
251 String Jumper;
252 String Computation;
253 String opCode;
254 if(checkSemicolon && checkEqual){
255 String[] initial = [Link](";");
256 String jumper = initial[1];
257 Jumper = jumperFunction(jumper);
258 String other = initial[0];
259 String[] information = [Link]("=");
260 String destination = information[0];
261 Destination = destinationFunction(destination);
262 String computation = information[1];
263 Computation = computationFunction(computation, checkM);
264 opCode = "0"; //default
265 if(checkM){
266 opCode = "1";
267 }
268 finalCommand = "111" + opCode + Computation + Destination +
Jumper; // 111accccccdddjjj
269 }
270 else if(checkSemicolon == true && checkEqual == false){
271 String[] information = [Link](";");
272 String computation = information[0];
273 String jumper = information[1];
274 Destination = "000"; //NULL
275 Computation = computationFunction(computation, checkM);
276 Jumper = jumperFunction(jumper);
277 opCode = "0";
278 if(checkM){
279 opCode = "1";
280 }
281 finalCommand = "111" + opCode + Computation + Destination +
Jumper; // 111accccccdddjjj
282 }
283 else if(checkSemicolon == false && checkEqual == true){
284 String[] information = [Link]("=");
285 String destination = information[0];
286 String computation = information[1];
287 Jumper = "000"; //i.e. NULL
288 Destination = destinationFunction(destination);
289 Computation = computationFunction(computation, checkM);
290 opCode = "0";
291 if(checkM){
292 opCode = "1";
293 }

localhost:43363/ca14ccd3-f028-421e-bc7c-1204481f0eba/ 6/8
28/04/2026, 13:14 [Link]

294 finalCommand = "111" + opCode + Computation + Destination +


Jumper; // 111accccccdddjjj
295 }
296 return finalCommand;
297 }
298 public String jumperFunction(String jumper){
299 String binaryTranslation="";
300 for(int i = 0; i <= [Link] -1; i++){
301 String[] information = [Link][i].split(";");
302 String instruction = information[0];
303 String value = information[1];
304 if([Link](instruction)){
305 binaryTranslation = value;
306 break;
307 }
308 }
309 return binaryTranslation;
310 }
311 public String destinationFunction(String destination){
312 String binaryTranslation="";
313 for(int i = 0; i <= [Link] -1; i++){
314 String[] information = [Link][i].split(";");
315 String instruction = information[0];
316 String value = information[1];
317 if([Link](instruction)){
318 binaryTranslation = value;
319 break;
320 }
321 }
322 return binaryTranslation;
323 }
324 public String computationFunction(String computation, boolean checkM){
325 String computation_new;
326 String binaryTranslation = "";
327 if(checkM){
328 computation_new = replaceFunction(computation);
329 }
330 else{
331 computation_new = computation;
332 }
333 for(int i = 0; i <= [Link] -1; i++){
334 String[] information = [Link][i].split(";");
335 String instruction = information[0];
336 String value = information[1];
337 if([Link](computation_new)){
338 binaryTranslation = value;
339 break;
340 }
341 }
342 return binaryTranslation;
343 }
344 public String replaceFunction(String computation){
345 String replacement = "";
346 String[] storage = new String[2];
localhost:43363/ca14ccd3-f028-421e-bc7c-1204481f0eba/ 7/8
28/04/2026, 13:14 [Link]

347 int m = 0; // storage parts


348 for(int i = 0; i <= [Link]() -1; i++){
349 if([Link](i) == 'M'){
350 m++;
351 continue;
352 }
353 else{
354 storage[m] = storage[m] + [Link](i);
355 }
356 }
357 replacement = storage[0] + "A" + storage[1];
358 return replacement;
359 }
360 }
361 }
362
363

localhost:43363/ca14ccd3-f028-421e-bc7c-1204481f0eba/ 8/8

You might also like