CS 1120 Fall 2019 LA 3 Instruction Execution Simulator LA3 Instruction Execution Simulator _____________________________________________________________________________________________ Due Date Friday...

It's my java programing homework.


CS 1120 Fall 2019 LA 3 Instruction Execution Simulator LA3 Instruction Execution Simulator _____________________________________________________________________________________________ Due Date Friday 10/18/19 @ 11:59pm Objectives  Review Inheritance  Review Polymorphism Background Any high level program language will finally be compiled into machine-readable instructions and executed on a machine with a CPU and memory. In this assignment you are asked to implement a Java application to simulate the execution of a list of instructions on a CPU and on locations in memory. The components of the application are as follows:  CPU - CPU has two registers: “ax” and “bx” for arithmetic operations. Each register can store an integer.  Memory - Each unit in memory can store an integer. The memory can store 10 integers. The integers are stored in an array. The address of each memory unit is its index in the array.  Instruction - This is the superclass (an abstract class) of the different types of instruction classes. Each instruction contains one operation code (opcode) followed by two arguments. The opcode specifies the type of the instruction which will be executed on the values stored in the locations indicated by the two arguments. There are 4 types of instructions in total:  Load Instruction The execution of a Load Instruction will load an integer from the memory to the register. The first argument (after the opcode) specifies the register name (ax or bx). The second argument is the memory address. For example, “load,ax,1” means load the integer from memory address 1 to the “ax” register.  Store Instruction The execution of a Store Instruction will store the integer value in a register to memory. The first argument specifies the register name. The second argument is the memory address at which to store the register value. For example, “store,ax,3” means store the value obtained from the “ax” register in memory address 3.  Add Instruction The execution of an Add Instruction will perform an add operations on two integers. The first argument is the register name in which the first integer is stored. The second argument can be either a register name or an integer which indicates the memory address of the second integer. The result of the add operation will be stored at the register specified by the first argument. For CS 1120 Fall 2019 LA 3 Instruction Execution Simulator example, “add,ax,1” will store the value “ax+1” in the ax register and “add,ax,bx” will store the value “ax+bx” in the ax register.  Sub Instruction (for subtraction) The execution of a Sub Instruction will perform a subtraction operation on two integers. The first argument is the register name in which the first integer is stored. The second operand can be either a register name or an integer which indicates the source of the second integer. The result of the subtraction operation will be stored at the register specified by the first argument. For example, “sub,ax,1” will store the value “ax-1” in the ax register and “sub,ax,bx” will store the value “ax-bx” in the ax register. Problem Specification Your application should load instructions from the input file provided (named “instructions.txt”. Initially, the value of the CPU registers are zero, and each location in the memory unit (which is represented by a one-dimensional array) is set to its index (i.e. memory[0] = 0; memory[1] = 1; etc. . Your program should execute all the instructions in the input file and then print out the status of the memory and CPU (whose values may be updated as the program executes). Each instruction has the following format: opcode,arg1,arg2 The first line in the input file (instructions.txt) is an integer specifying the number of instructions in the file. The three components of an instruction are separated by commas. Design Requirements Your program structure should follow the UML diagram below. Here is a list classes you need to write:  CPU CS 1120 Fall 2019 LA 3 Instruction Execution Simulator o This class implements the ICPU interface and contains the registers used for executing the instructions.  Memory o This implements the IMemory interface and is implemented using an array.  Instruction o This implements the IExecutable interface. It is an abstract class and serves as a superclass for the classes of the different types of instructions. Since the “execute(…)” method defined in the IExecutable interface will vary from one type of instruction to another, it is executed by each of the subclasses of class Instruction (these subclasses are specified below). Note that interface IExecutable has a static method which must be implemented in the interface. Since this method is static, it can be called just by prefixing its name with the interface name followed by a period.  AddInstruction o Extends class Instruction and overrides the interface method “execute(…)”.  SubInstruction o Extends class Instruction and overrides the interface method “execute(…)”.  LoadInstruction o Extends class Instruction and overrides the interface method “execute(…)”.  StoreInstruction o Extends class Instruction and overrides the interface method “execute(…)”. An example of the input and expected output is given below. Your program output must follow the format given in the example output below and display the same content. Test you program with the test case provided in the Dropbox. Example Input: add,ax,1 store,ax,1 add,bx,2 store,bx,2 load,ax,1 load,bx,2 add,ax,bx store,ax,3 add,ax,6 store,ax,8 sub,ax,3 store,ax,9 Example Output: Initial memory contents: Address 0: 0 Address 1: 1 Address 2: 2 Address 3: 3 Address 4: 4 Address 5: 5 Address 6: 6 CS 1120 Fall 2019 LA 3 Instruction Execution Simulator Address 7: 7 Address 8: 8 Address 9: 9 Program output: Memory Status: Address 0: 0 Address 1: 1 Address 2: 2 Address 3: 3 Address 4: 4 Address 5: 5 Address 6: 6 Address 7: 7 Address 8: 9 Address 9: 6 CPU Status: ax:6 bx:2 The interfaces to be implemented (as specified above) are provided below. public interface IExecutable { /** * Overridden by the four subclasses of abstract class Instruction. * * It checks to see which register(s) and memory locations (if applicable) * are to be used for this instruction and sets the relevant register or * memory location with the result / value based on the specific instruction * (add, subtract, load or store). * * @param cpu The CPU object with the registers ax, bx * @param memory The memory object to be used for retrieving or storing data */ void execute(ICPU cpu, IMemory memory); /** * This is a static method and so must be implemented here. YOU WILL NEED * TO WRITE THE IMPLEMENTATION FOR THIS METHOD. * * It reads the data in the input file and stores the individual instructions * in an array of IExecutable objects. In order to determine what kind of * instruction to instantiate, it needs to check the first token (or word) * on each line and then create the corresponding Instruction object * (passing the parameters for that object to the relevant constructor in the process). * CS 1120 Fall 2019 LA 3 Instruction Execution Simulator * Remember that the first line in the input file specifies the number of * instructions stored in it. * * @param file The File object referencing the input file. * @return The array of IExecutable objects / instructions. * @throws IOException In case the input file is not found. */ static IExecutable[] loadInstructionsFromFile(File file) throws IOException{ // INCLUDE THE CODE FOR THIS STATIC METHOD. } /** * A getter for the first argument in an instruction. * * @return The first argument. */ String getArg1(); /** * A setter for the first argument. * * @param arg1 The first argument. */ void setArg1(String arg1); /** * A getter for the second argument in an instruction. * * @return The second argument. */ String getArg2(); /** * A setter for the second argument. * * @param arg2 The second argument. */ void setArg2(String arg2); /** * A getter for the operation code that specifies the type of operation * or instruction to be performed. * * @return The type of operation or instruction. */ String getOpcode(); /** * A setter for the type of operation to be performed. * * @param opcode The operation to be performed. */ void setOpcode(String opcode); CS 1120 Fall 2019 LA 3 Instruction Execution Simulator } // End of IExecutable public interface ICPU { /** * Prints out the status of the 2 CPU registers (ax and bx) - * i.e the values stored in the registers. */ void printCPUStatus(); /** * Returns the value stored in register ax. *
Oct 16, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here