Hey there, I'm having trouble getting my program to read every instruction to then execute it, as well as displaying everything correctly. Right now my program displays mainly garbage and for now I simply want it to be able to successfully run a simple addition program, so any advice or hints, or even any links to send me for info would be great.
Here's my program so far:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int READ = 10;
const int WRITE = 11;
const int LOAD = 20;
const int STORE = 21;
const int ADD = 30;
const int SUBTRACT = 31;
const int DIVIDE = 32;
const int MULTIPLY = 33;
const int BRANCH = 40;
const int BRANCHNEG = 41;
const int BRANCHZERO = 42;
const int HALT = 43;
bool ERROR;
//# define Msize 100;
class Simpletron{
private:
int accumulator;
int instructionCounter;
int operationCode;
int operand;
int instructionRegister;
int memory[];
public:
Simpletron(){};
void LoadMemory();
void DisplayMemory();
void Execute();
};
void Instructions(void);
int main(){
Simpletron mycomputer;
Instructions();
mycomputer.LoadMemory();
mycomputer.Execute();
system ("pause");
return 0;
}
// Functions
void Instructions(){
cout << "*** Welcome to Simpletron! ***" << endl;
cout << "*** Please enter your program one instruction ***" << endl;
cout << "*** (or data word) at a time. I will type the ***" << endl;
cout << "*** location number and a question mark (?). ***" << endl;
cout << "*** You then type the word for that location. ***" << endl;
cout << "*** Type the sentinel -99999 to stop entering ***" << endl;
cout << "*** your program. ***" << endl;
}
bool FileExists(string file){
// Funcion para saber si un curso existe o no existe.
ifstream MySimpletron;
MySimpletron.open(file.c_str(),ifstream::in);
if(MySimpletron.good()){
MySimpletron.close();
return true ;
}
return false;
}
void Simpletron::DisplayMemory(){
int memory[100];
int Msize = 100;
cout << "\nREGISTERS: " << endl;
cout << "accumulator \t";
if (accumulator < 10){
cout << "000";
}
else if(accumulator < 100 && accumulator > 9){
cout << "00";
}
else if(accumulator <= 999 && accumulator > 99){
cout << "0";
}
cout << accumulator << endl;
cout << "instructionCounter \t";
if (instructionCounter < 10)
cout << "0";
cout << instructionCounter << endl;
cout << "instructionRegister\t";
if (instructionRegister < 10){
cout << "000";
}
else if(instructionRegister < 100 && instructionRegister > 9){
cout << "00";
}
else if(instructionRegister <= 999 && instructionRegister > 99){
cout << "0";
}
cout << instructionRegister << endl;
cout << "operationCode \t";
if (operationCode < 10)
cout << "0";
cout << operationCode << endl;
cout << "operand \t";
if (operand < 10)
cout << "0";
cout << operand << endl;
cout << "\nMEMORY: " << endl;
ifstream MySimpletron;
for(int i = 0; i < Msize;i++){
MySimpletron >> instructionRegister ;
memory[Msize] = instructionRegister;
operationCode = instructionRegister/100;
operand = instructionRegister%100;
cout << setw(5) << memory[operand] << setw(5);
}
}
void Simpletron::LoadMemory(){
ifstream MySimpletron; // To read the file named MySimpletron.
string file; // To store the name of the file.
float memory[100];
accumulator = 0;
instructionCounter = 0;
instructionRegister = 0;
operationCode = 0;
operand = 0;
cout << "\nEnter the name of the file: " << endl;
cin >> file;
MySimpletron.open(file.c_str(),ifstream::in); // To open the file.
int Msize = 0; // To clean the array when it opens the file again.
if(MySimpletron){ // This displays the contents of the file, if it was able to open.
cout << "\nThe instructions contained in the file " << file << " are the following: " << endl;
MySimpletron >> instructionRegister ;
while (!MySimpletron.eof()){ // It keeps reading the file until it ends.
memory[Msize] = instructionRegister;
MySimpletron >> instructionRegister ;
cout << setw(5) << memory[Msize] << setw(5);
instructionCounter++;
operationCode = instructionRegister/100;
operand = instructionRegister%100;
Msize++;
}
cout << "\nThe information in the file has been read." << endl;
}
else if(MySimpletron.fail()){ // This indicates that the file was not able to open.
cout << "\nThe file was not able to be opened because it does not exist. " << endl;
}
MySimpletron.close(); // To close the file.
}
void Simpletron::Execute(){
Simpletron mycomputer;
do{
instructionRegister = memory[instructionCounter];
mycomputer.DisplayMemory();
switch(operationCode){
case READ:
cout << "Enter an integer: " << endl;
cin >> memory[operand];
instructionCounter++;
break;
case WRITE:
cout << memory[operand] << endl;
instructionCounter++;
break;
case LOAD:
accumulator = memory[operand];
instructionCounter++;
break;
case STORE:
memory[operand] = accumulator;
instructionCounter++;
break;
case ADD:
accumulator += memory[operand];
instructionCounter++;
break;
case SUBTRACT:
accumulator -= memory[operand];
instructionCounter++;
break;
case MULTIPLY:
accumulator *= memory[operand];
instructionCounter++;
break;
case DIVIDE:
if (memory[operand] == 0){
cout << "*** Attempt to divide by zero ****" << endl;
cout << "*** Simpletron execution abnormally terminated ***" << endl;
exit(1);
break;
}
accumulator /= memory[operand];
instructionCounter++;
break;
case BRANCH:
instructionCounter = operand;
instructionCounter++;
break;
case BRANCHNEG:
if(accumulator < 0)
instructionCounter = operand;
instructionCounter++;
break;
case BRANCHZERO:
if(accumulator == 0)
instructionCounter = operand;
instructionCounter++;
break;
case HALT:
cout << "*** Simpletron execution terminated ***" << endl;
system("pause");
exit (0);
break;
default:
cout << "\nERROR: Invalid SML instruction -- Program terminating" << endl;
system("pause");
exit (1);
break;
}
}while (instructionRegister != HALT && instructionRegister != 1);
}