The Eternal Math applet, included below, simulates a Java virtual machine executing a few bytecode instructions. This applet accompanies Chapter 5, "The Java Virtual Machine, " of Inside the Java 2 Virtual Machine.
The instructions in the simulation were generated by the // On CD-ROM in file jvm/ex4/Act.java class Act { public static void doMathForever() { int i = 0; for (;;) { i += 1; i *= 2; } } } Here are the bytecodes generated by 0 iconst_0 // Push int constant 0. 1 istore_0 // Pop into local variable 0: int i = 0; 2 iinc 0, 1 // Increment local variable 0 by 1: i += 1; 5 iload_0 // Push int in local variable 0 6 iconst_2 // Push int constant 2 7 imul // Pop two ints, multiply, push result 8 istore_0 // Pop into local variable 0: i *= 2; 9 goto 2 // Jump unconditionally to 2: for (;;) {} The instructions in the simulation represent the body of the The applet has four buttons: Step, Reset, Run, and Stop. Each time you press the Step button, the Java virtual machine simulator will execute the instruction pointed to by the pc register. Initially, the pc register points to an The value of each register (pc and optop) is shown two ways. The contents of each register, an integer offset from the beginning of either the method's bytecodes or the operand stack, is shown in an edit box. Also, a small arrow (either "pc>" or "optop>") indicates the location contained in the register. In the simulation the operand stack is shown growing down the panel (up in memory offsets) as words are pushed onto it. The top of the stack recedes back up the panel as words are popped from it. The With enough patience and clicks of the Step button (or a long enough run of the Run button), you can get an arithmetic overflow. When the Java virtual machine encounters such a condition, it just truncates, as is shown by this simulation. It does not throw any exceptions. 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 Eternal Math applet. |
Copyright © 1996-1999 Bill Venners. All Rights Reserved. |