Question
C++ This assignment is the first in a sequence of three. It is not strictly necessary to complete this one in order to do the
C++
This assignment is the first in a sequence of three. It is not strictly necessary to complete this one in order to do the other two, but the understanding you gain in completing this assignment will make writing the third assignment (a major project) much easier. You will need to complete the second in the sequence in order to do the third.
We start by "peeling open" a computer, look at its internal structure, and introducing machine language (assembler-level) programming. Your assignment is to write a program that simulates a computer, one that is capable of executing machine language programs.
1. Initial Setup
-
Log in to Unix.
-
Run the setup script for Assignment 4 by typing:
setup 4
2. Description of the Simplesim Computer
In this assignment you will write a program to simulate a fictional computer that we will call the Simplesim. As its name implies it is a simple machine. All information in the Simplesim is handled in terms of words. A word is a signed four-digit decimal (base 10) number such as +3364, -1293, +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.
4. 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.
5. 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 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 20 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 30 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 40 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 +7777 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$
One of the first things that your program will do is read the SML program into the Simplesim's memory. This is called loading the program. There are a couple of things that could go wrong when loading the program; the program may be too large for the Simplesim's 100-word memory or a line of the input file may not fit into a word (i.e., it may be greater than 9999 or less than -9999). In these situations your program should print an error message, dump the contents of the machine, and terminate. It should not start to run the SML program.
If there was a successful SML program load, your program should start to execute the SML program. SML programs, like any other programs, may perform an illegal operation and terminate abnormally (abend). There are a number of conditions that may cause an SML program to abend, in which case processing stops immediately An example of this is an attempt to divide by 0. In that case, the Simplesim should print an appropriate abend message, stop execution, and dump the contents of the machine. Every execution of your program (normal termination of the SML program or SML program abend) ends with a dump of the Simplesim.
A summary of the possible abend conditions (program load and execution errors) with their error messages appear in the following table. Note that all error messages must appear exactly as they appear in the table.
Condition | Error Message | Description |
---|---|---|
Program Load Errors: | ||
Program too big | *** ABEND: pgm load: pgm too large *** | The program is too big (more than 100 words) to fit into memory. |
Invalid word | *** ABEND: pgm load: invalid word *** | During program load, one of the words in the input file was less than -9999 or greater than 9999. |
Execution Errors: | ||
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. |
Overflow | *** ABEND: overflow *** | The result of an arithmetic operation is greater than 9999, and therefore would not fit into the accumulator. |
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. |
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started