The Artima Developer Community
Sponsored Link

Logical Results
A Simulation of the Java Virtual Machine

Advertisement

The Logical Results applet, included below, demonstrates a Java virtual machine executing a sequence of bytecodes. This applet accompanies Chapter 13, "Logic," of Inside the Java 2 Virtual Machine.

For some reason, your browser won't let you view this way cool Java applet.

The bytecode sequence in the simulation was generated by javac for the incrementLogically() method of the VulcanCounter class:

// On CD-ROM in file opcodes/ex1/VulcanCounter.java
class VulcanCounter {

    static void incrementLogically() {
        int spock = 0;
        for (;;) {
            int tempSpock = spock;
            for (int i = 0; i < 32; ++i) {
                int mask = 0x1 << i;
                if ((tempSpock & mask) == 0) {
                    tempSpock |= mask;  // Change 0 to 1
                    break;
                }
                else {
                    tempSpock &= ~mask; // Change 1 to 0
                }
            }
            spock = tempSpock;
        }
    }
}

The bytecodes generated by javac for incrementLogically() are:

 0 iconst_0     // Push int constant 0.
 1 istore_0     // Pop to local variable 0: int spock = 0;
 2 iload_0      // Push local variable 0 (spock).
 3 istore_1     // Pop to local variable 1: int tempSpock = spock;
 4 iconst_0     // Push int constant 0.
 5 istore_2     // Pop to local variable 2: int i = 0;
 6 goto 35      // Jump unconditionally ()
 9 iconst_1     // Push int constant 1.
10 iload_2      // Push local variable 2 (i).
11 ishl         // Arithmetic shift left top int (i) by next
                // to top int (1).
12 istore_3     // Pop to local variable 3: int mask = i << 0x1;
13 iload_1      // Push local variable 1 (tempSpock).
14 iload_3      // Push local variable 3 (mask).
15 iand         // Bitwise AND top two ints: (spock & mask)
16 ifne 26      // Jump if top of stack is not equal to zero:
                // if ((spock & mask) == 0) {
19 iload_1      // Push local variable 1 (tempSpock).
20 iload_3      // Push local variable 3 (mask).
21 ior          // Bitwise OR top two ints (tempSpock | mask)
22 istore_1     // Pop to local variable 1: tempSpock |= mask;
23 goto 41      // Jump unconditionally (to just after
                // inner for): break;
26 iload_1      // Push local variable 1 (tempSpock).
27 iload_3      // Push local variable 3 (mask).
28 iconst_m1    // Push -1.
29 ixor         // Bitwise EXCLUSIVE-OR top two ints: ~mask
30 iand         // Bitwise AND top two ints: tempSpock & (~mask)
31 istore_1     // Pop to local variable 1: tempSpock &= ~mask;
32 iinc 2 1     // Increment local variable 2 by 1: ++i
35 iload_2      // Push local variable 2 (i).
36 bipush 32    // Push integer constant 32.
38 if_icmplt 9  // Jump (to top of inner for) if "next to top"
                // integer is less than "top" integer: i < 32
41 iload_1      // Push local variable 1 (tempSpock)
42 istore_0     // Pop to local variable 0: spock = tempSpock;
43 goto 2       // Jump unconditionally (to top of outer for).

The incrementLogically() method repeatedly increments an int without using the + or ++ operators. It uses only the logical operators: &, |, and ~. An increment is accomplished by searching through the bits of the current int, starting with the lowest order bit, and turning ones to zeros. As soon as a zero is encountered in the current int, it is changed to one and the search stops. The resultant int now represents the old int incremented by one. The process is started again on the new int. Each incremented number is stored in the spock variable. spock is local variable zero in the compiled bytecodes, so you can watch local variable zero count 0, 1, 2, 3, and so on.

To drive the Logical Results simulation, use the Step, Reset, Run, and Stop buttons. Each time you press the Step button, the simulator will execute the instruction pointed to by the pc register. If you press the Run button, the simulation will continue with no further coaxing on your part until you press the Stop button. To start the simulation over, press the Reset button. For each step of the simulation, a panel at the bottom of the applet contains an explanation of what the next instruction will do. Happy clicking.


Click here to view a page of links to the source code of the Logical Results applet.


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use