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. The bytecode sequence in the simulation was generated by // 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 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 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. |
Copyright © 1996-1999 Bill Venners. All Rights Reserved. |