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
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 \t";
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 \t";
if (insCounter < 10)
cout << "0";
cout << insCounter << endl;
cout << "instructionRegister\t";
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 \t";
if (opCode < 10)
cout << "0";
cout << opCode << endl;
cout << "operand \t";
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);}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
