Koray Tugay
Koray Tugay

Reputation: 23800

What does "taking operands immediately following the opcode" mean?

I am reading the book Inside the Java 2 Virtual Machine and I can't understand what this means:

The Java Virtual Machine is stack-based rather than register-based because its instructions take their operands from the operand stack rather than from registers. Instructions can also take operands from other places, such as immediately following the opcode (the byte representing the instruction) in the bytecode stream, or from the constant pool.

Can anyone help me with the part:

Instructions can also take operands from other places, such as immediately following the opcode

, perhaps with an example?

Upvotes: 0

Views: 140

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726889

This is a fancy way of saying that data values can be embedded within the byte code itself.

Consider sipush instruction for an example. It pushes a short value onto the stack. The value of the short comes from two bytes that follow the instruction:

sipush <byte1> <byte2>

Another situation that the paragraph fails to mention is when the value is embedded within the opcode itself, as in iconst_<N> instructions. For example,

iconst_5

loads constant 5 onto the stack. The value of 5 does not come from a separate storage, because it is embedded in the meaning of the instruction opcode.

Upvotes: 1

Tagir Valeev
Tagir Valeev

Reputation: 100279

For example, there's an iinc instruction which adds a constant value to the local variable like this:

iinc 1, 8

This means "add 8 to the local variable #1". The constant 8 is directly written in the bytecode, following the iinc instruction code and constant 1: 0x84 0x01 0x08.

Upvotes: 1

Related Questions