Hey there, I'm having trouble getting my program to read the last instruction of the file, and so far, I haven't been able to figure out why. Any help would be appreciated.

/*  The Simpletron Machine Language Program.
	The purpose of this program is to simulate a primitive computer. 
	This program written in C++, a high-level language, will read and 
	execute the program instructions written in machine language from 
	an input file. Then, the program should display the registers and 
	the simulated computer memory.
*/

/*	The functions used in the program are:
		Simpletron();
		void Instructions();
		void DisplayMemory();
		void LoadMemory();
		void Execute();
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

// Input/Output operations
const int READ = 10; // Reads a variable and places it into a specific memory location.
const int WRITE = 11; // Writes a variable from a specific memory location into the terminal.

// Load/Store operations
const int LOAD = 20; // Loads a variable from a specific memory location into the accumulator.
const int STORE = 21; // Stores a variable from the accumulator into a specific memory location.

// Arithmetic operations
const int ADD = 30; // Adds a variable from a specific memory location to a variable in the accumulator.
const int SUBTRACT = 31; // Substracts a variable from a specific memory location from a variable in the accumulator.
const int DIVIDE = 32; // Divides a variable from a specific memory location into a variable in the accumulator.
const int MULTIPLY = 33; // Multiplies a variable from a specific memory location by a variable in the accumulator.

// Transfer of control operations
const int BRANCH = 40; // Branches to a specific memory location.
const int BRANCHNEG = 41; // Branches to a specific memory location if the accumulator is negative.
const int BRANCHZERO = 42; // Branches to a specific memory location if the accumulator is zero.
const int HALT = 43; // Indicates that the program terminated. 

# define Msize 100

// Class Simpletron that holds data and operations.
class Simpletron{
	// Data members
	private:
		int accumulator; // Register that holds the results of the operations or data transfers.
		int instructionCounter; // Records the number of instructions being performed. 
		int operationCode; // Indicates the operation that is being perfomed.
		int operand; // Indicates the memory location of the current instruction operating.
		int instructionRegister; // Executes the instructions to be performed from the memory.
		int size;
		int memory[Msize]; // Array in which the program is going to be loaded.
		int Error;
	// Function members
	public:
		Simpletron(); // Inline function: default constructor.
		void LoadMemory(); // Function that loads the program from the memory.
		void DisplayMemory(); // Function that displays the program.
		void Execute(); // Function that executes the program.	
};

// Definition of the prototype of the Instructions function.
void Instructions(void);

// Default constructor.
Simpletron::Simpletron(){
// Initializes all private members of Simpletron class.
	accumulator = 0;
	instructionCounter = 0;
	instructionRegister = 0;
	operationCode = 0;
	operand = 0;
	size = 0;
	Error = 0;
}

// main function starts.
int main(){
	Simpletron mycomputer; // Defining an object for the class Simpletron.

	Instructions(); // Displays a set of instructions.
	mycomputer.LoadMemory(); //	Loads each instruction from a file.
	mycomputer.Execute(); // Executes each instruction in the program.
	
	return 0;
} // End of the main function.


// Functions

// Instructions function
void Instructions(){
// Precondition/Postcondition: Displays the following message or 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;
}

// DisplayMemory function
void Simpletron::DisplayMemory(){
// Precondition: This function receives the following variables.
	ifstream MySimpletron; // To read the file named MySimpletron.
	string file; // To store the name of the file.
	
	system("cls") ; // To clear the screen after each instruction has been executed.

	// The registers are displayed.
	cout << "\n REGISTERS:" << endl;
	cout << " accumulator         : " << setfill('0') << right << setw(4) << accumulator << endl;
    cout << " instruction Counter : " << setfill('0') << right << setw(2) << instructionCounter << endl;
    cout << " instruction Register: " << setfill('0') << right << setw(4) << instructionRegister << endl;
    cout << " operation code      : " << setfill('0') << right << setw(2) << operationCode << endl;
    cout << " operand             : " << setfill('0') << right << setw(2) << operand << endl;

	// The memory is displayed.
	cout << "\n MEMORY: " << endl; 
	cout << setfill(' ') << setw(6) << ""  << setw(3) << "0" << setw(6) << "1" << setw(6) << "2" 
						 << setw(6) << "3" << setw(6) << "4" << setw(6) << "5" << setw(6) << "6" 
						 << setw(6) << "7" << setw(6) << "8" << setw(6) << "9" << endl;

		for (int w = 0; w < Msize; w+=10){
			cout << w << "   ";
			for(int i = w; i < w + 10; i++){
				if (i < size)
					cout << " " << setfill('0') << setw(4) << memory[i] << " ";
				else if (i >= size)
					cout << "0000  ";
			}
			cout << endl;
		}
}

// LoadMemory function
void Simpletron::LoadMemory(){
// Precondition: This function receives the following variables.
	ifstream MySimpletron; // To read the file named MySimpletron.
	string file; // To store the name of the file.
    
//Postcondition: This function loads the instructions from the file into the program.
	cout << "\nEnter the name of the file: " << endl; // This indicates that it is ready for input.
    cin >> file; // To input the name of the file.
 
	do{
		MySimpletron.open(file.c_str(),ifstream::in); // To open the file. 
         
		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;
			exit(1);
		}   
	    
		cout << "\nThe instructions contained in the file " << file << " are the following: " << endl;
         
		size = 0; // To clean the array when it opens the file again.

		MySimpletron >> instructionRegister;
		
			while (!MySimpletron.eof()){ // It keeps reading the file until it ends.
				memory[size] = instructionRegister;
				MySimpletron >> instructionRegister ;
				cout << setfill('0') << setw(4) << memory[size] << "    ";
				size++;
			}
			
     
		cout << "\nThe information in the file has been read." << endl; 

		cout << "\n*** Program loading completed ***" << endl;
		cout << "*** Program execution begins  ***" << endl;

               
		MySimpletron.close(); // To close the file.
	}while(!MySimpletron);

	system ("pause");

}

// Execute function
void Simpletron::Execute(){
/*Postcondition: The twelve different SML instructions are simulated. 
The name and the contents of each register, and also the contents of the memory, are displayed. */
	do{
		DisplayMemory(); // Calls the DisplayMemory function to displays each instruction in the program.
		instructionRegister = memory[instructionCounter]; // Used to fetch the contents from the memory array.
		operationCode = instructionRegister/100; // To extract the operation code from the instruction register.
		operand = instructionRegister%100; // To extract the operand from the instruction register.
		
		switch(operationCode){ // The different SML instructions are simulated among the twelve operations.
			case READ: // Input operation.
				cout << "\nEnter an integer: " << endl; // To indicate that the program is ready for input.
				cin >> memory[operand]; // The input is read  into location.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case WRITE: // Output operation.
				cout << "Result: " << endl << memory[operand] << endl; // Write instruction.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case LOAD: // Load operation.
				accumulator = memory[operand]; // Load instruction.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case STORE: // Store operation.
				memory[operand] = accumulator; // Store instruction.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case ADD: // Add operation.
				accumulator += memory[operand]; // Addition instruction.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case SUBTRACT: // Subtract operation.
				accumulator -= memory[operand]; // Subtraction instruction.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case MULTIPLY: // Multiply operation.
				accumulator *= memory[operand]; // Multiplication instruction.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case DIVIDE: // Divide operation.
				if (memory[operand] == 0){ // If there is an attempt to divide by zero.
					cout << "*** Attempt to divide by zero ****" << endl; // Displays this message.
					cout << "*** Simpletron execution abnormally terminated ***" << endl; // Displays this message.
					exit(1); // The program is abnormally terminated.
					break;
				}
				accumulator /= memory[operand]; // Division instruction.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case BRANCH: // Branch operation.
				instructionCounter = operand; // Unconditional branch instruction.
				instructionCounter++; // To increment the instruction counter register.
			break;
			case BRANCHNEG: // Branchneg operation.
				if(accumulator < 0)
					instructionCounter = operand; // Conditional branch instruction (if accumulator is negative).
				instructionCounter++; // To increment the instruction counter register.
			break;
			case BRANCHZERO: // Branchzero operation.
				if(accumulator == 0)
					instructionCounter = operand; // Conditional branch instruction (if accumulator is zero).
				instructionCounter++; // To increment the instruction counter register.
			break;
			case HALT: // Halt operation.
				cout << "\n*** Simpletron execution terminated ***" << endl; // Displays that the program has terminated.
				exit (0); // Program terminated.
			break;
			default: // When an invalid instruction is read. 
				cout << "\nERROR: Invalid SML instruction -- Program terminating" << endl; //Displays an error message.
				Error = 1;
				exit (1); // Program terminated because of an invalid instruction.
			break;
		}
		system ("pause");
	}while (operationCode != HALT && Error != 1);

}

Recommended Answers

All 5 Replies

Delete that do loop on line 152. Why would you want to continuously read the same file over, and over, and over forever???


change the loop starting on line 64 to this:

while( MySimpletron >> instructionRegister )
{
    memory[size] = instructionRegister;
    cout << setfill('0') << setw(4) << memory[size] << "    ";
    ++size;
}

It worked, thanks, but it now says that the file doesnt exist :/

Post the changes you made.

Okay, it works good now. Thanks again.

// LoadMemory function
void Simpletron::LoadMemory(){
// Precondition: This function receives the following variables.
	ifstream MySimpletron; // To read the file named MySimpletron.
	string file; // To store the name of the file.
    
//Postcondition: This function loads the instructions from the file into the program.
	cout << "\nEnter the name of the file: " << endl; // This indicates that it is ready for input.
    cin >> file; // To input the name of the file.
 
	
		MySimpletron.open(file.c_str(),ifstream::in); // To open the file. 
         
		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;
			exit(1);
		}   
		
		cout << "\nThe instructions contained in the file " << file << " are the following: " << endl;
         
		size = 0; // To clean the array when it opens the file again.
		
			while (MySimpletron >> instructionRegister){ // It keeps reading the file until it ends.
				memory[size] = instructionRegister;
				cout << setfill('0') << setw(4) << memory[size] << "    ";
				size++;
			}
			
     
		cout << "\nThe information in the file has been read." << endl; 

		cout << "\n*** Program loading completed ***" << endl;
		cout << "*** Program execution begins  ***" << endl;
               
		MySimpletron.close(); // To close the file.
	
	system ("pause");

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.