Make sure to do something with the return value on the stack after returning from a method.
.constant
OBJREF 0x40 // dummy value used for method invocation
.end-constant
.main
// Push the object reference onto the stack
LDC_W OBJREF
// Push the parameters on the stack (2 in this case)
BIPUSH 0x03
BIPUSH 0x04
INVOKEVIRTUAL adddigits
BIPUSH 0x30 // Convert to an ASCII digit
IADD
OUT // display result which is on top of stack
HALT
.end-main
.method adddigits(num1, num2)
ILOAD num1
ILOAD num2
IADD // This will leave the sum on top of stack.
// This is just as good as pushing the return value.
IRETURN
.end-method
|
|
|
|
|
|
![]() 3 byte of code for INVOKEVIRTUAL instruction |
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
Initial Stack
|
Initial Registers
| ||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
| 1 | Read the 16 bit displacement operand |
|---|---|
| 2 | Compute the index into the constant pool where method code address is stored. |
| 3 | From the constant pool, get the address of the method code in the method area |
| 4 | From the method area, get the two bytes indicating how many parameters this method has. |
| 5 | Compute new location of the local variable stack frame. |
| 6 | Save the current value of the PC and LV registers. |
| 7 | Look in the method area to find out how many extra local variables this method uses. |
| 8 | Make room on the stack for the extra local variables. |
| 9 | Put the old PC and LV registers onto the stack. |
| 10 | Set the local variable stack frame for this new method and start executing its instructions. |
|
|
|
||||||||||||||||||||||||
IRETURN Implementation Overview
|
|
Current Stack
|
Current Registers
| ||||||||||||||||||||||
| 1 | Reset the stack pointer.SP |
|---|---|
| 2 | Restore the program counter.PC |
| 3 | Restore the local variable pointer.LV |
| 4 | move return value to top of current stack |
|
|
|
||||||||||||||||||||||||
| 1 | Reset the stack pointer.SP |
|---|---|
| 2 | Restore the program counter.PC |
| 3 | Restore the local variable pointer.LV |
| 4 | move return value to top of current stack |
|
|
|
||||||||||||||||||||||||
| 1 | Reset the stack pointer.SP |
|---|---|
| 2 | Restore the program counter.PC |
| 3 | Restore the local variable pointer.LV |
| 4 | move return value to top of current stack |
|
|
|
||||||||||||||||||||||||
| 1 | Reset the stack pointer.SP |
|---|---|
| 2 | Restore the program counter.PC |
| 3 | Restore the local variable pointer.LV |
| 4 | move return value to top of current stack |
|
|
|
||||||||||||||||||||||||

