SHARE
TWEET

Untitled

a guest Nov 14th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package interpreter;
  2. //----------------------------------------------------------------------
  3. /*
  4.   file  : Interpreter.java
  5.   date  : 10/24
  6.   author: Robby Manis
  7.   description: Assembler interpreter
  8. */
  9. //----------------------------------------------------------------------
  10.  
  11. public class Interpreter {
  12.  
  13.     static int  PC;          //program counter holds address of next instr
  14.     static int  AC; //the accumulator - a register for doing arithmetic
  15.     static int r0; //register 0
  16.     static int r1; //register 1
  17.     static int r2; //register 2
  18.     static int r3; //register 3
  19.     static int r4; //register 4
  20.     static boolean    run_bit = true;  //a bit that can be turned off to halt the machine
  21.     static int  instr;       //a holding register for the current instruction
  22.     static int  instr_type;  //the instruction type (opcode)
  23.     static int  data_loc;    //the address of the data, or -1 if none
  24.     static int  data;        //holds the current operand
  25.  
  26.     static boolean DEBUG = true;
  27.  
  28.     static final int CLR = 1;
  29.     static final int ADDI = 2;
  30.     static final int ADDM = 3;
  31.     static final int HALT = 4;
  32.     static final int LDI = 5;//load the AC with the value x
  33.     static final int LDM = 6;//load the AC with the value in memory location y
  34.     static final int ST = 7;//store the value in the AC  in memory location y
  35.    
  36.            
  37.  
  38.     //------------------------------------------------------------------
  39.     //This procedure interprets programs for a simple machine.  The machine
  40.     //has a register AC (accumulator), used for arithmetic.  The interpreter
  41.     //keeps running until the run bit is turned off by the HALT instruction.
  42.     //The state of a process running on this machine consists of the memory,
  43.     //the program counter, the run bit, and the AC.  The input parameters
  44.     //consist of the memory image and the starting address.
  45.     public static void interpret ( int memory[], int starting_address ) {
  46.         PC = starting_address;
  47.         run_bit = true;
  48.         while (run_bit) {
  49.             instr = memory[PC];  //fetch next instruction into instr
  50.             PC = PC + 1;  //increment program counter
  51.             instr_type = get_instr_type( instr );  //determine instruction type
  52.             data_loc = find_data( instr, instr_type, memory );  //locate data (-1 if none)
  53.             if (data_loc >= 0)  {  //if data_loc is -1, there is no operand
  54.                 data = memory[data_loc];  //fetch the data
  55.             }
  56.             execute( instr_type, data, memory);  //execute instruction
  57.             if (DEBUG) System.out.println("AC: " + AC + " PC: " + PC); //print accumulator value if in debug mode
  58.         }
  59.     }
  60.     //------------------------------------------------------------------
  61.     //since our instruction set is so simple, we'll let the opcode and
  62.     // instruction type be the same.
  63.     private static int get_instr_type ( int opcode ) { return opcode; }
  64.     //------------------------------------------------------------------
  65.     private static int find_data ( int opcode, int type, int memory[] ) {
  66.         switch (opcode) {
  67.             case ST:
  68.             case LDI:
  69.             case ADDI:
  70.                 return PC++;
  71.             case ADDM:
  72.             case LDM:
  73.                 return memory[PC++];
  74.             case CLR:
  75.             case HALT:
  76.                 PC++;
  77.             default:
  78.                 return -1;
  79.         }
  80.     }
  81.     //------------------------------------------------------------------
  82.     private static void execute ( int type, int data, int memory[] ) {
  83.         switch (type) {
  84.             case ST:
  85.                 memory[data]=AC;
  86.                 break;
  87.             case LDM:
  88.             case LDI:
  89.                 AC = data;
  90.                 break;
  91.             case ADDI:
  92.             case ADDM:
  93.                 AC += data;
  94.                 break;
  95.             case CLR:
  96.                 AC = 0;
  97.                 break;
  98.             case HALT:
  99.                 run_bit = false;
  100.                 break;
  101.             default:
  102.         }
  103.     }
  104.  
  105.     public static void main(String[] args) {
  106.         System.out.println("Test 1");
  107.         int  m2[] = {  9,
  108.                 -5,
  109.                 CLR,    0,
  110.                 ADDI,  17,
  111.                 ADDI,   2,
  112.                 ST,     0,
  113.                 ADDM,   0,
  114.                 ADDM,   1,
  115.                 CLR,    0,
  116.                 HALT,   0
  117.   };
  118.   interpret( m2, 2 );
  119.  
  120.         System.out.println("\nTest 2");
  121.         int  m3[] = {  2,
  122.                 CLR,        0,
  123.                 ADDI,   2,
  124.                 ADDM,   0,
  125.                 CLR,        0,
  126.                 HALT,   0
  127.         };
  128.         interpret( m3, 1 );
  129.  
  130.  
  131.         System.out.println("\nTest 3");
  132.         int  m4[] = {  2,
  133.                 -5,
  134.                 CLR,        0,
  135.                 ADDM,   6,
  136.                 ST,         5,
  137.                 LDM,        1,
  138.                 CLR,        0,
  139.                 HALT    ,   0
  140.         };
  141.         interpret( m4, 2 );
  142.  
  143.     }
  144. }
RAW Paste Data
Top