EECS 211
Stream Exercises

Below are exercises suitable for submission to the Code Critic after reading Chapters 1 through 16 of the textbook. Don't forget the rules of the queue.

The exercises are:

Exercise: Simpletron

Especially Relevant Readings: Chapter 9, Chapter 10.6, Chapter 15, Section 16.1-16.4, Section 18.12, and the links given below.

Use TDD, pointers, dynamic memory allocation, string streams, and exceptions (WHEW!) to implement the Simpletron machine simulator (Exercises 8.18 and 8.19). That's a lot of concepts, but each one is valuable, fairly simple (at least as far used here), and makes the resulting solution much simpler.

Read Exercise 8.18 to see what SML looks like first.

Then read Exercise 8.19 to see how to implement the Simpletron Simulator. Specifically, you will use TDD to implement the class Simpletron with the following public API:

Follow the description given in the book for implementing the simulator, except:

After running a program, possible error codes are:

Don't worry about any other kind of error, e.g., arithmetic overflow. Use class constants to define the non-zero error codes. When an error is detected, the simulator should stop immediately, set the appropriate error code, and return. Use exceptions to do this cleanly and simply without cluttering up the rest of the code.

For example, the code fragment below makes a Simpletron simulator, runs it on the SML program in Figure 8.40, then checks that it stopped on the instruction 4300, the output was the sum of 12 and -8, and there was no error code:

istringstream in( "12 -8" );
ostringstream out;
Simpletron sml( in, out );
int program[] = { 1007, 1008, 2007, 3008, 2109, 1109, 4300, 0, 0, 0, -99999 };
sml.load( program );
CPPUNIT_ASSERT_EQUAL( string(" 4"), out.str( ) );
CPPUNIT_ASSERT_EQUAL( 0, sml.getErrorCode( ) );

-99999 marks the end of program input. -99999 is not stored in memory as part of the program.

The 0's in the program reserve space for data. The call to load() above should allocate 10 words of memory, 7 for the instructions, and 3 for the data. This memory should be freed up if another program is loaded, orif the simulator's destructor is called. Be sure to use C++'s memory allocation functions, not C's.

Use stringstreams to enable automated testing of the simulator. Our example code above:


Use TDD to develop this simulator. Here are some test programs, and the results your simulator should. I strongly recommend you work on these test programs one at a time. Write the test code for the first one, get it to work, before moving onto the next one. Do not write any code to handle bad instructions or addresses until you get to tests that require that.

TestSML CodeInputrun() returnsOutputError code
Halting 4300, -9999 empty 0 "" 0
Read and write 1 number 1003, 1103, 4300, 0, -99999 12 2 " 12" 0
Read and write 2 numbers 1005, 1006, 1105, 1106, 4300, 0, 0, -99999 12 -4 4 " 12 -4" 0
Read 2 numbers and write sum 1007, 1008, 2007, 3008, 2109, 1109, 4300, 0, 0, 0, -99999 12 -4 6 " 8" 0
Read 2 numbers and write larger 1009, 1010, 2009, 3110, 4107, 1109, 4300, 1110, 4300, 0, 0, -99999 12 4 6 " 12" 0
Test for illegal instruction 5009, -99999 empty 0 "" Simpletron::ILLEGAL_INSTRUCTION
Test for illegal address operand 1002, 4300, -99999 empty 0 "" Simpletron::ILLEGAL_ADDRESS

Now write some SML code of your own:

Finally, test creating two simulator instances with the same input and output streams. Run one simulator on the "add two numbers" code and the other on code with an illegal instruction. Verify that both simulators exit on the correct instruction, that the correct total output was printed, and the both still have the correct error code for their particular program, after both programs have run.

Submit to Code Critic:

Clearly label each section of code.

Wrap Up

When the above has been approved via the Code Critic, run make handin.

Email the Zip archive produced by your Makefile with the Subject EECS 211: Simpletron.

Comments? comment icon Contact the Prof!

Valid HTML 4.01 Transitional