CDA-4101 Lecture 18 Notes



IJVM ISA


Memory Usage


Local Variables in Fixed Locations


Stacks

  • A stack turns out to be a useful data structure for storing local variables.
  • When a procedure is called, you push its local variables onto the stack; when it finishes you pop them off the stack.
  • Recursive calls to the same routine just keep popping new sets of local variables on top of the stack.
  • each procedure call creates its own local variable stack frame
  • However, the stack itself is really just some area of memory, so each local variable on the stack will still have a memory address.
  • A much simpler way to reference the local variables is relative to the stack frame.
  • This greatly simplifies figuring out where the variables for a function are, since they will always be in the same relative location, where this is relative to the stack frame.

Important Note

When we are showing a bunch of memory locations as a vertical array of cells, higher address memory locations are on the bottom. However, when showing stacks as an array of cells, the higher memory addresses are toward the top.


Local Variable (LV) Stack Frames

  • We'll reserve some section of main memory to hold the stack
  • We will use a register (SP), will keep track of the top of the stack
  • We will use another register (LV) to hold the address of the current local stack frame (where the local variables begin in memory).
  • Our data is always 4 bytes (remember we have 32 bit registers)

Local Variable Stack Frame Example

  1. method 'a' called, 3 local variables
  2. 'a' calls 'b' which has 4 local variables
  3. 'b' call 'c' which has 2 local variables
  4. 'c' returns
  5. 'b' returns
  6. 'a' calls 'd' which has 5 local variables

This is the general idea. In the next lecture we will look at the details of how this is really done in IJVM and the Mic-1 architecture


Operand Stacks


IJVM Memory Model


Words vs. Bytes


IJVM Instruction Set

Op-code
(Hex)
Assembly
Language
Mnemonic
Operands Description
0x10 BIPUSH byte Push a byte onto stack
0x59 DUP N/A Copy top word on stack and push onto stack
0xA7 GOTO label name Unconditional jump
0x60 IADD N/A Pop two words from stack; push their sum
0x7E IAND N/A Pop two words from stack; push Boolean AND
0x99 IFEQ label name Pop word from stack and branch if it is zero
0x9B IFLT label name Pop word from stack and branch if it is less than zero
0x9F IF_ICMPEQ label name Pop two words from stack and branch if they are equal
0x84 IINC variable name, byte Add a constant value to a local variable
0x15 ILOAD variable name Push local variable onto stack
0xB6 INVOKEVIRTUAL method name Invoke a method
0x80 IOR N/A Pop two words from stack; push Boolean OR
0xAC IRETURN N/A Return from method with integer value
0x36 ISTORE variable name Pop word from stack and store in local variable
0x64 ISUB N/A Pop two words from stack; push their difference
0x13 LDC_W constant name Push constant from constant pool onto stack
0x00 NOP N/A Do nothing
0x57 POP N/A Delete word from top of stack
0x5F SWAP N/A Swap the two top words on the stack
0xC4 WIDE N/A Prefix instruction; next instruction has a 16-bit index
N/A ERR N/A Print an error message and halt the simulator
N/A HALT N/A Halt the simulator
N/A IN N/A Reads a character from the keyboard buffer and pushes it onto the stack. If no character is available, 0 is pushed
N/A OUT N/A Pop word off stack and print it to standard out

IJVM Instruction Set Notes



Java to IJVM Conversion

Java IJVM Assembly IJVM ISA
  i = j + k ;
  if ( i == 3 )
  {
    k = 0 ;
  }
  else
  {
    j = j - 1 ;
  }
       ILOAD j 0x15 0x02
       ILOAD k 0x15 0x03
       IADD 0x60
       ISTORE i 0x36 0x01
       ILOAD i 0x15 0x01
       BIPUSH 3 0x10 0x03
       IF_ICMPEQ L1 0x9F 0x00 0x0D
       ILOAD j 0x15 0x02
       BIPUSH 1 0x10 0x01
       ISUB 0x64
       ISTORE j 0x36 0x02
       GOTO L2 0xA7 0x00 0x07
L1: BIPUSH 0 0x10 0x00
       ISTORE k 0x36 0x03
L2: