TinyJ VM Instruction Execution Notes
Expression Evaluation Stack
Expression Evaluation Stack: EXPRSTACK[]
-
Data memory:
TJ.data[]→TJ.data[c]represents the data memory location whose address is c. -
VM registers:
ESP,PC,FP,ASP-
ESP is a count of the number of items on the expression stack (
EXPRSTACK).- Top of stack is
EXPRSTACK[ESP - 1].
- Top of stack is
-
PC contains the next instruction to be executed.
-
FP is a pointer to the data memory location at offset
0in the currently executing method activation stack frame.
(FP + kwill be a pointer to data memory location offsetk.) -
ASP is a pointer to the first unused location in the stack-dynamically allocated level of data memory
(location after address of last local variable).
-
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