Question: in c++ given skeleton code : #include #include #include sml.h #include simplesim.h using namespace std; simplesim::simplesim() { for (int i = 0; i < 100;
in c++ given skeleton code :
#include
#include "sml.h" #include "simplesim.h"
using namespace std;
simplesim::simplesim() { for (int i = 0; i < 100; i++) memory[i] = 7777; }
bool simplesim::load_program() { int count = 0; int instruction;
while (cin >> instruction && instruction != -99999) { if (not_valid_word(instruction)) { cout << "*** ABEND: pgm load: invalid word *** "; return false; }
if (count >= 100) { // print error and return false. }
// copy instruction into memory and increment count. }
return true; }
bool simplesim::not_valid_word(int word) const { return (word < -9999 || word > 9999); }
void simplesim::execute_program() { bool done = false; instruction_counter = 0;
while (!done) { // instruction fetch // Test instruction counter instruction_register = memory[instruction_counter]; operation_code = instruction_register / 100; operand = instruction_register % 100; // instruction execute switch (operation_code) { case READ: break;
case WRITE: break; // More cases
case HALT: cout << "*** Simplesim execution terminated *** "; done = true; break; default: // Print error and return }
if (/*operation_code is not branching*/ && !done) instruction_counter++; } }
void simplesim::dump() const { // Print the registers. cout << "REGISTERS: ";
cout << "accumulator: " << accumulator << endl; cout << "instruction_counter: " << instruction_counter << endl; cout << "instruction_register: " << instruction_register << endl; cout << "operation_code: " << operation_code << endl; cout << "operand: " << operand << endl;
// Print memory. cout << " MEMORY: ";
for (int i = 0; i < 100; i++) cout << memory[i] << endl; }
simplesim.h
#ifndef SIMPLESIM_H #define SIMPLESIM_H
class simplesim { private: int memory[100]; int accumulator = 0; int instruction_counter = 0; int instruction_register = 0; int operation_code = 0; int operand = 0;
bool not_valid_word(int) const;
public: simplesim(); bool load_program(); void execute_program(); void dump() const; }; #endif
sml.h
#ifndef SML_H #define SML_H
//******************************************************* // sml.h // Assignment 4 // // //*******************************************************
#define READ 11 #define WRITE 12 #define STORE 21 #define LOAD 22 #define ADD 31 #define SUBTRACT 32 #define MULTIPLY 33 #define DIVIDE 34 #define BRANCH 41 #define BRANCHZERO 42 #define BRANCHNEG 43 #define HALT 44
#endif
having trouble getting the read in portion to work. Needs to read from file from bash console, not ask user for input.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
