We want to develop a simulator for a subset of the MiniJVM code. The following commands are to be implemented:
arithmetic operations: ADD, SUB
Loading constants: CONST i
Allocate storage space ALLOC i
Load/Store: LOAD i, STORE i
Comparison operators: LESS
Jump instructions: JUMP i, FJUMP i
End of program: STOP
Each operation is to be implemented in its own class, which inherits from the abstract class Instruction. A simulator has an array of instructions code and a stack. The stack stores int-values. For the sake of simplicity, this task uses ints as an internal representation of truth values. The stack of the simulators has a fixed capacity, which is defined during instantiation. The stackPointer of the stack points to the uppermost occupied cell. In addition, an instance of Simulator has a programCounter that stores the index of the next command to be executed and holds an attribute that stores whether a HALT was executed.
The files are already present in the template. The abstract class Instruction is already defined like the classes Simulator and Stack. Edit the following tasks:
Create the classes for the instructions in the predefined files. Select the class names so that they match the predefined file name. The classes should inherit from the abstract class Instruction.
The objects of the classes that represent the instructions for CONST i, ALLOC i, LOAD i, STORE i, JUMP i, FJump i should each have an int attribute that stores the parameter i of the command. In the classes for each of these commands, create a public constructor that expects an int parameter and initializes the respective attribute with it.
For each concrete class of instructions, implement the method public void execute(Simulator simulator), which changes the stack according to the command. The behavior of execute(...) for the different instructions is as follows:
ADD: Takes the two top elements from the stack and sets the result of the addition on the stack.
SUB: Takes the two top elements a, b from the stack and sets the result of b - a to the stack.
CONST i: Loads the constant i to the stack.
ALLOC i: Increases the stack pointer by i. You can use Stack's alloc method to do this.
LOAD i: Reads the value from the index i of the stack and places it on the stack.
STORE i: Takes the top value from the stack and stores it at index i of the stack.
LESS: Takes the two uppermost elements a, b from the stack and places the result of the comparison b < a as int coded on the stack. The truth value true should be coded by a 1, the truth value false by a 0.
JUMP i is to implement a jump to the command at index i.
FJump i Takes the top element of the stack. If the value of this element is 0, a jump to the command at index i is to be executed. Otherwise no jump is executed and the program flow continues normally.
HALT is to terminate the program. The halted attribute of the simulator should be set to true.
To access the stack of the simulator instance, use the getStack() object method.
In particular, a stack instance has the following methods that you can use during implementation:
pop() : Returns the element at the stack pointer and decrements the stack pointer.
push(int value): Increments the stack pointer by one and stores value at the new index of the stack pointer.
alloc(int count): Increases the stack pointer by count.
setValueAtIndex(int index, int value): Sets the value at index to value.
The program counter of the simulator can be set with setProgrammCounter(...).
You can assume that the stack sizes and instruction sequences of the MiniJVM code that are tested with will not produce stack overflow or stack underflow when correctly implemented.
Do not change the code of instruction, stack and simulator.
4 freelancer đang chào giá trung bình €37 cho công việc này
I'll complete your project satisfactorily. You will get quality work at an affordable price. I can start work right away. I will be looking forward to hearing from you. Thank you.