The Prime Time applet, included below, demonstrates a Java virtual machine executing a sequence of bytecodes that generates prime numbers. This applet accompanies Chapter 12, "Integer Arithmetic," of Inside the Java 2 Virtual Machine. The bytecode sequence in the simulation was generated by the // On CD-ROM in file integer/ex1/PrimeFinder.java class PrimeFinder { static void findPrimes() { int primeNum = 1; int numToCheck = 2; for (;;) { boolean foundPrime = true; for (int divisor = numToCheck / 2; divisor > 1; --divisor) { if (numToCheck % divisor == 0) { foundPrime = false; break; } } if (foundPrime) { primeNum = numToCheck; } ++numToCheck; } } } The For each number to check, the In the inner for loop, the For example, when The bytecodes generated by 0 iconst_1 // Push int constant 1 1 istore_0 // Pop into local var 0: int primeNum = 1; 2 iconst_2 // Push int constant 2 3 istore_1 // Pop into local var 1: int numToCheck = 2; // The outer for loop (the "forever" loop) begins here: 4 iconst_1 // Push int constant 1 5 istore_2 // Pop into local var 2: boolean foundPrime = true; // The inner for loop begins here. First, initialize divisor. 6 iload_1 // Push int in local var 1 (numToCheck) 7 iconst_2 // Push int constant 2 8 idiv // Pop two ints, divide them, push int result // Pop int into local var 3: 9 istore_3 // int divisor = numToCheck / 2; // Next, test the inner for loop's termination condition 10 goto 27 // Jump to for loop condition check // The body of the inner for loop begins here. 13 iload_1 // Push the int in local var 1 (numToCheck) 14 iload_3 // Push the int in local var 3 (divisor) 15 irem // Pop two ints, remainder them, push result // Pop int, jump if equal to zero: 16 ifne 24 // if (numToCheck % divisor == 0) 19 iconst_0 // Push int constant 0 20 istore_2 // Pop into local var 2: foundPrime = false; 21 goto 32 // Jump out of inner for loop // At this point, the body of the inner for loop is done. Now just // perform the third statement of the for expression: decrement // divisor. 24 iinc 3 -1 // Increment local var 3 by -1: --divisor // The test for the inner for loop's termination condition // begins here. This loop will keep on looping while (divisor > 1). 27 iload_3 // Push int from local var 3 (divisor) 28 iconst_1 // Push int constant 1 29 if_icmpgt 13 // Pop top two ints, jump if greater than // At this point, the inner for loop has completed. Next check // to see if a prime number was found. 32 iload_2 // Push int from local var 2 (foundPrime) 33 ifeq 38 // Pop top int, jump if zero: if (foundPrime) { 36 iload_1 // Push int from local var 1 (numToCheck) 37 istore_0 // Pop into local var 0: primeNum = numToCheck; 38 iinc 1 1 // Increment local var 1 by 1: ++numToCheck; 41 goto 4 // Jump back to top of outer for loop. The One thing to note about this bytecode sequence is that it demonstrates the way in which Another thing to note about this simulation is that eventually the To drive the Prime Time 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 Prime Time applet. |
Copyright © 1996-1999 Bill Venners. All Rights Reserved. |