F
fb
Hi everyone. I have the following code. It was a question out of C++
how to program. I get a warning about "possible unreachable code" in
the RunCode() function, but I don't see any problem with it...
I was hoping for a better way of exiting the case statements below. I
used exit() for "divide by zero" and "invalid instruction". I can't get
a try/catch to work. Probably from a lack of experience. I have a
fairly strong C background if that help any.
Also, everything is very global. If you have any suggestions as to how
I might improve information hiding, feel free to point them out.
Thanks for your time...
//Simpletron Code Listing
#include <iostream>
#include <iomanip>
#include <cstdio> //For EXIT_SUCCESS and EXIT_FAILURE
//From iostream
using std::cout;
using std::endl;
using std::cin;
using std::ios;
//From iomanip
using std::setfill;
using std::setw;
using std::setiosflags;
using std::resetiosflags;
//Input/Output operations:
const int READ = 10;
const int WRITE = 11;
//Load and store operations:
const int LOAD = 20;
const int STORE = 21;
//Arithmetic operations:
const int ADD = 30;
const int SUBTRACT = 31;
const int DIVIDE = 32;
const int MULTIPLY = 33;
//Transfer-of-control operations:
const int BRANCH = 40;
const int BRANCHNEG = 41;
const int BRANCHZERO = 42;
const int HALT = 43;
//Generic Operations
const int NOP = 00;
//General size boundaries
const int MEMSIZE = 100;
//variables
int Accumulator = 0; //Accumulator Register
int counter = 0; //Counter to keep track of location in
memory that
//contains the instruction being performed
int OperationCode = 0; //Current operation being performed
int Operand = 0; //Memory location of opcode being acted on
int InstructionRegister = 0; //next instruction to be split and run
int Memory[MEMSIZE] = {0}; //My memory array -- zeroed out
void WelcomeHeader(void);
int ReadInput(void);
int RunCode(void);
int main(void)
{
WelcomeHeader();
ReadInput();
RunCode();
return EXIT_SUCCESS;
}
void WelcomeHeader(void)
{
cout << "*** Welcome to Simpletron ***\n\n"
<< "*** Please enter your program one instruction ***\n"
<< "*** (or data word) at a time. I will type the ***\n"
<< "*** location number and a question mark (?). ***\n"
<< "*** You then type the word for that location. ***\n"
<< "*** Type the sentinel -99999 to stop entering ***\n"
<< "*** your program. ***\n" << endl;
}
int ReadInput(void)
{
while(counter < 100)
{
if (counter < 10) //Formatting...Add a zero to make it line up
cout << "0";
cout << counter << " ? ";
cin >> Memory[counter];
if((Memory[counter] < 10000) && (Memory[counter] > -10000))
counter++;
else if(Memory[counter] == -99999){
Memory[counter] = 0;
break;
}
}
return EXIT_SUCCESS;
}
int RunCode(void)
{
counter = 0;
for(;
{
InstructionRegister = Memory[counter];
OperationCode = InstructionRegister / 100;
Operand = InstructionRegister % 100;
switch(OperationCode){
case READ:
cout << "Enter Integer: " << std::flush;
cin >> Memory[Operand];
counter++;
break;
case NOP:
counter++;
break;
case WRITE:
cout << Memory[Operand] << endl;
counter++;
break;
case LOAD:
Accumulator = Memory[Operand];
counter++;
break;
case STORE:
Memory[Operand] = Accumulator;
counter++;
break;
case ADD:
Accumulator += Memory[Operand];
counter++;
break;
case SUBTRACT:
Accumulator -= Memory[Operand];
counter++;
break;
case MULTIPLY:
Accumulator *= Memory[Operand];
counter++;
break;
case DIVIDE:
if (Memory[Operand] == 0){
cout << "Division by zero error. Program Terminated." << endl;
exit(1);
break;
}
Accumulator /= Memory[Operand];
counter++;
break;
case BRANCH: //Unconditional Jump (aka: goto)
counter = Operand;
counter++;
break;
case BRANCHNEG:
if(Accumulator < 0)
counter = Operand;
counter++;
break;
case BRANCHZERO:
if(Accumulator = 0)
counter = Operand;
counter++;
break;
case HALT:
cout << "*** Simpletron execution terminated ***" << endl;
return EXIT_SUCCESS;
break;
default:
cout << "ERROR: Invalid SML instruction -- Terminating" << endl;
exit(1);
break;
} //end switch
}//end for
return EXIT_SUCCESS; //Is this unreachable?
}//end RunCode
how to program. I get a warning about "possible unreachable code" in
the RunCode() function, but I don't see any problem with it...
I was hoping for a better way of exiting the case statements below. I
used exit() for "divide by zero" and "invalid instruction". I can't get
a try/catch to work. Probably from a lack of experience. I have a
fairly strong C background if that help any.
Also, everything is very global. If you have any suggestions as to how
I might improve information hiding, feel free to point them out.
Thanks for your time...
//Simpletron Code Listing
#include <iostream>
#include <iomanip>
#include <cstdio> //For EXIT_SUCCESS and EXIT_FAILURE
//From iostream
using std::cout;
using std::endl;
using std::cin;
using std::ios;
//From iomanip
using std::setfill;
using std::setw;
using std::setiosflags;
using std::resetiosflags;
//Input/Output operations:
const int READ = 10;
const int WRITE = 11;
//Load and store operations:
const int LOAD = 20;
const int STORE = 21;
//Arithmetic operations:
const int ADD = 30;
const int SUBTRACT = 31;
const int DIVIDE = 32;
const int MULTIPLY = 33;
//Transfer-of-control operations:
const int BRANCH = 40;
const int BRANCHNEG = 41;
const int BRANCHZERO = 42;
const int HALT = 43;
//Generic Operations
const int NOP = 00;
//General size boundaries
const int MEMSIZE = 100;
//variables
int Accumulator = 0; //Accumulator Register
int counter = 0; //Counter to keep track of location in
memory that
//contains the instruction being performed
int OperationCode = 0; //Current operation being performed
int Operand = 0; //Memory location of opcode being acted on
int InstructionRegister = 0; //next instruction to be split and run
int Memory[MEMSIZE] = {0}; //My memory array -- zeroed out
void WelcomeHeader(void);
int ReadInput(void);
int RunCode(void);
int main(void)
{
WelcomeHeader();
ReadInput();
RunCode();
return EXIT_SUCCESS;
}
void WelcomeHeader(void)
{
cout << "*** Welcome to Simpletron ***\n\n"
<< "*** Please enter your program one instruction ***\n"
<< "*** (or data word) at a time. I will type the ***\n"
<< "*** location number and a question mark (?). ***\n"
<< "*** You then type the word for that location. ***\n"
<< "*** Type the sentinel -99999 to stop entering ***\n"
<< "*** your program. ***\n" << endl;
}
int ReadInput(void)
{
while(counter < 100)
{
if (counter < 10) //Formatting...Add a zero to make it line up
cout << "0";
cout << counter << " ? ";
cin >> Memory[counter];
if((Memory[counter] < 10000) && (Memory[counter] > -10000))
counter++;
else if(Memory[counter] == -99999){
Memory[counter] = 0;
break;
}
}
return EXIT_SUCCESS;
}
int RunCode(void)
{
counter = 0;
for(;
InstructionRegister = Memory[counter];
OperationCode = InstructionRegister / 100;
Operand = InstructionRegister % 100;
switch(OperationCode){
case READ:
cout << "Enter Integer: " << std::flush;
cin >> Memory[Operand];
counter++;
break;
case NOP:
counter++;
break;
case WRITE:
cout << Memory[Operand] << endl;
counter++;
break;
case LOAD:
Accumulator = Memory[Operand];
counter++;
break;
case STORE:
Memory[Operand] = Accumulator;
counter++;
break;
case ADD:
Accumulator += Memory[Operand];
counter++;
break;
case SUBTRACT:
Accumulator -= Memory[Operand];
counter++;
break;
case MULTIPLY:
Accumulator *= Memory[Operand];
counter++;
break;
case DIVIDE:
if (Memory[Operand] == 0){
cout << "Division by zero error. Program Terminated." << endl;
exit(1);
break;
}
Accumulator /= Memory[Operand];
counter++;
break;
case BRANCH: //Unconditional Jump (aka: goto)
counter = Operand;
counter++;
break;
case BRANCHNEG:
if(Accumulator < 0)
counter = Operand;
counter++;
break;
case BRANCHZERO:
if(Accumulator = 0)
counter = Operand;
counter++;
break;
case HALT:
cout << "*** Simpletron execution terminated ***" << endl;
return EXIT_SUCCESS;
break;
default:
cout << "ERROR: Invalid SML instruction -- Terminating" << endl;
exit(1);
break;
} //end switch
}//end for
return EXIT_SUCCESS; //Is this unreachable?
}//end RunCode