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

Write a Java program to take the assembly code as input and write the output of

ID: 3841051 • Letter: W

Question

Write a Java program to take the assembly code as input and write the output of accumulator when it sees HALT instruction. The program can be implement in general numbers. Sample input file contents: 100 Load 104 101 Add 105 102 Store 106 103 HALT 104 0023 105 FFE9 106 0000

This program is for general number input, use the very basic assembly code as input and write the output of accumulator. I also see some program on Cheg using C++, but I have some chanllenge to write it in Java. If you really confuse, you can search the code in C++ on Chegg and translate to Java Please.

This is the code of C++, which I found it in chegg as you required, please translate to Java code, any questions please let me know!!!!.

Code:

#include

#include

#include

#include

#include

using namespace std;

const int lWRITE = 11;

const int lREAD = 10;

const int lSTORE = 21;

const int lLOAD = 20;

const int lSUBTRACT = 31;

const int lADD = 30;

const int lDIVIDE = 32;

const int lBRANCH = 40;

const int lMULTIPLY = 33;

const int HALT = 43;

bool ERROR;

class Simp

{

   private:

       int accum;

       int insCounter;

       int opCode;

       int opd;

       int insRegister;

       int mem[];

   public:

       Simp(){};

       void LoadMem();

       void DispMem();

       void Exec();

     

};

void Instruction(void);

int main()

{

   Simp mycomp;

   mycomp.LoadMem();

   mycomp.Exec();

   return 0;

}

bool FileExist(string file)

{

ifstream MySimp;

MySimp.open(file.c_str(),ifstream::in);

if(MySimp.good())

   {

   MySimp.close();

   return true ;

   }

return false;

}

void Simp::DispMem()

{

   int mem[100];

   int memsize = 0;

   int sizeval = 0;

   ifstream MySimpletron;

   string file;

   cout << " Enter file : " << endl;

cin >> file;

   cout << " REGISTER: " << endl;

   cout << "accumulator content ";

   if (accum < 10)

   {

       cout << "000";

   }

   else if(accum < 100 && accum > 9)

   {

       cout << "00";

   }

   else if(accum <= 999 && accum > 99)

   {

       cout << "0";

   }

   cout << accum << endl;

   cout << "instructionCounter ";

   if (insCounter < 10)

       cout << "0";

   cout << insCounter << endl;

   cout << "instructionRegister ";

   if (insRegister < 10)

   {

       cout << "000";

   }

   else if(insRegister < 100 && insRegister > 9)

   {

       cout << "00";

   }

   else if(insRegister <= 999 && insRegister > 99)

   {

       cout << "0";

   }

   cout << insRegister << endl;

   cout << "operationCode ";

   if (opCode < 10)

       cout << "0";

   cout << opCode << endl;

   cout << "operand ";

   if (opd < 10)

       cout << "0";

   cout << opd << endl;

   cout << " MEMORY: " << endl;

   MySimp.open(file.c_str(),ifstream::in);     

   MySimp>> insRegister ;

   while (!MySimp.eof())

   {

       mem[Memsize] = insRegister;

       MySimp >> insRegister ;

       cout << setw(5) << mem[Memsize] << setw(5);

       Memsize++;

   }

   MySimp.close();

}

void Simp::LoadMem()

{

   ifstream MySimp;

   string file;

   int mem[100];

   int instruction;

   accum = 0;

   insCounter = 0;

   insRegister = 0;

   opCode = 0;

   opd = 0;

   cout << " Enter the name of the file: " << endl;

cin >> file;  

MySimp.open(file.c_str(),ifstream::in);

int sizeval = 0;

   if(MySimp)

   {  

       cout << " The instructions in file " << file << "following: " << endl;

       MySimp >> instruction ;

       while (!MySimp.eof())

       {

           mem[sizeval] = instruction;

           MySimp >> instruction ;

           cout << setw(5) << mem[sizeval] << setw(5);

           sizeval++;

       }

       cout << " file information been read." << endl;

   }

   else if(MySimp.fail())

   {

           cout << " The file was not able to be opened because it does not exist. " << endl;

   }

   MySimp.close();

}

void Simp::Exec()

{

   Simp mycomp;

   do

   {         

       insRegister = mem[insCounter];

       opCode = insRegister/100;

       opd = insRegister%100;

       mycomputer.DispMem();     

       switch(opCode)

       {

           case lREAD:

               cout << "Enter an integer: " << endl;

               cin >> mem[opd];

               insCounter++;

               break;

           case lWRITE:

               cout << mem[opd] << endl;

               insCounter++;

               break;

           case lLOAD:

               accum = mem[opd];

               insCounter++;

               break;

           case lSTORE:

               mem[opd] = accum;

               insCounter++;

               break;

           case lADD:

               accum += mem[opd];

               insCounter++;

               break;

           case lSUBTRACT:

               accum -= mem[opd];

               insCounter++;

               break;

           case lMULTIPLY:

               accum *= mem[opd];

               insCounter++;

               break;

           case lDIVIDE:

               if (mem[opd] == 0){

                   cout << "divide by 0 " << endl;

             

                   exit(1);

                   break;

               }

               accum /= mem[opd];

               insCounter++;

               break;

           case lBRANCH:

               insCounter = operand;

               insCounter++;

               break;

           case HALT:

               cout << " execution terminated " << endl;

               system ("pause");

               exit (0);

               break;

           default:

               cout << " ERROR: SML instruction invalid " << endl;

               system ("pause");

               exit (1);

               break;

       }

     

   }while (insRegister != HALT && insRegister != 1);}

Explanation / Answer

Converted Java program is as follows:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AssemblyMain {
   public static void main(String args[]) throws IOException {
       Simp mycomp = new Simp();
       mycomp.LoadMem();
       mycomp.Exec();
   }
}

class Simp
{
   private static final int LWRITE = 11;
   private static final int LREAD = 10;
   private static final int LSTORE = 21;
   private static final int LLOAD = 20;
   private static final int LSUBTRACT = 31;
   private static final int LADD = 30;
   private static final int LDIVIDE = 32;
   private static final int LBRANCH = 40;
   private static final int LMULTIPLY = 33;
   private static final int HALT = 43;
   private int accum;
   private int insCounter;
   private int opCode;
   private int opd;
   private int insRegister;
   private int mem[] = new int[100];
   public void LoadMem() throws IOException {
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
       String file;
       int instruction;
       accum = 0;
       insCounter = 0;
       insRegister = 0;
       opCode = 0;
       opd = 0;
       System.out.println(" Enter the name of the file: ");
       file = bufferedReader.readLine();
       BufferedReader MySimp = new BufferedReader(new FileReader(file));
       int sizeval = 0;
       System.out.println(" The instructions in file " + file + "following: ");
       while((instruction = MySimp.read()) != -1) {
           mem[sizeval] = instruction;
           System.out.println(mem[sizeval]);
           sizeval++;
       }
       System.out.println(" file information been read.");
       MySimp.close();
   }
   public void DispMem() throws IOException {
       int memsize = 0;
       String file;
       System.out.println(" Enter file : ");
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
       file = bufferedReader.readLine();
       System.out.println(" REGISTER: ");
       System.out.println("accumulator content ");
       if (accum < 10)
       {
           System.out.print("000");
       }
       else if(accum < 100 && accum > 9)
       {
           System.out.print("00");
       }
       else if(accum <= 999 && accum > 99)
       {
           System.out.print("0");
       }
       System.out.println(accum);
       System.out.print("instructionCounter ");
       if (insCounter < 10)
           System.out.print("0");
       System.out.println(insCounter);
       System.out.print("instructionRegister ");
       if (insRegister < 10)
       {
           System.out.print("000");
       }
       else if(insRegister < 100 && insRegister > 9)
       {
           System.out.print("00");
       }
       else if(insRegister <= 999 && insRegister > 99)
       {
           System.out.print("0");
       }
       System.out.println(insRegister);
       System.out.print("operationCode ");
       if (opCode < 10)
           System.out.print("0");
       System.out.println(opCode);
       System.out.print("operand ");
       if (opd < 10)
           System.out.print("0");
       System.out.println(opd);
       System.out.println(" MEMORY: ");
       BufferedReader MySimp = new BufferedReader(new FileReader(file));
       while ((insRegister = MySimp.read()) != -1)
       {
           mem[memsize] = insRegister;
           System.out.println(mem[memsize]);
           memsize++;
       }
       MySimp.close();
   }
   public void Exec() throws IOException {
       do
       {
           insRegister = mem[insCounter];
           opCode = insRegister/100;
           opd = insRegister%100;
           DispMem();
           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
           switch(opCode)
           {
           case LREAD:
               System.out.println("Enter an integer: ");
               mem[opd] = bufferedReader.read();
               insCounter++;
               break;
           case LWRITE:
               System.out.println(mem[opd]);
               insCounter++;
               break;
           case LLOAD:
               accum = mem[opd];
               insCounter++;
               break;
           case LSTORE:
               mem[opd] = accum;
               insCounter++;
               break;
           case LADD:
               accum += mem[opd];
               insCounter++;
               break;
           case LSUBTRACT:
               accum -= mem[opd];
               insCounter++;
               break;
           case LMULTIPLY:
               accum *= mem[opd];
               insCounter++;
               break;
           case LDIVIDE:
               if (mem[opd] == 0){
                   System.out.println("divide by 0 ");
                   System.exit(1);
                   break;
               }
               accum /= mem[opd];
               insCounter++;
               break;
           case LBRANCH:
               insCounter = opd;
               insCounter++;
               break;
           case HALT:
               System.out.println(" execution terminated ");
               System.out.println("pause");
               System.exit(0);
               break;
           default:
               System.out.println(" ERROR: SML instruction invalid ");
               System.out.println("pause");
               System.exit(1);
               break;
           }

       } while (insRegister != HALT && insRegister != 1);
   }
}

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