Convert Hex To Dec in java with HashMap and put in .txt file I\'m making a compi
ID: 3867179 • Letter: C
Question
Convert Hex To Dec in java with HashMap and put in .txt file
I'm making a compiler for assembler in JAVA. My program after several processes leaves a text file as follows. 02TLIGHT2.ASM file with this:
CLO
Start:
MOV AL,0
OUT 01
MOV AL,FC
OUT 01
JMP Start
END
Im trying by means of my hashmap convert the commands to his assigned hexadecimal value and put it in a text file?
FE
D0 00 00
F1 null
D0 00 null
F1 null
C0 F6
The problem is that when the program does not find something in the hashmap it puts it as null, what I need is that if it does not find it write the same and not null. Also ... the program validates if there is a start or end, if it finds them deletes but leaves a space. Is it possible to remove that space?
Example: If the program reads FC and is not in the hashmap, write FC. As in the example below
Sample program output i need in 02TLIGHTHexa.ASM file
FE
D0 00 00
F1 01
D0 00 FC
F1 01
C0 F6
++++++++++++++++++++++++++++CODE++++++++++++++++++++++++++++++++++++++++++++++++++
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hexap;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.*;
import static jdk.nashorn.internal.objects.NativeArray.map;
//import static softwaresistemas.CambiaReg.CambiaReg;
//import static softwaresistemas.eliminaContenido.eliminaContenido;
/**
*
* @author Naf_Ross
*/
public class HexaP {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
String linea;
HashMap map=new HashMap();
map.put("MOV","D0");
map.put("ADD","A0");
map.put("SUB", "A1");
map.put("MUL", "A2");
map.put("DIV","A3" );
map.put("INC", "A4");
map.put("DEC", "A5");
map.put("AND","AA");
map.put("OR","AB");
map.put("XOR","AC");
map.put("NOT","AD" );
map.put("ROL","9A" );
map.put("ROR", "9B");
map.put("SHL", "9C");
map.put("SHR", "9D");
map.put("CMP","DA" );
map.put("JMP","C0" );
map.put("JZ", "C1");
map.put("JNZ","C2" );
map.put("JS","C3" );
map.put("JNS", "C4");
map.put("JO","C5" );
map.put("JNO", "C6");
map.put("CALL","CA" );
map.put("RET", "CB");
map.put("INT","CC" );
map.put("IRET","CD" );
map.put("POP","E1" );
map.put("PUSH","E0" );
map.put("PUSHF","EA" );
map.put("POPF","EB" );
map.put("IN","F0" );
map.put("OUT","F1" );
map.put("CLO","FE" );
map.put("HALT","00" );
map.put("NOP", "FF");
map.put("STI", "FC");
map.put("CLI", "FD");
map.put("ORG", "");
map.put("DB","" );
map.put("AL","00" );
map.put("BL","01" );
map.put("CL","02" );
map.put("DL","03" );
map.put("Start","F6" );
//map.put("FC","FC" );
File archivoFuente = new File ("C:/Program Files/Ensamblador/02TLIGHT2.ASM");
FileReader fr = new FileReader (archivoFuente);
BufferedReader br = new BufferedReader(fr);
FileWriter archivoMaquina = new FileWriter("C:/Program Files/Ensamblador/02TLIGHT2Hexa.ASM");
PrintWriter pw = new PrintWriter(archivoMaquina);
while ((linea=br.readLine())!=null) {
linea = linea.trim();
String[] lineaToPrint = linea.split(",|\s");
String[] counterToPrint = linea.split(",|\s");
int i;
for(i=0; i if(lineaToPrint[i].equals("START:") || lineaToPrint[i].equals("END") || lineaToPrint[i].equals("Start:")){
continue;
}else{
if(lineaToPrint[i].equals("0") || lineaToPrint[i].equals("1")){
counterToPrint[i] = "0"+ lineaToPrint[i];
}
else{
counterToPrint[i] = (String)map.get(lineaToPrint[i]);
}
pw.print(counterToPrint[i] + " ");
System.out.print(counterToPrint[i] + " ");
}
}
System.out.println();
pw.println();
}
fr.close();
archivoMaquina.close();
}
}
Explanation / Answer
package hexap;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.regex.*;
import static jdk.nashorn.internal.objects.NativeArray.map;
//import static softwaresistemas.CambiaReg.CambiaReg;
//import static softwaresistemas.eliminaContenido.eliminaContenido;
/**
*
* @author Naf_Ross
*/
public class HexaP {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
String linea;
HashMap map=new HashMap();
map.put("MOV","D0");
map.put("ADD","A0");
map.put("SUB", "A1");
map.put("MUL", "A2");
map.put("DIV","A3" );
map.put("INC", "A4");
map.put("DEC", "A5");
map.put("AND","AA");
map.put("OR","AB");
map.put("XOR","AC");
map.put("NOT","AD" );
map.put("ROL","9A" );
map.put("ROR", "9B");
map.put("SHL", "9C");
map.put("SHR", "9D");
map.put("CMP","DA" );
map.put("JMP","C0" );
map.put("JZ", "C1");
map.put("JNZ","C2" );
map.put("JS","C3" );
map.put("JNS", "C4");
map.put("JO","C5" );
map.put("JNO", "C6");
map.put("CALL","CA" );
map.put("RET", "CB");
map.put("INT","CC" );
map.put("IRET","CD" );
map.put("POP","E1" );
map.put("PUSH","E0" );
map.put("PUSHF","EA" );
map.put("POPF","EB" );
map.put("IN","F0" );
map.put("OUT","F1" );
map.put("CLO","FE" );
map.put("HALT","00" );
map.put("NOP", "FF");
map.put("STI", "FC");
map.put("CLI", "FD");
map.put("ORG", "");
map.put("DB","" );
map.put("AL","00" );
map.put("BL","01" );
map.put("CL","02" );
map.put("DL","03" );
map.put("Start","F6" );
//map.put("FC","FC" );
File archivoFuente = new File ("C:/Program Files/Ensamblador/02TLIGHT2.ASM");
FileReader fr = new FileReader (archivoFuente);
BufferedReader br = new BufferedReader(fr);
FileWriter archivoMaquina = new FileWriter("C:/Program Files/Ensamblador/02TLIGHT2Hexa.ASM");
PrintWriter pw = new PrintWriter(archivoMaquina);
while ((linea=br.readLine())!=null) {
linea = linea.trim();
String[] lineaToPrint = linea.split(",|\s");
String[] counterToPrint = linea.split(",|\s");
int i;
for(i=0; i if(lineaToPrint[i].equals("START:") || lineaToPrint[i].equals("END") || lineaToPrint[i].equals("Start:")){
counterToPrint[i]==" "; //print space for start and end
continue;
}else{
if(lineaToPrint[i].equals("0") || lineaToPrint[i].equals("1")){
counterToPrint[i] = "0"+ lineaToPrint[i];
}
else{
if(map.contains(lineaToPrint[i]))
counterToPrint[i] = (String)map.get(lineaToPrint[i]);
else
lineaToPrint[i]; // if not available in the map print same
}
pw.print(counterToPrint[i] + " ");
System.out.print(counterToPrint[i] + " ");
}
}
System.out.println();
pw.println();
}
fr.close();
archivoMaquina.close();
}
}