Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Furthermore, instructions that begin with one of these letters followed by the letter a operate on arrays of the type. Thus, iload loads an int value from the local variable array onto the stack, but iaload loads a value from an array of ints onto the stack. (I discuss arrays in more detail later.)

Instructions that end with an underscore (_) followed by a small integer (0, 1, 2, or 3) operate on the local variable at that point in the local variable array. Otherwise, the next byte in the code array is used as an unsigned index into the local variable array. Thus, istore_2 pops a value from the stack and stores it in local variable 2. However, istore first reads another byte from the code array to determine which local variable it should use to store the value that it pops from the top of the stack.

There are 12 basic instructions that work with the local variable array: aload, iload, fload, lload, dload, istore, fstore, astore, lstore, dstore, ret, and iinc. Each of these instructions is followed by a single, unsigned byte. This byte determines which local variable is used. The use of a single byte is very space-efficient. However, it does place an upper limit of 256 local variables, fewer if some of them are longs or doubles. Sometimes this isn’t quite enough.

The wide instruction allows access to many more local variables — up to 65,536. When wide precedes any of the first 11 of these instructions (that is, any except ret), the instruction uses the next two bytes in the code array, rather than only one byte, as an index into the local variable array.

For example, istore 2 normally means to pop an int from the stack and store it in local variable 2. However, if the instruction before istore is wide, then you have to read an extra byte. Thus, wide istore 2 8 means to pop an int from the stack and store it in local variable 520. Figure 5-18 demonstrates.


Figure 5-18  The effect of the wide instruction.

The wide instruction has an even larger effect on the iinc instruction. Recall that normally the iinc instruction is followed in the code array by two bytes, an index into the local variable array and the constant increment. When preceded by wide, both of these are widened to 2-byte shorts, the first unsigned and the second signed. Thus, wide iinc 0 2 2 0 means “increment local variable 2 by 512.”

Arithmetic

Arithmetic byte codes come in two groups: the binary operators and the unary operators. Most of the common operators that you’re familiar with, such as + and *, are binary operators. This means that they take two arguments. For example, you always add numbers two at a time. There’s no way to add just a single number, even when you write something like this:

     int a = 3 + 7 + c;

Java first adds the 3 and the 7. Then it adds c to their sum. That is, it splits the calculation into two parts, like this:

     int temp = 3 + 7;
     int a = temp + c;

In fact, you do this sort of splitting implicitly when you add a series of numbers yourself by hand.

The only unary arithmetic operator in Java is the minus sign (-). This operator changes the sign of a variable. Thus, if the int variable a is 7, then -a is -7. This can be a little confusing, because the - character serves two other purposes in Java. It is also the binary subtraction operator in expressions like 3 - 7 and can be a part of numeric literals like -7 or -98.6. Although these appear to be the same thing in .java source code, they are three different things in .java byte code.

All byte code binary arithmetic instructions operate on two values of the same type. In other words, ints can be added only to other ints, floats to other floats, and so on. You can’t add an int and a double or multiply a float times a long. If you need to combine two types in one expression, you must first use one of the type conversion operators that we discuss later.

The add codes

There are four addition instructions: iadd, ladd, fadd, and dadd. These add ints, longs, floats, and doubles respectively.

The instructions iadd and fadd pop two words off the stack, add them, and push the sum back onto the stack. After the instruction has executed, the stack is one word shorter. Figure 5-19 shows the addition of two ints — 4 and 2 — to get 6.


Figure 5-19  Adding 4 to 2 to get 6.

The instructions ladd and dadd pop four words from the stack, add them, and push a two-word result back onto the stack. After the instruction has executed, the stack is two words shorter. Figure 5-20 shows the addition of the long values 4L and 2L to get 6L.


Figure 5-20  Adding 4L to 2L to get 6L.

The sub codes

Subtraction is similar to addition. There are four subtraction operators — isub, lsub, fsub, and dsub — one each for ints, longs, floats, and doubles. Unlike addition, subtraction is not commutative; that is, a - b is not, in general, the same as b - a. Therefore, it’s important to note that the value on the top of the stack is subtracted from the value immediately below it, and not the other way around. Figure 5-21 and Figure 5-22 demonstrate this.


Figure 5-21  4 - 2 = 2.


Figure 5-22  2 - 4 = -2.

Floats are subtracted exactly the same way. Longs and doubles are too, except that each number requires two words. Figure 5-23 shows what happens on the stack when you subtract 2L from 4L.


Figure 5-23  4L - 2L.

The mul codes

There are four multiplication instructions: imul, lmul, fmul, and dmul. These multiply ints, longs, floats, and doubles, respectively. The instructions imul and fmul pop two words off the stack, multiply them, and push the product back onto the stack. After the instruction has executed, the stack is one word shorter. Figure 5-24 shows the multiplication of two ints — 4 and 2 — to get 8.


Figure 5-24  Multiplying 4 by 2 to get 8.

The instructions lmul and dmul pop two two-word values from the stack (a total of four words), multiply them, and push a two-word result back onto the stack. After the instruction has executed, the stack is two words shorter. Figure 5-25 shows the multiplication of the long values 4L and 2L to get 8L.


Figure 5-25  Multiplying 4L by 2L to get 8L.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.