You have to create a MARIE Computer Simulator. So basically, you have to write a
ID: 3840994 • Letter: Y
Question
You have to create a MARIE Computer Simulator.
So basically, you have to write a program that takes the assembly code as input and write the output of accumulator when it sees HALT instruction.
You can use JAVA to the write the program.
Sample input file contents:
100 Load 104
101 Add 105
102 Store 106
103 HALT
104 0023
105 FFE9
106 0000
The output should be for example, like the image below.
ORG 100 Example 4.1 Load Addr /Load address of first number to be added Store Next /Store this address is our Next pointer Load Num /Load the number of items to be added Subt One /Decrement to control looping Store Ctr /Store this value in Ctr oop, Load Sum /Load the Sum into AC Next /Add the value pointed to by location Next AddI Store Sum /Store this sum Load Next /Load Next Add One /Increment by one to point to next address Store Next /Store in our pointer Next Load Ctr /Load the loop control variable Subt One /Subtract one from the loop control variable Store Ctr /Store this new value in loop control variable Skip Cond 000 /If control variable 0, skip next instruction Loop /Otherwise, go to Loop Jump Halt /Terminate program Addr Hex 117 /Numbers to be summed start at location 118 /A pointer to the next number to add Next Hex /The number of values to add Num Dec Sum Dec The sum Ctr Hex /The loop control variable One /Used to increment and decrement by 1 DeC 10 /The values to be added together Dec Dec 15 Dec 20 25 Dec Dec 30Explanation / Answer
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
const int WRITE = 11;
const int READ = 10;
const int STORE = 21;
const int LOAD = 20;
const int SUBTRACT = 31;
const int ADD = 30;
const int DIVIDE = 32;
const int BRANCH = 40;
const int MULTIPLY = 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 the name of the 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 << " The information in the file has 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 READ:
cout << "Enter an integer: " << endl;
cin >> mem[opd];
insCounter++;
break;
case WRITE:
cout << mem[opd] << endl;
insCounter++;
break;
case LOAD:
accum = mem[opd];
insCounter++;
break;
case STORE:
mem[opd] = accum;
insCounter++;
break;
case ADD:
accum += mem[opd];
insCounter++;
break;
case SUBTRACT:
accum -= mem[opd];
insCounter++;
break;
case MULTIPLY:
accum *= mem[opd];
insCounter++;
break;
case DIVIDE:
if (mem[opd] == 0){
cout << "divide by zero " << endl;
exit(1);
break;
}
accum /= mem[opd];
insCounter++;
break;
case BRANCH:
insCounter = operand;
insCounter++;
break;
case HALT:
cout << " execution terminated " << endl;
system ("pause");
exit (0);
break;
default:
cout << " ERROR: Invalid SML instruction -- Program terminating" << endl;
system ("pause");
exit (1);
break;
}
}while (insRegister != HALT && insRegister != 1);}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.