Search code examples
javajvmjava-bytecode-asm

In JVM Instructions, some are of different prefixes but their functions are quite similar. Do we really need them all? If Yes, why?


For add : iadd/ladd/fadd/dadd, For sub : isub/lsub/fsub/dsub, For mul : imul/lmul/fmul/dmul For div : idiv/ldiv/fdiv/ddiv For rem : irem/lrem/frem/drem Fro neg : ineg/lneg/fneg/dneg...

  1. Can we only use only one instruction, e.g. add, to replace all the add instructions?
  2. If the answer of Q1 is true, will this get better performance than current solutions?
  3. If Q1 is false, why?

Solution

  • Just like in real machines, you can't have the same instruction for negating a double or an int. These instructions work really different on the bit-level, and are only the same on a sufficiently high abstraction level (i.e. negate a number).

    This is why we have higher level languages to abstract away uninteresting machine details. But in an assembly language, the distinction is essential.