Skip to content

Commit

Permalink
adding opcodes (GT, LT, EQ, LHS, RHS, NEG, AND, OR, XOR) to the vm
Browse files Browse the repository at this point in the history
  • Loading branch information
xersky committed Dec 19, 2023
1 parent 7a6c6ba commit 3a219a6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
38 changes: 37 additions & 1 deletion Instruction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ public enum Instruction {
LOAD(0x0C),
STORE(0x0D),
DUP(0x0E),
SWAP(0x0F);
SWAP(0x0F),
GT(0x10),
LT(0x11),
EQ(0x12),
LHS(0x13),
RHS(0x14),
NEG(0x15),
AND(0x16),
OR(0x17),
XOR(0x18);

public int byteValue;

Expand Down Expand Up @@ -72,6 +81,33 @@ public static Instruction fromInt(int n) {

case 0x0F:
return SWAP;

case 0x10:
return GT;

case 0x11:
return LT;

case 0x12:
return EQ;

case 0x13:
return LHS;

case 0x14:
return RHS;

case 0x15:
return NEG;

case 0x16:
return AND;

case 0x17:
return OR;

case 0x18:
return XOR;

default:
throw new ExceptionInInitializerError();
Expand Down
2 changes: 1 addition & 1 deletion State.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class State {

Stack<Integer> stack = new Stack<Integer>();

byte[] memory = new byte[40];
byte[] memory = new byte[2048];

public Stack<Integer> getStack() {
return this.stack;
Expand Down
43 changes: 42 additions & 1 deletion VirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,50 @@ public Optional<Integer> byteInterpreter(byte[] byteChunk) {
stack.getStack().insertElementAt(stack.getStack().pop(), stack.getStack().size() - indexToSwap);
stack.getStack().remove(indexToSwap);
stack.getStack().push(itemToSwap);

i += 2;
break;

case GT:
calc(stack, (args) -> args[0] > args[1] ? 1 : 0);
break;

case LT:
calc(stack, (args) -> args[0] < args[1] ? 1 : 0);
break;

case EQ:
calc(stack, (args) -> args[0] == args[1] ? 1 : 0);
break;

case LHS:
calc(stack, (args) -> args[0] << args[1]);
break;

case RHS:
calc(stack, (args) -> args[0] >> args[1]);
break;

case NEG:
Integer argToNeg = stack.getStack().pop();
stack.getStack().push(~argToNeg);
/* byte[] byteToNeg = ByteBuffer.allocate(4).putInt(argToNeg).array();
byte[] negdByte = new byte[4];
for(int j = 3, k = 0; j >= 0; negdByte[k++] = byteToNeg[j--]);
stack.getStack().push(ByteBuffer.wrap(negdByte).getInt()); */
break;

case AND:
calc(stack, (args) -> args[0] & args[1]);
break;

case OR:
calc(stack, (args) -> args[0] | args[1]);
break;

case XOR:
calc(stack, (args) -> args[0] ^ args[1]);
break;
}
}

Expand Down

0 comments on commit 3a219a6

Please sign in to comment.