Search code examples
compiler-constructionjvmvm-implementation

ioper commands isub idiv order in java virtualmachine


when looking at ioper commands imul and iadd are obvious but if I have:

sipush 9
sipush 3
sipush 4

the stack will look like

4
3
9

if the next one is isub do i subtract 4 from 3? or 3 from 4

cheers


Solution

  • The semantics of the instructions are defined in section 6.5 of the JVM spec. In particular isub is defined like this:

    Operand Stack

    ..., value1, value2 →

    ..., result

    Description

    Both value1 and value2 must be of type int. The values are popped from the operand stack. The int result is value1 - value2. The result is pushed onto the operand stack.

    The notation ..., value1, value2 means that value2 is on the top of the stack and value1 is the one below that (and the rest of the stack is denoted as ... because isub does not touch it).

    So in your example it'd be 3 - 4 because value1 = 3 and value2 = 4.