CS 1030 Spring 2008,    Homework 11       Due:  Thursday April 17
A paper copy of your program should be turned in at the beginning of class.
Your work should be dropped off electronically in the Lamont folders by 10:00 AM on the due date.
Late Deadline:  turn in by Midnight Friday April 18 for a 10% penalty.  (Paper on my office door.)
For further details on how to hand in assignments, see these Submission Guidelines.
 

The IBM 1030 is a primitive computer that uses the programming language C--.  In C--,
the only data type is int and the only variable names are memory addresses (0..19).
There are 7 instructions that the computer understands.

D <addr> <val>   D(for Data) puts <val> in the memory cell at address <addr>
I <addr>         I(for Input) prompts the user for a value that is put at address
                 <addr>
O <addr>         O(for Output) displays the value at address <addr>
<op> <addr1> <addr2>    <op> is +, -, /, or *
                                 These 4 instructions take 2 numbers from the given addresses,
                 do the indicated operation, and put the result back at the first
                 address.  + <addr1> <addr2>  is comparable to <addr1> += <addr2>.

You will write a program that
     - prompts the user to type in a C-- program
     - displays the C-- program
     - executes the C-- program

Here is an example run:

How many instructions to load? 6
Enter your program one instruction per line.
I 0
I 1
D 10 2
+ 0 1
/ 0 10
O 0
Program Listing
I 0
I 1
D 10 2
+ 0 1
/ 0 10
O 0
Execution Begins ...
? 13
? 17
15
Execution Done
Press any key to continue

The C-- program shown above asks the user to enter 2 numbers.  The average is calculated and displayed.
Since C-- has no strings, fancy input prompts and output labels are not possible.

Use the following design for your program:

Create a class that acts as the memory of the IBM 1030.  The memory should provide the following functions:
    - a method that allows a value to be written to a location in the memory (write)
    - a method that allows a value to be read from a location in memory (read)
    If either of these methods is given a bad address (too big/small), a message should be displayed to note the problem.

Create a class for C-- instructions.  A single C-- instruction has a code (the initial character, e.g. +) and one or two
integer operands which are either addresses or values.  The following member functions should be available:
     - a method that reads in a single C-- instruction using cin (no prompt is needed)
              (You will have to examine the code to see how many operands will be typed in after it.)
     - a method that displays a single C-- instruction
     - 3 methods that get the code, first operand, and second operand of the instruction (very simple functions!)

Create a class for the IBM 1030 computer.  All such computers have a memory and a program (an array of instructions).
Provide the following member functions:
     - a method that reads in a program (First ask the user how many instructions there will be. Then read in that many.
             Be sure to let each instruction object read in the user's typing.
     - a method to display the program (Let the instruction objects display themselves one at a time.)
     - a method to execute the program.  (Go through the array of instructions one time from start to finish.
             For each instruction, look at the code and decide what to do.  This will involve looking at the
             operand(s) of the instruction and you will also need to use the memory object.

In main, you will create a computer object and use it to
     - read in a C-- program (typed by the user)
     - display the program
     - execute the program

As in the lab, all data members must be private and all your member functions will be public.
Use no global variables.  You will not need to write any constructors for this assignment which
can be solved in about 125 lines of code.  An executable version of the solution is here.

How to execute a IBM1030 instruction (for example  + 3 4):
           Look at the instruction code.  What you do next depends on which code is used.
           A + code indicates that two operands will be used, both addresses.
           Go to the memory and fetch the values at those two addresses.
           Add the two values to get a result.
           Put the result back into memory (where?  at the address given by the first operand.)