Skip to content

TinyJ VM Instruction Execution Notes

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].
    • PC contains the next instruction to be executed.

    • FP is a pointer to the data memory location at offset 0 in the currently executing method activation stack frame.
      (FP + k will be a pointer to data memory location offset k.)

    • 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).

Code
EXPRSTACK[--ESP-1] += EXPRSTACK[ESP];
Code
EXPRSTACK[--ESP-1] -= EXPRSTACK[ESP];
Code
EXPRSTACK[--ESP-1] *= EXPRSTACK[ESP];
Code
EXPRSTACK[--ESP-1] /= EXPRSTACK[ESP];
Code
EXPRSTACK[--ESP-1] %= EXPRSTACK[ESP];
Code
EXPRSTACK[ESP-1] *= -1;
Code
EXPRSTACK[ESP-1] ^= 1;
Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP] == EXPRSTACK[ESP-1] && EXPRSTACK[ESP] == 1) ? 1 : 0;
Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP] == 1 || EXPRSTACK[ESP-1] == 1) ? 1 : 0;
Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP] == EXPRSTACK[ESP-1]) ? 1 : 0;
Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP] != EXPRSTACK[ESP-1]) ? 1 : 0;
Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP-1] <= EXPRSTACK[ESP]) ? 1 : 0;
Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP-1] >= EXPRSTACK[ESP]) ? 1 : 0;
Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP-1] < EXPRSTACK[ESP]) ? 1 : 0;
Code
EXPRSTACK[--ESP-1] = (EXPRSTACK[ESP-1] > EXPRSTACK[ESP]) ? 1 : 0;
Code
EXPRSTACK[ESP] = operand;
ESP++;
Code
System.out.println();
Code
System.out.print(EXPRSTACK[--ESP]);
Code
for (int i = firstOperand; i <= secondOperand; i++){
System.out.print(TJ.data[i]);
}
Code
PC = operand;
Code
PC = (EXPRSTACK[ESP-1] == 0) ? operand: PC;
ESP--;
Code

POINTERTAG indicates that the address is a pointer.

EXPRSTACK[ESP] = operand + POINTERTAG;
ESP++;
Code
EXPRSTACK[ESP] = FP + operand;
ESP++;
Code
int value = TJ.data[EXPRSTACK[ESP-1] - POINTERTAG];
EXPRSTACK[ESP-1] = value;
Code
int val = EXPRSTACK[--ESP];
int addr = EXPRSTACK[--ESP] - POINTERTAG;
TJ.data[addr] = val;
Code
TJ.data[ASP - POINTERTAG] = EXPRSTACK[--ESP];
ASP++;
Code
TJ.data[ASP - POINTERTAG] = PC;
ASP++;
PC = operand;
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;
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