-4

I have some instructions for a cpu so i have assembly code and how can i translate them to machine code in java.

I have code like this:

LI 5
ST 1
AD 1
ST 0
LI 0
BZ 0

And i want to them like this:

85 41 c1 40 80 00

That assembly code stored in a txt file, so I have tried to reading that datas from txt file try {

        String f = "C:\\Users\\comp\\Desktop\\CompArch\\a.txt";
        FileReader fw = new FileReader(f);
        BufferedReader assembly_file = new BufferedReader(fw);


        Hashtable<String, String> symtab = new Hashtable<String, String>();
        String str;
        int z=0;


        String f4 = "C:\\Users\\comp\\Desktop\\CompArch\\MACHINE_CODE.asm";
        FileWriter fw4 = new FileWriter(f4);
        BufferedWriter machine_code_file = new BufferedWriter(fw4);
Codeisacode
  • 37
  • 1
  • 7
  • Core java do not provide that functionality. Still, maybe there are other packages developed by others which can provide something with `asm`, Look at the following link `http://jasmin.sourceforge.net/guide.html`, it's not exactly what you asked but you can made yourself an idea.(personally, i do not think what you asked even exists for java) – Traian GEICU May 22 '19 at 17:54
  • Thank you for your reply, but it should be exist because I was asked to do this. Then i convert them to machine code as an asm file, I will took that file and put into my cpu which is on logisim. – Codeisacode May 22 '19 at 18:20
  • Assume that the translation from machine code to hex is specific just for a particular architecture.(see Bas van der Linden). Then maybe you could find a package to do that. But even if not exists you could do your own parser to do the translation.(LI -> something and with vars -> 85 ... for a limited set of instructions it could be achieved). Better ask the person who give you the task further clarification, because maybe what your are asked to do have a much simple solution. – Traian GEICU May 22 '19 at 18:28
  • Thank you again. I understood properly now. – Codeisacode May 22 '19 at 18:33
  • Maybe a duplicate of [Alternative technique to write an assembler](//stackoverflow.com/q/15184164), but that's about one aspect of writing an assembler in Java, and assumes you already know the basics of reading a text file, parsing it, and writing a binary file. – Peter Cordes May 22 '19 at 20:41

1 Answers1

1

Your question is quite unclear. It seems as if you want the to translate the assembly instruction to their corresponding machine code, but then you follow with your code about how you read the input file.

First things first, your code for reading your input.asm file does not actually read the file. You create an BufferdReader but never read from it (why show the code then).

Second, machine code may vary from architecture to architecture you intend to build the program on. If you have in mind what HEX number matches your instruction, you could build a hashmap that translates from instruction code to the corresponding HEX number.

Otherwise I would recommend finding a IDE for assembly and seeing if it has a feature that show the compiled assembly as machine code. Or you could compile the assembly yourself and look at the machine code of the compiled program, and then automate that using java.

Hope that helped, if I didn't understand your question correctly please comment, or post a response.

  • First of all, thank you very much for your reply. I may not have been able to fully express my problem. Anyways the thing that i want to say is, I want to write some java code which takes the assembly code and converts it to a machine code (expressed in HEX). – Codeisacode May 22 '19 at 18:15