The Fibonacci Forever applet, included below, demonstrates a Java Virtual Machine executing a sequence of bytecodes that generate the Fibonacci series. This applet accompanies Chapter 10, "Stack and Local Variable Operations, " of Inside the Java 2 Virtual Machine. The bytecode sequence in the simulation was generated by the // On CD-ROM in file stackops/ex1/Fibonacci.java class Fibonacci { static void calcSequence() { long fiboNum = 1; long a = 1; long b = 1; for (;;) { fiboNum = a + b; a = b; b = fiboNum; } } } The The bytecodes generated by 0 lconst_1 // Push long constant 1 1 lstore_0 // Pop long into local vars 0 & 1: long a = 1; 2 lconst_1 // Push long constant 1 3 lstore_2 // Pop long into local vars 2 & 3: long b = 1; 4 lconst_1 // Push long constant 1 5 lstore 4 // Pop long into local vars 4 & 5: long fiboNum = 1; 7 lload_0 // Push long from local vars 0 & 1 8 lload_2 // Push long from local vars 2 & 3 9 ladd // Pop two longs, add them, push result 10 lstore 4 // Pop long into local vars 4 & 5: fiboNum = a + b; 12 lload_2 // Push long from local vars 2 & 3 13 lstore_0 // Pop long into local vars 0 & 1: a = b; 14 lload 4 // Push long from local vars 4 & 5 16 lstore_2 // Pop long into local vars 2 & 3: b = fiboNum; 17 goto 7 // Jump back to offset 7: for (;;) {} The You may notice that Keep in mind that this manner of representing Although according to the best mathematical minds, the Fibonacci series does indeed go on forever, the To drive the Fibonacci Forever 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 Fibonacci Forever applet. |
Copyright © 1996-1999 Bill Venners. All Rights Reserved. |