|
|
|
Advertisement
|
Arithmetic opcodes
Integer addition can be performed on ints and longs.
Bytes, shorts, and chars are automatically converted to int before they
take part in an addition. The following table shows the opcodes that pop
the top two values on the stack, add them, and push the result. The type
of the values is indicated by the opcode itself, and the result always
has the same type as the numbers being added. No exceptions are thrown
for any of these opcodes. Overflow is just ignored.
The next table shows the exception to the rule that arithmetic opcodes take their operands from the stack. The iinc opcode performs an addition on a local variable of type int. The local variable is indicated by the first byte that follows the iinc instruction in the bytecode stream. The amount to add to the local variable is taken from the second byte following the iinc instruction. The second byte is interpreted as a byte type, an eight-bit signed two's-complement number. The local variable and byte are added, and the result is written back to the local variable. This opcode can be used to change a local variable value by any number between and including -128 through 127. This opcode makes for more efficient incrementing and decrementing of variables that are used to control execution of loops, such as for or while. No exceptions are thrown.
Integer subtraction is performed on ints and longs via the following opcodes. Each opcode causes the top two values of the appropriate type to be popped off the stack. The topmost value is subtracted from the value just beneath the topmost value. The result is pushed back onto the stack. No exceptions are thrown by these opcodes.
Integer multiplication of ints and longs is accomplished via the following opcodes. Each opcode causes two values of the same type to be popped off the stack and multiplied. The result, of the same type as the numbers being multiplied, is pushed back onto the stack. No exceptions are thrown.
The opcodes that perform division on ints and longs are shown in the next table. The division opcodes cause the top two values of the appropriate type to be popped off the stack. The topmost value is divided by the value just beneath the topmost value. The result is pushed onto the stack. Integer division yields a result that is truncated down to the nearest integer value between it and zero. Integer division by zero throws a "/ by 0" ArithmeticException.
The remainder operation is accomplished via the following opcodes on ints and longs. The following opcodes cause the top two values to be popped from the stack. The topmost value is divided by the value just beneath it, and the remainder of that division is pushed back onto the stack. As with the division opcodes, integer remainder by zero throws a "/ by 0" ArithmeticException.
The following opcodes perform arithmetic negation on ints and longs. The negation opcodes pop the top value from the stack, negate it, and push the result.
Logical operands
The JVM's logic capabilities operate on ints and longs.
These operations treat ints and longs not as signed two's-complement numbers,
necessarily, but more as generic bit patterns. Integer shifting is accomplished
via the ishl, ishr, and iushr opcodes. Java's << operator is implemented by ishl.
The >> operator is implemented by ishr, and the >>> operator is implemented by
iushl. The difference between ishr and iushr is
that only ishr does sign extension. The following table
shows the instructions that shift ints left and right.
The next table shows the instructions that shift longs left and right.
The following opcodes perform bitwise logical operations on ints. The opcodes implement Java's &, |, and ^ operators.
The next table shows the opcodes that perform bitwise logical operations on longs.
|
Sponsored Links
|