Question

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

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?

The output of my actual program in a 02TLIGHTHexa.ASM file is:

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();

}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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();
}
  
}

Add a comment
Know the answer?
Add Answer to:
Convert Hex To Dec in java with HashMap and put in .txt file I'm making a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet,...

    Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to EFFICIENTLY accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...

  • Please complete the give code (where it says "insert code here") in Java, efficiently. Read the...

    Please complete the give code (where it says "insert code here") in Java, efficiently. Read the entire input one line at a time and then output a subsequence of the lines that appear in the same order they do in the file and that are also in non-decreasing or non-increasing sorted order. If the file contains n lines, then the length of this subsequence should be at least sqrt(n). For example, if the input contains 9 lines with the numbers...

  • Please answer question correctly and show the result of the working code. The actual and demo...

    Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • I'm working with Java and I have a error message  1 error Error: Could not find or...

    I'm working with Java and I have a error message  1 error Error: Could not find or load main class Flowers. This is the problem: Instructions Make sure the source code file named Flowers.java is open. Declare the variables you will need. Write the Java statements that will open the input file, flowers.dat, for reading. Write a while loop to read the input until EOF is reached. In the body of the loop, print the name of each flower and where...

  • Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK...

    Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK to answer it. It is very essential that you do. Please make sure it is very efficient and doesnt run out of memory. A demo code required for the question is given below. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • Please correct the comments on the java program below jtxtRanking.setText("Invalid marks"); JOptionPane.showMessageDialog(null, "Marks should be between 0-100", "Error", JOptio...

    Please correct the comments on the java program below jtxtRanking.setText("Invalid marks"); JOptionPane.showMessageDialog(null, "Marks should be between 0-100", "Error", JOptionPane.ERROR_MESSAGE); System.exit(0); }    //The DefaultTableModel can be acessed through the getModel method DefaultTableModel model = (DefaultTableModel) jTable.getModel();    //Add a row model.addRow(new Object []{    // receive datas from the following and display it on the table jtxtStudentID.getText(), jcmbCourseCode.getSelectedItem(), jtxtAverage.getText(), jtxtRanking.getText(),    }); try { FileReader fr = new FileReader(file); // read data from the file BufferedReader br = new BufferedReader(fr); // instantiate BufferedReader object   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT