Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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.

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 +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$

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:

  1. Creates an object of the simplesim class.
  2. Calls load_program() for that object to read and load an SML program.
  3. 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.
  4. Calls dump() for the simplesim object to dump its registers and memory.

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

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago