Assignment 4: Assignment for Topic 4, Exceptions & Threads


The goal of this assignment is to give you practice with the use of exceptions and threads.  You will extend the MML interpreter you implemented in the last assignment with two new capabilities - the ability to halt and report error messages and the ability to run multiple programs simultaneously.

Incorporating Exceptions

When writing a program there are many mistakes that a user can make that cause the program to be unrunnable.  For example, the user can cause a divide by zero to occur or the user can write a bnz instruction to a label that does not exist.  The rational reporting of errors would be a useful capability for your interpreter to have and exceptions are the right Java mechanism to implement it.

What you must do:

  1. Download the exception class file MMLException.java.
  2. Write exception classes that extend MMLException to handle divide by zero and illegal branch address.
  3. Modify the div and bnz instruction classes from the last assignment to throw these exceptions when appropriate.
  4. Modify the machine to catch these exceptions when they occur.  When an exception is caught the machine should halt and report an error message that includes the label of the offending instruction as well as the offending register (for divide) or label (for branch-non-zero).

Incorporating Threads

At present the machine is capable of running only a single thread of execution.  It would be useful to be able to run two separate threads of execution simultaneously sharing the same set of registers.  In this part of the assignment you will use Java's built-in thread capabilities to implement this capability.

What you must do:

Modify Machine.java (and whatever other files you need to) so that multiple threads of execution (multiple machines) can be executing different programs simultaneously on the same register set.

Things you must consider:

  1. The two programs must be running in parallel (not serially) so you have to use java threads - you cannot simply create two machines M1 and M2, execute M1 and then execute M2.  (You may, however, load the programs into the machines serially.)
  2. Use a command line argument to specify how many program(s) are to be loaded.  You will need to cast the string input parameter to a integer.
  3. Use Java's synchronize primitives to make sure that the individual instructions are atomic operations.

Here are producer and consumer programs to test your modifications on.  If implemented correctly the consumer should output 100, 200, 400 in that order.

A solution set for this assignment is now available.


Last Modified:  Tuesday, October 02, 2001 12:58:05 PM