TinyJ VM Instruction Execution Notes

Expression Evaluation Stack

Expression Evaluation Stack: EXPRSTACK[]

INSTRUCTIONS

ADD Instruction

Code
EXPRSTACK[--ESP-1] += EXPRSTACK[ESP];

SUB Instruction

Code
EXPRSTACK[--ESP-1] -= EXPRSTACK[ESP];

MUL Instruction

Code
EXPRSTACK[--ESP-1] *= EXPRSTACK[ESP];

DIV Instruction

Code
EXPRSTACK[--ESP-1] /= EXPRSTACK[ESP];

MOD Instruction

Code
EXPRSTACK[--ESP-1] %= EXPRSTACK[ESP];

CHANGESIGN Instruction

Code
EXPRSTACK[ESP-1] *= -1;

NOT Instruction

Code
EXPRSTACK[ESP-1] ^= 1;

AND Instruction

Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP] == EXPRSTACK[ESP-1] && EXPRSTACK[ESP] == 1) ? 1 : 0;

OR Instruction

Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP] == 1 || EXPRSTACK[ESP-1] == 1) ? 1 : 0;

EQ Instruction

Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP] == EXPRSTACK[ESP-1]) ? 1 : 0;

NE Instruction

Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP] != EXPRSTACK[ESP-1]) ? 1 : 0;

LE Instruction

Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP-1] <= EXPRSTACK[ESP]) ? 1 : 0;

GE Instruction

Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP-1] >= EXPRSTACK[ESP]) ? 1 : 0;

LT Instruction

Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP-1] < EXPRSTACK[ESP]) ? 1 : 0;

GT Instruction

Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP-1] > EXPRSTACK[ESP]) ? 1 : 0;

PUSHNUM Instruction

Code
EXPRSTACK[ESP] = operand;
ESP++;

WRITELNOP Instruction

Code
System.out.println();

WRITEINT Instruction

Code
System.out.print(EXPRSTACK[--ESP]);

WRITESTRING Instruction

Code
for (int i = firstOperand; i <= secondOperand; i++){
  System.out.print(TJ.data[i]);
}

JUMP Instruction

Code
PC = operand;

JUMPONFALSE Instruction

Code
PC = (EXPRSTACK[ESP-1] == 0) ? operand: PC;
ESP--;

PUSHSTATADDR Instruction

Code

POINTERTAG indicates that the address is a pointer.

EXPRSTACK[ESP] = operand + POINTERTAG;
ESP++;

PUSHLOCADDR Instruction

Code
EXPRSTACK[ESP] = FP + operand;
ESP++;

LOADFROMADDR Instruction

Code
int value = TJ.data[EXPRSTACK[ESP-1] - POINTERTAG];
EXPRSTACK[ESP-1] = value;

SAVETOADDR Instruction

Code
int val = EXPRSTACK[--ESP];
int addr = EXPRSTACK[--ESP] - POINTERTAG;
TJ.data[addr] = val;

PASSPARAM Instruction

Code
TJ.data[ASP - POINTERTAG] = EXPRSTACK[--ESP];
ASP++;

CALLSTATMETHOD Instruction

Code
TJ.data[ASP - POINTERTAG] = PC;
ASP++;
PC = operand;

INITSTKFRM Instruction

Code

TJ.data[ASP++ - POINTERTAG] = FP; // sets offset 0 to offset 0 of caller
FP = ASP - 1; // sets new FP to current offset 0
ASP += operand;

RETURN Instruction

Code
ASP = FP + 1; // sets ASP = offset 1
FP = TJ.data[--ASP - POINTERTAG]; // sets FP to caller's offset 0
// instruction that we need to return to
PC = TJ.data[--ASP - POINTERTAG]; // sets PC = next instruction to be (offset - 1)
ASP -= operand; // decreases ASP by # of operands to go to previous stack