Convert Hex To Dec in java with HashMap.. I\'m making a compiler for assembler i
ID: 3866862 • Letter: C
Question
Convert Hex To Dec in java with HashMap..
I'm making a compiler for assembler in JAVA. My program after several processes leaves a text file as follows. example.ASM file with this:
CLO
Start:
MOV AL,0
OUT 01
MOV AL,FC
OUT 01
JMP Start
END
In my program i have a hash map like this.
HashMap<String, String> myObjs =new HashMap<>();
myObjs.put("MOV","D0");
myObjs.put("END","A0");
myObjs.put("SUB","A1");
myObjs.put("MUL","A2");
myObjs.put("DIV","A3");
myObjs.put("JMP","A4");
myObjs.put("AND","AA");
myObjs.put("AL","AB");
myObjs.put("OUT","C4");
myObjs.put("Start","C6");
myObjs.put("CLO","CA");
How can I by means of that hashmap convert the commands to his assigned hexadecimal value?
Sample program output for example.ASM file:
CA C6 D0 AB 0 C4 01 D0 AB FC C4 01 A4 C6 A0
Explanation / Answer
Hi,
Below is the TestMain.java, which takes example.ASM as input and reads the hashmap and provides its corresponsing values as output.
package decToHexaDec;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
public class TestMain {
public static void main(String[] args) {
HashMap<String, String> myObjs = new HashMap<>();
myObjs.put("MOV", "D0");
myObjs.put("END", "A0");
myObjs.put("SUB", "A1");
myObjs.put("MUL", "A2");
myObjs.put("DIV", "A3");
myObjs.put("JMP", "A4");
myObjs.put("AND", "AA");
myObjs.put("AL", "AB");
myObjs.put("OUT", "C4");
myObjs.put("Start", "C6");
myObjs.put("CLO", "CA");
//calls the method to read file and to store the output, provide the input file path as parameter
String output = readFile(myObjs, "D:\workspace\Test\src\decToHexaDec\example.ASM");
//print the output
System.out.println(output);
}
/**
* @param Hashmap, fileName
* @return list of key values for the given input file
*/
public static String readFile(HashMap<String, String> map, String fileName) {
//to scan the values from the file
Scanner in;
String out ="";
try {
//stores the contents from example.ASM file
in = new Scanner(new File(fileName));
String line;
// checks if file has more lines
while (in.hasNextLine()) {
line = in.nextLine();
//splits the lines when there is space or comma(,)
String tokens[] = line.split(",|\s");
//loop to go through each command available in input file
for (int i = 0; i < tokens.length; i++) {
//replces the special character ":" if available in the command
String temp = tokens[i].replace(":", "");
//gets the value of key in the map and stores
temp = map.get(temp);
//if value of corresponding key is not available assigns the original command
if (temp == null)
temp = tokens[i];
//adds to the out String where all the values of corresponding keys are stored.
out=out+" "+temp;
}
}
// close scanner
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return out;
}
}
Sample Output:
Input: example.ASM
CLO
Start:
MOV AL,0
OUT 01
MOV AL,FC
OUT 01
JMP Start
END
Output:
CA C6 D0 AB 0 C4 01 D0 AB FC C4 01 A4 C6 A0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.