The Artima Developer Community
Sponsored Link

Eternal Math
A Simulation of the Java Virtual Machine


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.

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

The instructions in the simulation were generated by the javac compiler given the following code:

// On CD-ROM in file jvm/ex4/
class Act {

    public static void doMathForever() {
        int i = 0;
        for (;;) {
            i += 1;
            i *= 2;

Here are the bytecodes generated by javac for class Act:

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 doMathForever() method of class Act, shown above. This simulation shows the local variables and operand stack of the current frame, the pc register, and the bytecodes in the method area. It also shows an optop register, which you can think of as part of the frame data of this particular implementation of the Java virtual machine. The optop register always points to one word beyond the top of the operand stack.

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 iconst_0 instruction. The first time you press the Step button, therefore, the virtual machine will execute iconst_0. It will push a zero onto the stack and set the pc register to point to the next instruction to execute. Subsequent presses of the Step button will execute subsequent instructions and the pc register will lead the way. 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.

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 doMathForever() method has only one local variable, i, which sits at array position zero. The first two instructions, iconst_0 and istore_0 initialize the local variable to zero. The next instruction, iinc, increments i by one. This instruction implements the i += 1 statement from doMathForever(). The next instruction, iload_0, pushes the value of the local variable onto the operand stack. iconst_2 pushes an int 2 onto the operand stack. imul pops the top two ints from the operand stack, multiplies them, and pushes the result. The istore_0 instruction pops the result of the multiply and puts it into the local variable. The previous four instructions implement the i *= 2 statement from doMathForever(). The last instruction, goto, sends the program counter back to the iinc

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.

Sponsored Links

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