Homework 5

Hypothetical Computer Implementation

Modified

Overview

As discussed in class, a hypothetical computer architecture can be implemented in software to model the architecture and interpret each hypothetical computer machine instruction. The purpose of this exercise is to gain understanding of how simple interpreters work and some experience using Java as the implementation language for the interpreter.

Discussion

The interpreter program has two main parts, 1) a loader for both data values and algorithm machine code, and 2) a machine language instruction interpreter. Recall that all details of the hypothetical machine architecture are to be simulated in software including the organization of memory into areas separate for data and algorithm storage.

Load - The loader must store the initial data (everything up to the first +9999999) into the data area. Then store machine instructions (everything up to the next +9999999) into the algorithm area. After both the data and instructions have been loaded execution of the instructions can start by simulating the fetch/execute cycle of the hypothetical machine.

The structure of a machine language program is:

+0000000 // (00)   Constant 0     Initial data
+0000021 // (01)   x              
+0000054 // (02)   y
-0000019 // (03)   z
+0000005 // (04)   Constant 5
+9999999                          
+1010203 // (00)   z = x + y
-8030000 // (01)   Print z
+8000001 // (02)   Read x
+8000002 // (03)   Read y
-2010401 // (04)   x = x / 5
+9000000 // (05)   Stop
+9999999 //                        Data Input
+0000012 //         x              by READ
-0000123 //         y              execution

Interpret - After the program data and algorithm (machine instructions) are loaded into the Hypothetical Machines memory, the program can be executed.

Execution is performed by fetching the next machine instruction of the algorithm and executing. This process is commonly called interpreting the program when performed by software rather than directly by hardware. This is explained in the notes but briefly the steps required are:

  1. IP = 0
  2. Fetch
  3. Decode
  4. Execute
  5. Go to 2.

Base Interpreter.java

An incomplete interpreter is listed below. 

//   	Hypothetical Machine Interpreter

public class Interpreter {

  static float Data[] = new float[100];
  static int Algorithm[] = new int[100];

  public static void main(String args[]) throws Exception
  {
        Load();
        Interpret();
  }

  static void Interpret()
  {
    int IP;
    boolean TraceOn = false;
    int  Instruction;
    int   Op;
    int   Opn1;
    int   Opn2;
    int   Dest;

     System.out.print("\tIP\tOp\tOpr1\tOpr2\tDest\n");
     IP = 0;
     while (true) {
// Fetch
        Instruction = Algorithm[IP];
// Decode
        Op = Instruction / 1000000;
        Dest = Math.abs(Instruction % 100);
         Opn2 = 0;
         Opn1 = 0;
        
        if (TraceOn) 
             System.out.println("\t"+IP+"\t"+Op+"\t"+Opn1+"\t"+Opn2+"\t"+Dest);

// Update IP
        IP = IP + 1;
// Execute
        switch (Op) {
             case 0  : Data[Dest] = Data[Opn1]; break;
             case 1  : Data[Dest] = Data[Opn1] + Data[Opn2]; break;
             case 2  : Data[Dest] = Data[Opn1] * Data[Opn2]; break;
             case 3  : Data[Dest] = Data[Opn1] * Data[Opn1]; break;
             case 4  : if (Data[Opn1] == Data[Opn2]) IP = Dest; break;
             case 5  : if (Data[Opn1] >= Data[Opn2]) IP = Dest; break;
             case 6  : Data[Dest] = Data[Opn1 + (int)Data[Opn2]]; break;
             case 7  : Data[Opn1] = Data[Opn1] + 1;
                          if (Data[Opn1] < Data[Opn2]) IP = Dest; break;
             case 8  : Data[Dest] = C311.readFloat(); break;
             case 9  : return;
	   case -1 :
	   case -2 :
	   case -3 :
	   case -4 :
	   case -5 :
	   case -6 :
	   case -7 :
	   case -8 :
             case -9 : TraceOn = !TraceOn;
       }
     }
  }

static void Load()
  {
        int DataMemoryLocation;
        float DataValue;

        DataMemoryLocation = 0;
        while ((DataValue = C311.readFloat()) != 9999999.0){
                Data[ DataMemoryLocation ] = DataValue;
                DataMemoryLocation = DataMemoryLocation + 1;
        }

        int AlgorithmMemoryLocation;
        int Instruction;

        AlgorithmMemoryLocation = 0;
        while ((Instruction = C311.readInt()) != 9999999){
                Algorithm[ AlgorithmMemoryLocation ] = Instruction;
                AlgorithmMemoryLocation = AlgorithmMemoryLocation + 1;
        }
  }
}

Test Sort Program

+0000000 (00)   Constant 00
+0000000 (01)   i
+0000000 (02)   j
+0000000 (03)   min
+0000000 (04)   n
+0000000 (05)   temp1
+0000000 (06)   temp2
+0000000 (07)   A
+9999999        End of Initial Data
-9000000 (00)                               (-9000000 turns on trace )
+8000004 (01) Read n
+8000005 (02)   Read temp1
-6050701 (03)   A[i] = temp1
+7010402 (04)   increment i, if i<n goto 2
+0000001 (05)   i=0
+0010003 (06)   min=i
+0010002 (07)        j=i
+6070205 (08)        temp1=A[j]
+6070306 (09)        temp2=A[min]
-5060512 (10)        if A[min] < A[j] goto 12
+0020003 (11)            min=j
+7020408 (12)        increment j, if j<n goto 8
+6070305 (13)   temp1=A[min]
+6070106 (14)   temp2=A[i]
-6050701 (15)   A[i]=temp1
-6060703 (16)   A[min]=temp2
+7010406 (17)   increment i, if i<n goto 6
+0000001 (18)   i=0
+6070105 (19)   temp1=A[i]
-8050000 (20)   print temp1
+7010419 (21)   increment i, if i<n goto 19
+9000000 (22) Stop
+9999999      End of program
+0000005      n
+0000028      Data Values to sort
+0000020
+0000021
+0000025
+0000024
 

Assignment

  1. Write a pseudocode machine language program to input two numbers and print the largest. Turn on program execution tracing.
     
  2. Complete the interpreter for those sections in bold in the base interpreter listing. A compiled, completed Interpreter program can be downloaded below for comparison with your interpreter. Verify that your interpreter produces the same results for the above sort program.
     
  3. After verifying your interpreter correctly executes the sort program, create two new instructions:
     
    1. an exchange (XCHG) operation to exchange two variables of the same array. For example:

      A[min] <-> A[i]

      exchanges the min and i variables of array A.
       

    2. a no operation (NOP) instruction that does nothing when executed.

    Note the two new instructions exceed the number of unused codes. Consider replacing some operation not used by the sort (e.g. sqrt).
     

  4. Modify the sort program above to use your new exchange operation in place of:
    +6070305 (13)   temp1=A[min]
    +6070106 (14)   temp2=A[i]
    -6050701 (15)   A[i]=temp1
    -6060703 (16)   A[min]=temp2

    Your exchange instruction should only occupy one of the four instruction words. Replace the remaining three words with your no operation.

Turn In

  1. Cover sheet with your name, date, and Homework 5.
     
  2. Listing of pseudocode machine language program to input and print the largest of two real numbers.
     
  3. Traced execution of pseudocode program. The simplest method is to redirect the program execution output by:
    1. java -cp .  Interpreter < pseudo.pgm > output

    then print the file named output.
     

  4. Listing of your Interpreter program with your exchange instruction implementation. 
     
  5. Traced execution of your interpreter executing the pseudocode sort program with your exchange instruction. The simplest method is to redirect the program execution output by:
    1. java -cp .  Interpreter < sort.pgm > output

    then print the file named output.

 

Files

File links
Sort program sort.pgm
Class for int and float C311.class
Completed Interpreter for verification Interpreter.class
Starter Interpreter for your assignment Copy Interpreter.java above.

Getting Started

 Compiling and executing at IUS

  1. Download or create the files listed above. It is necessary that C311.class be in the same directory.
  2. To compile a Java program named Interpreter.java enter:

      javac -classpath . Interpreter.java
       

  3. To execute the compiled program, with C311.class in the same directory, using the pseudocode program sort.pgm as input:
    1. java -cp . Interpreter < sort.pgm

Compiling and executing at Home - Java is freely available from the SUN Java website. Download the software and follow the instructions for installing the Java SDK. With the SDK installed, the Interpreter can be executed by (note that Java is case-sensitive everywhere, including the DOS command prompt line):

  1. Install Java SDK.
  2. Download files listed above. It is necessary that C311.class be in the same directory.
  3. To compile a Java program named Interpreter.java enter:
    1. javac -classpath . Interpreter.java

  4. To execute the compiled program, with C311.class in the same directory, using the pseudocode program sort.pgm as input:
    1. java -cp . Interpreter < sort.pgm

FAQ

  1. Stop a run-away Java execution!?! When printing at the command prompt press Ctrl C simultaneously. If nothing is being printed, Ctrl Alt Del and use Task Manager to end the java process.
     
  2. Why are all the operands negative? The operation should be negative but the operands positive. Take the absolute value of x in Java by:
    1. x = Math.abs(x);
       

  3. How to do output in Java? Java uses System.out.print( ) rather than C++ cout. All standard types can be output. Generally only one data type can be printed at a time. To output Hello World:
    1. System.out.print("Hello World");

    in C++ it would be:

      cout << "Hello World";

    The simplest is to use:

      System.out.print( expr );

    where expr is a string, int, float, etc. expression. To output several expressions in one output statement use concatenation (i.e. the + operator), for example to output the value of IP and op variables seperated by a tab and terminated by a new line use:

      System.out.print(IP + "\t" + op + "\n");
       

  4. Why no output appears? No output is displayed until a Carriage return/Line feed is printed. This can be done by:
    1. System.out.println();

    or by outputting a "\n".


Document last modified: