Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

help me write a java code for this lab, thank you Hint (starting code): public c

ID: 3602795 • Letter: H

Question

help me write a java code for this lab, thank you

Hint (starting code):

public class VirtualMachine
{
private IntStack stack = new IntStack(20);
private int pc;
private int ir;
private int[] memory = {8,5, // const 1
8,2, // const 2
11, // add
10,16, //store at address 16
9,16, // load from address 16
21, // write
0, // halt
0,0,0,0,0,0,0,0,0,0,0,0};
  
public VirtualMachine()
{
int d, x, y;
do
{
ir = memory [pc]; //fetch
pc++;// inc the pc

switch(ir)
{
case 0: // halt
System.out.println("Program halted by halt instruction");
System.exit(0);
break;

case 8:// const
d = memory[pc];//get constant from memory
pc++;// inc the pc
stack.push(d);//Store constant on stack
break;

case 9:// load
stack.push(memory[memory[pc]]);
pc++;// inc the pc
break;

default:
System.out.println("Program halted by halt instruction");
System.exit(0);
break;

}//switch
}while(true);
}//FetchDecodeExecute

public static void main(String[] args)

Explanation / Answer

This coding requires simple logic and the good understanding of the Java programming.

Here we have written the IntStack class to maintain the stack related methods and we have used it in the main class VirtualMachine where we are performing the operation on the stack values. so, here you find the code for this problem.

============================================================================

import java.util.Scanner; //importing Scanner to read input

class IntStack //Stack Class

{

private int[] stack; //Stack array

private int last; //Stack entry/exit pointer

  

public IntStack(int size)

{

stack = new int[size];

last = -1;

}

  

public void push(int value)

{

stack[++last] = value; //incr. pointer & then Adding value to stack

}

  

public int pop()

{

if (last == -1) { //chceking if stack is empty or not

return -1;

}

else {

return stack[last--]; //Removing last value and decr. pointer

}

}

}

public class VirtualMachine

{

private IntStack stack = new IntStack(20);

private int pc;

private int ir;

private int[] memory = {8,5, // const 1

8,2, // const 2

11, // add

10,16, //store at address 16

9,16, // load from address 16

21, // write

0, // halt

0,0,0,0,0,0,0,0,0,0,0,0};

  

public VirtualMachine()

{

int d, x, y, a;

do

{

ir = memory [pc]; //fetch

pc++; //incr. the pc

  

switch(ir)

{

case 0: // Halt/Terminate

System.out.println("Program halted by halt instruction");

System.exit(0);

break;

  

case 8:// Load constant to stack

d = memory[pc];//get constant from memory

pc++;// incr. the pc

stack.push(d);//Store constant on stack

break;

  

case 9:// Load variable from memory to stack

a = memory[pc];

stack.push(memory[a]);

pc++;// inc the pc

break;

  

case 10:// store variable from the stack to memory

d = stack.pop();

a = memory[pc];

memory[a] = d;

pc++;

break;

  

case 11:// Add two number

y = stack.pop();

x = stack.pop();

d = x + y;

stack.push(d);

break;

  

case 12:// Subtract two number

y = stack.pop();

x = stack.pop();

d = x - y;

stack.push(d);

break;

  

case 13:// Multiply two number

y = stack.pop();

x = stack.pop();

d = x * y;

stack.push(d);

break;

  

case 14:// Divide two number

y = stack.pop();

x = stack.pop();

d = x / y;

stack.push(d);

break;

  

case 15:// Equal check

y = stack.pop();

x = stack.pop();

if (x == y) {

stack.push(1);

}

else {

stack.push(0);

}

break;

  

case 16:// Less/min number check

y = stack.pop();

x = stack.pop();

if (x < y) {

stack.push(1);

}

else {

stack.push(0);

}

break;

  

case 17:// Greater/max number chcek

y = stack.pop();

x = stack.pop();

if (x > y) {

stack.push(1);

}

else {

stack.push(0);

}

break;

  

case 18:// Jump to index 'a'

pc = memory[pc];

break;

  

case 19:// Jump to index 'a' if false

if (stack.pop() == 0) {

pc = memory[pc];

}

break;

  

case 20:// Read/Input (int)

System.out.println("Enter any Integer number: ");

Scanner in = new Scanner(System.in);

d = in.nextInt();

stack.push(d);

break;

  

case 21:// Write/Output (int)

System.out.println(stack.pop());

break;

  

case 22:// Call subprogram

stack.push(pc + 1);

pc = memory[pc];

break;

  

case 23:// Return from subprogram

pc = stack.pop();

break;

  

default:

System.out.println("Program halted by halt instruction");

System.exit(0);

break;

  

}//switch

  

}while(true);

  

}//FetchDecodeExecute

  

public static void main(String[] args) {

VirtualMachine thisObj = new VirtualMachine();

}

}