Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3, +0007, -0001, 0000, etc. The Simplesim is equipped with memory and five registers. The Simplesim has a 100-word memory and these words are referenced

3, +0007, -0001, 0000, etc. The Simplesim is equipped with memory and five registers. The Simplesim has a 100-word memory and these words are referenced by their location numbers 00, 01, . . . , 99. Each word in the Simplesim's memory (always a single signed four-digit decimal number) may be interpreted as an instruction to be executed, a data value, or may be uninitialized. The first register is the accumulator, which is just large enough to hold a single word. Words from memory must be placed into the accumulator in order to perform arithmetic on them or test their values. All arithmetic and branching is done using the accumulator. The second register is the instruction counter, which is just large enough to hold a memory location (a two digit number, 00, 01, ... , 99). The instruction counter is used to hold the memory location of the next instruction to be executed. The third register is the instruction register, which, like the accumulator, is just large enough to hold a single word. The instruction register is used to hold a copy of the instruction (a word that was pulled out of memory) that is currently being executed. The fourth and fifth registers are the operation code and operand, respectively. Each one is just large enough to hold half of a word (a two digit decimal number). The operation code and operand registers are used to "split" the instruction register in half, with the 2 leftmost digits and sign of the instruction register going into the operation code and the 2 rightmost digits going into the operand. For example, if the instruction register had +1009, the operation code would have +10 and the operand would have 09. Likewise, if the instruction register had -1201, the operation code would have -12 and the operand would have 01. Input Your program will take as input an SML program followed by any input for that SML program. The input file will start with the SML program, one instruction per line. Following the last line of the SML program will be the number -99999, which is not part of the SML program. If the SML program expects any input (i.e., if it has any READ instructions) then input for the SML program, one input value per line, immediately follows the -99999 line. For example, below is the input file for the first program from the previous section. It adds -5 and 15. 1107 1108 2207 3108 2109 1209 4400 0000 0000 0000 -99999 -5 15 Note that each line of the input file, other than -99999 which is used to denote the end of program and not intended to be placed into the Simplesim's memory, fits into a single word. Note also that not all SML programs require input (those that do not have READ instructions). In that case there would be no data after the -99999 line. All input files to your program have -99999 after the last SML instruction. For those SML programs that do not require input, (those that do not have READ instructions), -99999 is simply the last line of the input file. Output Each time a READ instruction is executed your program must print the value that was read. For example, the two values read in the program from the previous section are -5 and 15. As each value is read, your program should print output that looks exactly like this. READ: -0005 READ: +0015 For each WRITE instruction your program must print the value of the word in that memory location. For example, from the program in the previous section, the sum 10 is printed exactly like this. +0010 When the HALT statement is executed, your program should print the following line: *** Simplesim execution terminated *** At the end of any execution your program should dump the entire contents of the Simplesim. This means dumping the contents of all five registers and all 100 words of the Simplesim's memory. Assuming that the name of your program is simplesim and the name of the SML program file above is sum.sml, then the output of your program must look exactly like this: z123456@turing:~/csci241/Assign4$ ./simplesim < sum.sml READ: -0005 READ: +0015 +0010 *** Simplesim execution terminated *** REGISTERS: accumulator: +0010 instruction_counter: 06 instruction_register: +4400 operation_code: 44 operand: 00 MEMORY: 0 1 2 3 4 5 6 7 8 9 0 +1107 +1108 +2207 +3108 +2109 +1209 +4400 -0005 +0015 +0010 10 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 20 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 30 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 40 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 +4444 50 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 60 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 70 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 80 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 90 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 z123456@turing:~/csci241/Assign4$ Files You Must Write You will need to write three files for this assignment: sml.h - This header file must contain all the #define statements that define the Simplesim's instruction set. i.e., #define READ 11, #define WRITE 12, etc. The header file should have an appropriate set of header guards to prevent it from being included multiple times in the same source file. simplesim.h - This header file should contain the class definition for a class called simplesim. This class definition should contain the private data members described below under 7. Simulating the Simplesim. It should also contain public declarations (i.e., prototypes) for the four member functions whose definitions are contained in simplesim.cpp (described below). The header file should have an appropriate set of header guards. simplesim.cpp - This source file will contain function definitions for the following four member functions of the simplesim class: simplesim::simplesim() This "default constructor" takes no arguments and has no return value. Its job is to perform the initialization process for a simplesim object described under 7.1. Initialize Simplesim. bool simplesim::load_program() This member function reads an SML program from standard input and attempts to load it into the Simplesim's memory, as described under 7.2. Load SML Program. It takes no arguments and returns a Boolean value indicating whether or not the program was successfully loaded (true if so, false if the load process abnormally terminated). void simplesim::execute_program() This member function executes an SML program in the Simplesim's memory, as described under 7.3. Execute SML Program. It takes no arguments and returns nothing. void simplesim::dump() const This member function dumps the contents of the Simplesim's registers and memory, as described under 7.4. Dump Simplesim. It takes no arguments and returns nothing. Since this method does not modify any any of the data of the simplesim object that calls it, it is declared to be const. Place the following two #include statements at the top of this file, following any other required #include statements that you have coded (, , etc.): #include "sml.h" #include "simplesim.h" This will ensure that the code that you write for your member functions will have access to the definition of the simplesim class and the #define statements that define the Simplesim instruction set. Note that there is no mention here of a main() function for the program. That function will be provided to you in a separate file (described below under 8. Files I Give You). It will be linked together with your code for the simplesim class during the build process initiated by the make command. The setup script will create the directory Assign4 under your csci241 directory. It will copy a makefile named makefile to the assignment directory. Unlike the makefiles for the previous three assignments, this makefile has only a single executable target named simplesim. You can build the entire project for Assignment 4 simply by typing the command make. The setup process will also copy a file named main.cpp to your assignment directory. This is a short driver program consisting of a main() function that does the following: Creates an object of the simplesim class. Calls load_program() for that object to read and load an SML program. If the call to load_program() returns true, it calls execute_program() for the object to execute the loaded SML program. Otherwise, it just skips to the next step. Calls dump() for the simplesim object to dump its registers and memory.SkeletonCode\\//#include #include #include "sml.h" #include "simplesim.h" using namespace std; //For loop up to 7777 simplesim::simplesim() { for (int i = 0; i < 100; i++) memory[i] = 7777; } //Load the SML program into the Simplesim's memory. This requires you to read the program, one line at a time, from stdin and stop when you encounter -99999 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 cout << "*** ABEND: pgm load: invalid word *** "; return false; } //Copy instruction into memory and increment count. //Invalid opcode *** ABEND: invalid opcode *** An attempt was made to execute an unrecognizable instruction, i.e., the leftmost two digits of the word was not a valid instruction. //Addressability *** ABEND: addressability error *** An attempt was made to fetch an instruction from an invalid memory location. //Division by 0 *** ABEND: attempted division by 0 *** Attempt to divide by 0. //Underflow *** ABEND: underflow *** The result of an arithmetic operation is less than -9999, and therefore would not fit into the accumulator. (make seperate function) //Overflow *** ABEND: overflow *** The result of an arithmetic operation is greater than 9999, and therefore would not fit into the accumulator. (make seperate function) } //Illegal input *** ABEND: illegal input *** During a READ instruction an attempt was made to read a value that was either less than -9999 or greater than 9999. return true; } //Invalid word bool simplesim::not_valid_word(int word) const { return (word < -9999 || word > 9999); } //Execute program void simplesim::execute_program() { bool done = false; instruction_counter = 0; while (!done) { //Instruction fetch if (count >= 100) //Test instruction counter instruction_register = memory[instruction_counter]; operation_code = instruction_register / 100; operand = instruction_register % 100; //Instruction execute switch (operation_code)//Operation code needs to be fetched { case READ: break; case WRITE: break; case LOAD: break; case STORE: break; case ADD: num1+num2; break; case SUBTRACT: num1-num2; break; case MULTIPLY: num1*num2; break; case DIVIDE: num1/num2; break; case BRANCH: break; case BRANCHEG: break; case BRANCHZERO: break; 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 FORMATTING HINTS IN VIDEO 2.4 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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

What's something that makes you envious?

Answered: 1 week ago