Java Compiler - Load Method
<p>So I have been working on a java project where the goal is to create a virtual computer. So I am basically done but with one problem. I have created a compiler which translates a txt document with assembly code in it and my compiler has created a new-file with this code written as machine executable ints. But now I need to write a load method that reads these ints and runs the program but I am having difficulty doing this. Any help is much appreciated....also this is not homework if you are thinking this. The project was simply to make a compiler and now I am trying to complete it for my own interest. Thanks.</p>

<p>Here is what I have so far for load:  </p>

<pre><code>public void  load(String newfile) throws FileNotFoundException
{
    try{
        File file = new File(newfile);
        FileInputStream fs = new FileInputStream(file);
        DataInputStream dos = new DataInputStream(fs);
        dos.readInt();
        dos.close();
    }
    catch (IOException e)
        {
            e.printStackTrace();
                }

}
</code></pre>

<p>Ok here is the part of the Compiler that does the writeInts:</p>

<pre><code> public void SecondPass(SymbolList symbolTable, String filename){
        try {
            int dc = 99;
            //Open file for reading
            File file = new File(filename);
            Scanner scan = new Scanner(file);

            //Make filename of new executable file

            String newfile = makeFilename(filename);

            //Open Output Stream for writing new file.

            FileOutputStream fs = new FileOutputStream(newfile);
            DataOutputStream dos = new DataOutputStream(fs);

            //Read First line. Split line by Spaces into linearray.
            String line = scan.nextLine();
            String[] linearray = line.split(" ");

            while(line!=null){
                if(!linearray[0].equals("REM")){
                    int inst = 0, opcode, loc;
                    if(isInstruction(linearray[0])){
                        opcode = getOpcode(linearray[0]);
                        loc = symbolTable.searchName(linearray[1]).getMemloc();
                        inst = (opcode*100)+loc;

                    } else if(!isInstruction(linearray[0])){
                        if(isInstruction(linearray[1])){
                            opcode = getOpcode(linearray[1]);
                            if(linearray[1].equals("STOP"))
                                inst=0000;
                            else {
                                loc = symbolTable.searchName(linearray[2]).getMemloc();
                                inst = (opcode*100)+loc;

                            }
                        }
                        if(linearray[1].equals("DC"))
                            dc--;
                    }
                    dos.writeInt(inst);

                    System.out.println(" inst is being written as:" + inst);
                }
                try{
                    line = scan.nextLine();
                }
 catch(NoSuchElementException e){
                    line = null;
                    break;
                }
                linearray = line.split(" ");

            }
            scan.close();
            for(int i=lc; i&lt;=dc; i++){
                dos.writeInt(0);

            }

            for(int i = dc+1; i &lt; 100; i++)
                {
                    dos.writeInt(symbolTable.searchLocation(i).getValue());

                }


            dos.close();
            fs.close();
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
</code></pre>

<p>So what I have done is write a file in txt like:  </p>

<pre><code>IN X
In Y
SUB X
STO Y
OUT Y
DC: X 0
DC: Y 0
</code></pre>

<p>And I wrote a compiler that has now converted this file into machine code so I have created a file for example called program.txt.ex and it contains a bunch of @@@@@@@ or machine code and I did this using the SecondPass code above and now I need to write a load method that will allow me to load and run this file. </p>

<p>Here is my Run method </p>

<pre><code> public void run(String filename) throws IOException
    {
        if (mem == null)
            System.out.println("mem null");
        if (filename == null)
            System.out.println("filename null");
        mem.loadFromFile(filename);
        cpu.reset();
        cpu.setMDR(mem.read(cpu.getMAR()));
        cpu.fetch2();
        while (!cpu.stop())
            {
                cpu.decode();
                if (cpu.OutFlag())
                    OutPut.display(mem.read(cpu.getMAR()));
                if (cpu.InFlag())
                    mem.write(cpu.getMDR(),in.getInt());
                if (cpu.StoreFlag())
                    {
                        mem.write(cpu.getMAR(),in.getInt());
                        cpu.getMDR();
                    }
                else
                    {
                        cpu.setMDR(mem.read(cpu.getMAR()));
                        cpu.execute();
                        cpu.fetch();
                        cpu.setMDR(mem.read(cpu.getMAR()));
                        cpu.fetch2();
                    }
            }
    }
</code></pre>

<p>The Run Method:</p>

<pre><code>public void run(int mem)
    {
        cpu.reset();
        cpu.setMDR(mem.read(cpu.getMAR()));
        cpu.fetch2();
        while (!cpu.stop())
            {
                cpu.decode();
                if (cpu.OutFlag())
                    OutPut.display(mem.read(cpu.getMAR()));
                if (cpu.InFlag())
                    mem.write(cpu.getMDR(),in.getInt());
                if (cpu.StoreFlag())
                    {
                        mem.write(cpu.getMAR(),in.getInt());
                        cpu.getMDR();
                    }
                else
                    {
                        cpu.setMDR(mem.read(cpu.getMAR()));
                        cpu.execute();
                        cpu.fetch();
                        cpu.setMDR(mem.read(cpu.getMAR()));
                        cpu.fetch2();
                    }
            }
}
</code></pre>
