Swing Operand Stack Demo: Pick a Method, Change Arguments, Watch Real Bytecode Run
I previously wrote "[Java] Visualizing the effects of iadd, isub, iconst_ instructions" (link), using Python + Tkinter to build a small window: seven iconst_ buttons push constants, iadd / isub pop and compute, and a canvas draws the stack from bottom to top. In that article, we could see how values enter and leave the operand stack, which is more intuitive than reading the stack transformation formulas in the specification.
This time, with CodeBuddy's help, I reworked it with Swing, reusing the drawing code I've accumulated in previous bytecode articles (frames, byte streams, control flow graphs, headless rendering). During the rewrite, I changed one deliberate interaction: the old version let users click instruction buttons to freely assemble sequences, which could easily mislead people into thinking bytecode can be assembled at runtime. But the instructions in a class file are fixed when compilation finishes; runtime cannot change them. So the new version becomes method selection: the window contains a set of precompiled small methods (add1, sub1, calc, etc.), each with a fixed instruction list, and the user can only change arguments, then use Step to watch instruction by instruction how the operations affect the stack. This way, you see the effects of iconst_, iadd, isub without being misled.
The old title carried [Java], but the code was actually Python (Tkinter); this version is real Java Swing.
Three instructions, three sentences
The JVM supports more than 200 bytecode instructions, most of which are straightforward. The old version picked a few of the most basic ones; the stack transformation formulas from section 6.5 of the spec are as follows:
iconst_<i> (seven instructions: iconst_m1, iconst_0, iconst_1, iconst_2, iconst_3, iconst_4, iconst_5) pushes an int constant from -1 to 5:
... → ..., <i>
iadd pops two ints from the top of the stack, adds them, and pushes the result back:
..., value1, value2 → ..., value1 + value2
isub also pops two, but pushes back the difference; the order must not be reversed:
..., value1, value2 → ..., value1 - value2
value2 is the top of the stack, value1 is the second from the top. For iadd, commutativity hides the difference; isub reveals it: 2 - 7 = -5, not 7 - 2 = 5.
The accompanying demo
The accompanying OperandStackDemo.java is a Swing window. After downloading, run it (requires JDK 16+ because it uses record; I used JDK 21 when writing this):
javac OperandStackDemo.java
java OperandStackDemo
The window is divided into three areas:
- Top left: ShowOps.java source, with the line for the current method highlighted
- Bottom left: The decoded instructions of that method, listing offset, raw bytes, mnemonics, and effect; the title says "decoded instructions — javac output, not editable"
- Right: The frame view, showing the local variable table and the operand stack (drawn from bottom to top), with depth and max_stack both labeled
The window is entirely in English, consistent with previous demos. Above the window are a method dropdown and argument input fields: after selecting a method, the instruction list is immediately fixed; the argument fields only allow changing arguments (default value 10, calc defaults to 2, 3, 4); click Apply or press Enter to re-run the simulation. Step executes one instruction at a time, Run auto-plays (speed adjustable), Reset returns to pc = 0. At the bottom is a status bar that explains in English what the current step did.
There is no "click buttons to assemble instructions" here — the instructions are compiled by javac, the demo just decodes them for viewing. This is the biggest difference from the old version.
The color convention is the same as previous articles: blue is the value pushed in this step, amber indicates a local variable read in this step. The roles of operands (value1 / value2) are read directly from their positions on the stack — the top is value2, the slot below is value1; no extra panel is needed to repeat this.
Where the methods come from
The dropdown contains 11 methods, all from source actually compiled by javac 21. They are written in one class:
class ShowOps {
int addM1(int a) { return a + (-1); }
int add0(int a) { return a + 0; }
int add1(int a) { return a + 1; }
int add2(int a) { return a + 2; }
int add3(int a) { return a + 3; }
int add4(int a) { return a + 4; }
int add5(int a) { return a + 5; }
int sub1(int a) { return a - 1; }
int sub2(int a) { return a - 2; }
int sub3(int a) { return a - 3; }
int calc(int a, int b, int c) { return a - (b + c); }
}
The output of javap -p -c -v ShowOps (excerpt for add3):
int add3(int);
descriptor: (I)I
flags: (0x0000)
Code:
stack=2, locals=2, args_size=2
0: iload_1
1: iconst_3
2: iadd
3: ireturn
Each addN / subN follows this four-instruction skeleton: iload_1, iconst_N, iadd / isub, ireturn. The only differences are the one byte for iconst_N and whether the last operation is addition or subtraction. The byte arrays in the demo are hardcoded from these listings; for example, add3 is 1B 06 60 AC, sub3 is 1B 06 64 AC.
calc is slightly longer, six instructions:
int calc(int, int, int);
descriptor: (III)I
flags: (0x0000)
Code:
stack=3, locals=4, args_size=4
0: iload_1
1: iload_2
2: iload_3
3: iadd
4: isub
5: ireturn
stack=3 is because the three iload instructions push all three arguments onto the stack before the arithmetic — this is the real origin of the old version's "push 2, 3, 4, then iadd, isub" example.
Walk through add3's four instructions
Select add3, keep argument a at 10, and step through:
Step 1 iload_1 (0x1B): pushes the value of local variable slot 1 (that is, a) onto the stack. The operand stack goes from empty to [10], depth = 1 / 2 (the operand stack's maximum capacity is 2 slots, now 1 is used), and the border of a's slot turns amber (read this step):
Step 2 iconst_3 (0x06): pushes constant 3 onto the stack. The spec says ... → ..., 3; the constant is embedded in the opcode, so one byte suffices — to push 100 you'd need bipush 100 (two bytes, mentioned in the fourth article). The stack becomes [10, 3], and 3 is newly pushed, drawn in blue:
Step 3 iadd (0x60): the spec gives ..., value1, value2 → ..., value1 + value2. The top slot 3 is value2, the slot below 10 is value1; both disappear, and the newly pushed 13 is drawn in blue. The stack goes from [10, 3] to [13], depth = 1 / 2:
Step 4 ireturn (0xAC): pops 13 and returns, method ends. At the bottom of the frame view, a green "returned 13 to the caller" appears; this is the result of add3(10).
The seven iconst_ instructions each have a corresponding addN method (addM1 through add5), covering iconst_m1 through iconst_5. Switching in the dropdown changes the second byte of the instruction listing from 02 to 08, leaving the other three lines unchanged — a difference of one byte changes the result from a-1 to a+5.
Use calc to see isub's order
The old version's most important issue was isub's order. calc(a, b, c) { return a - (b + c); } with default arguments a=2, b=3, c=4 exactly reproduces the old version's 2 - (3 + 4).
The first three steps iload_1, iload_2, iload_3 push 2, 3, 4 in order, filling depth = 3 / 3. Step 4 iadd pops the top 4 (value2) and the next 3 (value1), pushes back 7, and the stack becomes [2, 7]:
Step 5 isub: the top 7 is value2, the slot below 2 is value1, and it pushes back 2 - 7 = -5 — value1 - value2, not the reverse. The stack is left with [-5], depth = 1 / 3:
Change calc's arguments to a=7, b=2, c=0 and repeat, and the isub step becomes 7 - 2 = 5 — the bytecode hasn't changed a single byte, only the arguments, yet the result flips sign. This is exactly "instructions fixed, data variable": the compile-time part is the algorithm (a - (b+c)), and the runtime arguments decide what number is produced.
First difference from the old version: instructions also show bytes
The old Tkinter canvas only drew the stack; buttons only needed mnemonics. Since we already spent an article on "one instruction = 1 byte opcode + optional operand", this version labels each instruction with its opcode:
| Instruction | opcode | byte count | where the value/operand lives |
|---|---|---|---|
| iconst_m1 | 0x02 | 1 | value -1 encoded in opcode |
| iconst_0 | 0x03 | 1 | value 0 encoded in opcode |
| iconst_1 | 0x04 | 1 | value 1 encoded in opcode |
| iconst_2 | 0x05 | 1 | value 2 encoded in opcode |
| iconst_3 | 0x06 | 1 | value 3 encoded in opcode |
| iconst_4 | 0x07 | 1 | value 4 encoded in opcode |
| iconst_5 | 0x08 | 1 | value 5 encoded in opcode |
| iadd | 0x60 | 1 | no operand, values on stack |
| isub | 0x64 | 1 | no operand, values on stack |
| iload_1 | 0x1B | 1 | local variable slot number encoded in opcode |
| iload_2 | 0x1C | 1 | local variable slot number encoded in opcode |
| iload_3 | 0x1D | 1 | local variable slot number encoded in opcode |
| ireturn | 0xAC | 1 | pops top of stack and returns |
Each row in the bottom-left instruction panel has these bytes. For add3, the four rows are 1B, 06, 60, AC — four bytes corresponding to four instructions; for add5, only the second byte changes (06 → 08).
Second difference: max_stack is drawn
The Tkinter version only cared about what's on the stack now. Previous articles frequently showed stack= and locals= in javap output; I'm including max_stack here too: the stack panel's bottom shows depth = N / M, where M is the method's max_stack.
For addN / subN, M = 2 — after loading the local variable and the constant, the stack depth reaches 2, then iadd / isub pops two and pushes one, returning to 1. For calc, M = 3: three iloads push consecutively, and the stack reaches depth 3 before arithmetic starts.
max_stack is computed at compile time and written into the Code attribute; it does not change at runtime. Changing the argument from 10 to 1000000 leaves the depth trajectory identical and M unchanged — the type decides how many slots are occupied, the instruction sequence decides how the depth changes, independent of the actual numeric value. This is also why changing the interaction from "click instructions" to "select a method" is more honest: max_stack is tied to a fixed instruction sequence, and there's no way to talk about max_stack if you assemble instructions on the fly.
Practice: switch to sub3
Select sub3, change argument a to 5, try stepping through yourself first, then check:
- iload_1: pushes 5, stack [5]
- iconst_3: pushes 3, stack [5, 3]
- isub: value2 = 3 (top), value1 = 5 (second), pushes back 5 - 3 = 2
- ireturn: returns 2
Then change a to 1: the same three steps produce 1 - 3 = -2. The iconst_3 instruction hasn't changed, nor has isub; only the value loaded by iload_1 from local variable slot has changed.
Finally, change a to 10 and compare with add3: the two methods' instruction listings differ only in the third byte — add3 is 60 (iadd), sub3 is 64 (isub); 10 + 3 = 13, 10 - 3 = 7. A difference of one byte.
Summary
- iconst_ is a family of seven instructions (iconst_m1 to iconst_5) that encode -1 to 5 into the opcode, pushing a constant in one byte with no operand.
- iadd / isub pop two ints from the top and push back value1 + value2 / value1 - value2; value2 is the top, value1 is the second from the top.
- The order of subtraction cannot be reversed: isub computes second-from-top minus top, so 2 - 7 = -5, not 7 - 2.
- The operand stack grows from bottom to top: newly pushed values go to the top, arithmetic instructions only touch the top two values, and the stack depth decreases by one.
- Compile time determines the instruction sequence and max_stack; runtime changes only the arguments. The GUI lets users choose a method and change arguments, which matches this boundary — you see how instructions manipulate the stack without mistakenly thinking bytecode can be assembled live.
Compared to a pure "click buttons to see effects" version, this one adds a layer of realism: each method comes from real javap output, and the bytes, stack depths, and return values all align. Visualization cannot replace reading the spec, but it can turn the abstract phrase "pop two, push one" into three visible steps — provided you don't draw instructions as toys that can be clicked randomly.
References
- 用图形化界面演示 iadd, isub, iconst_ 指令的效果 — the author's old version, the starting point of this article
- The Java Virtual Machine Specification, Java SE 27 sections:
- Accompanying demo: OperandStackDemo.java