Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Convert Hex To Dec in java with HashMap and put in .txt file... I\'m making a co

ID: 3867144 • 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 (When a single number exists, a 0 is added before in the conversion to hexadecimal)
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 in the same format?

What i can be doing wrong?

Sample program output i need in 02TLIGHTHexa.ASM file:

FE

DO 00 00   

F1 01

DO 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" );
  
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/02TLIGHTHexa.ASM");
PrintWriter pw = new PrintWriter(archivoMaquina);

while ((linea=br.readLine())!=null) { // while que leerea todo el archivo
linea = linea.trim();   
String[] lineaToPrint = linea.split(",|\s");
String[] counterToPrint = linea.split(",|\s");
int i;
for(i=0; i<lineaToPrint.length; i++){
counterToPrint[i] = (String)map.get(lineaToPrint[i]);
pw.print(counterToPrint);//esta linea es la que se supone lo imprime en el archivo TXT
System.out.println(counterToPrint[i]);
}
}
fr.close();
archivoMaquina.close();
// TODO code application logic here
}
  
}

Explanation / Answer

Solution:

The code has been corrected except a few :

FC, Start these command interpretations are not clear.

Apart from these two the code had a few corrections. The two *null* in the output are the above two.

/*
* 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" );
//adding start

File archivoFuente = new File ("C:\cheg\02TLIGHT.txt");
FileReader fr = new FileReader (archivoFuente);
BufferedReader br = new BufferedReader(fr);
FileWriter archivoMaquina = new FileWriter("C:\cheg\02TLIGHT2Hexa.txt");
PrintWriter pw = new PrintWriter(archivoMaquina);
while ((linea=br.readLine())!=null) { // while que leerea todo el archivo
linea = linea.trim();   
String[] lineaToPrint = linea.split(",|\s");
String[] counterToPrint = linea.split(",|\s");
int i;
for(i=0; i<lineaToPrint.length; i++){
if(lineaToPrint[i].equals("START:") || lineaToPrint[i].equals("END")){
continue;
}else{
if(lineaToPrint[i].equals("0") || lineaToPrint[i].equals("1")){
counterToPrint[i] = "0"+ lineaToPrint[i];
}else if(lineaToPrint[i].equals("01") || lineaToPrint[i].equals("10") || lineaToPrint[i].equals("11") || lineaToPrint[i].equals("00")){
counterToPrint[i] = lineaToPrint[i];
}else{
counterToPrint[i] = (String)map.get(lineaToPrint[i]);
}
pw.print(counterToPrint[i] + " ");//esta linea es la que se supone lo imprime en el archivo TXT
System.out.print(counterToPrint[i] + " ");
}
}
System.out.println();
pw.println();
}
fr.close();
archivoMaquina.close();
// TODO code application logic here
}
  
}

OUTPUT:

FE

D0 00 00
F1 01
D0 00 null
F1 01
C0 null

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote