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

Assembler Java

The document contains a Java program for an assembler that reads assembly code from a file, processes it by refining and translating it, and outputs the translated code. It includes functions for handling labels, variables, and binary conversions, as well as ignoring comments and blank lines in the input code. The program also initializes predefined variables and manages variable storage and translation for the assembly language instructions.

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 views4 pages

Assembler Java

The document contains a Java program for an assembler that reads assembly code from a file, processes it by refining and translating it, and outputs the translated code. It includes functions for handling labels, variables, and binary conversions, as well as ignoring comments and blank lines in the input code. The program also initializes predefined variables and manages variable storage and translation for the assembly language instructions.

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

File: /home/dhruvsud/Documents/JAVA/Assembler/Assembler.

java Page 1 of 4

import [Link];
import [Link];
import [Link];
import [Link];

public class Assembler {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// ASSEMBLER PROJECT
// The file name is [Link]
ArrayList<String> untranslatedCode = new ArrayList<String>();
ArrayList<String> translatedCode = new ArrayList<String>();
try(Scanner fileScanner = new Scanner([Link]("[Link]"))){
while([Link]()){
String untranslatedRow = [Link]();
[Link](untranslatedRow);
}
}
catch(Exception e){
[Link]("the file couldnt be accessed");
[Link]("Error is "+ e);
}
ArrayList<String> refinedCode = new ArrayList<String>();
// we will now call the refining function.
// to ignore the comments.
refinedCode = refiningFunction(untranslatedCode);
ArrayList<String> untranslatedCode_new = refinedCode;
printingFunction(untranslatedCode_new);
//Now that we have a untranslated code stored in our data structure we have to
// make a translation function
translatedCode = translationFunction(untranslatedCode_new);
printingFunction(translatedCode);

[Link]();
}

public static void printingFunction(ArrayList<String> toPrint){


for(String row : toPrint){
[Link](row);
}
}

public static ArrayList<String> translationFunction(ArrayList<String>


untranslatedCode){
ArrayList<String> translatedCode = new ArrayList<String>();
ArrayList<String> variables = new ArrayList<String>();
int i = 0;
int variableCounter = 16;
variables = predefinedAddition();
while(i <= [Link]() -1){
char fistCharacter = [Link](i).charAt(0);
char secondCharacter = [Link](i).charAt(1);
if(fistCharacter == '('){ //Lets deal with loops first 2 if's
String labelName = insideBracketFunction([Link](i));
int location = i;
String variableValue = [Link](location);
String variableName = labelName + ";" + variableValue;
[Link](variableName);
}

if(fistCharacter == '@' && (secondCharacter == '0' || secondCharacter == '1'


|| secondCharacter == '2' ||
secondCharacter == '3' || secondCharacter == '4' || secondCharacter == '5' ||
File: /home/dhruvsud/Documents/JAVA/Assembler/[Link] Page 2 of 4

secondCharacter == '6' ||
secondCharacter == '7' || secondCharacter == '8' || secondCharacter == '9'))
{
//checking if it is a number or a variable at second place.
String translatedRow = binaryFunction([Link](i));
[Link](translatedRow);
}
else if(fistCharacter == '@' && (secondCharacter != '0' || secondCharacter !=
'1' ||
secondCharacter != '2' ||secondCharacter != '3' || secondCharacter != '4' ||
secondCharacter != '5' || secondCharacter != '6' || secondCharacter != '7' ||
secondCharacter != '8' || secondCharacter != '9')){
String[] temporary = [Link](i).split("@");
String translatedRow;
String variableName = temporary[1];

if(variableCheckFunction(variables, variableName) == false){


variableName = variableName + ";" +
[Link](variableCounter); //CSV
// CSV --> Comma Separated Values
[Link](variableName);
translatedRow =
variableTranslationFunction([Link]([Link]()-1));
variableCounter++; // we start from sixteen
}

else{
translatedRow = alreadyExistingFunction(variables, variableName);
}
[Link](translatedRow);
}
i++;
}

return translatedCode;
}
public static String insideBracketFunction(String label){
int i = 0;
char characterValue = [Link](i);
String name = "";
while(true){
i++;
characterValue = [Link](i);
if(characterValue == ')'){
break;
}
name = name + [Link](i);
}
return name;
}
public static boolean variableCheckFunction(ArrayList<String> variables, String
varibleName){
boolean checker = false;
for(int i = 0; i <= [Link]() -1; i++){
String temporary = [Link](i);
String[] temporary1 = [Link](";");
String temporary2 = temporary1[0];
if([Link](varibleName)){
checker = true;
}
}
return checker;
}
public static String alreadyExistingFunction(ArrayList<String> variables, String
variableName){
String translatedRow;
File: /home/dhruvsud/Documents/JAVA/Assembler/[Link] Page 3 of 4

int value = 0;
for(int i = 0; i <= [Link]() -1; i++){
String[] temporary = [Link](i).split(";");
String temporary1 = temporary[0];
String value1 = temporary[1];
if([Link](variableName)){
value = [Link](value1);
break;
}
}
translatedRow = binaryFunction(value);
return translatedRow;
}

public static String variableTranslationFunction(String untranslatedRow){


String translatedRow;
String[] temporary = [Link](";");
translatedRow = temporary[1];
String translatedBinaryRow = binaryFunction([Link](translatedRow));
return translatedBinaryRow;
}

public static ArrayList<String> refiningFunction(ArrayList<String> untranslatedCode){


ArrayList<String> refinedCode = new ArrayList<String>();
int i = 0;
while(i <= [Link]() -1){
if([Link](i).equals("") != true){ // now we can ignore
blankLines
if([Link](i).charAt(0) != '/'){
// we are using this function to ignore the comments in the code
String row = [Link](i);
[Link](row);
}
}
i++;
}
return refinedCode;
}

public static String binaryFunction(String untranslatedRow){

String translatedRow = "";


String[] temporarySplitter = [Link]("@");
String valueStr = temporarySplitter[1];
int value = [Link](valueStr);
int quotient = (value / 2);
int remainder = (value % 2);
int counter = 1;
value = quotient;
//int placeCounter = 0; //first we are operating at the base of 10
//int placeValue = (int)[Link](10, placeCounter); //typeCasting double -->
integer
//int binaryValue = remainder*placeValue;
translatedRow = [Link](remainder) + translatedRow;
while(quotient != 0){
quotient = value/2;
remainder = value%2;
value = quotient;
translatedRow = [Link](remainder) + translatedRow;
counter++;
}
String zeros = "";
for(int i = 1; i <= 16-(counter+1); i++){
zeros = zeros + "0";
}
translatedRow = "0" + zeros + translatedRow;
File: /home/dhruvsud/Documents/JAVA/Assembler/[Link] Page 4 of 4

return translatedRow;
}

public static String binaryFunction(int value){


String binaryTranslation = "";
int quotient = (value / 2);
int remainder = (value % 2);
int counter = 1;
value = quotient;
binaryTranslation = [Link](remainder) + binaryTranslation;
while(quotient != 0){
quotient = value/2;
remainder = value%2;
value = quotient;
binaryTranslation = [Link](remainder) + binaryTranslation;
counter++;
}
String zeros = "";
for(int i = 1; i <= 16-(counter+1); i++){
zeros = zeros + "0";
}
binaryTranslation = "0" + zeros + binaryTranslation;
return binaryTranslation;
}
public static ArrayList<String> predefinedAddition(){
ArrayList<String> keyWords = new ArrayList<String>();
String[] keywords = {"R0;0", "R1;1", "R2;2", "R3;3", "R4;4", "R5;5", "R6;6",
"R7;7",
"R8;8", "R9;9", "R10;10", "R11;11", "R12;12", "R13;13",
"R14;14",
"R15;15", "SCREEN;16384", "KBD;24576", "SP;0", "LCL;1", "ARG;
2",
"THIS;3", "THAT;4" };

for(int i = 0; i <= [Link] -1; i++){


[Link](keywords[i]);
}
return keyWords;
}
}

You might also like