This tutorial is intended to follow the tutorial on assembly language constants.
A variable is a memory location whose value can be changed during the execution of the program. Variables are local to the method in which they are defined. There is no provision for global variables. Variables are declared in the variable section of a method:
General Format | Example |
.var variable list .end-var |
.var time speed distance .end-var |
Variables can also be declared in the parameter list of a method declaration:
General Format |
.method methodId (
paramList ) [variable declarations] executable code .end-method |
Variable identifiers begin with a letter and consist of letters, numerals, and underscore characters. Variable values are stored as 4-byte signed integers.
Three instructions access program variables: ILOAD, ISTORE, and IINC. ILOAD loads (copies) a variable to the top of the stack. ISTORE pops the top element from the stack and writes its value to the variable. They both have the same basic syntax:
General Format | Examples |
iload varId istore varId |
iload speed istore distance |
The IINC instruction increments a variable by adding a signed byte value (a decimal integer in the range -128 to 127):
General Format | Examples |
iinc varId byte | iinc n 1 iinc time -1 |
Variables are stored in a block of memory pointed to by the LV (local variables) register. Variables in the main method are stored in the section of memory addressed by the default value of the LV register. These values exist throughout the execution of the program though they can be accessed only by the main method.
When a method is invoked, a stack frame is created for its local variables (which includes its parameters). The LV register is adjusted to point to the base of this stack frame. When a method terminates, the previous value of the LV register is restored.
1. Clear
the memory of the computer (Memory menu; Clear Memory).
2. If
necessary, clear the source code editor (Source menu, Clear
Source Code).
3. Enter
the following assembly language program using the source code editor. It uses a
value-returning method to calculate the perimeter of a triangle. Note that it is
possible (though perhaps not wise) to use the same identifier for a variable and
for a method. You cannot, however, use the same identifier for a constant and a
method because, in IJVM, they are really both constants and are both stored in
the constant pool.
.constant OBJREF 0 .end-constant .main .var side1 side2 side3 perimeter .end-var // Initialize sides bipush 3 istore side1 bipush 4 istore side2 bipush 5 istore side3 // Call perimeter method ldc_w OBJREF iload side1 iload side2 iload side3 invokevirtual perimeter istore perimeter halt .end-main .method perimeter(a, b, c) iload a iload b iload c iadd iadd ireturn .end-method
4.
Assemble the program (tap the F2 function key). If you do not want to
save this program, just click the Cancel button in the file save dialog;
the machine language code will still be loaded into memory.
5. Check
the Single-Step through IJVM Code checkbox below the input/output window.
6. Click
the Reset button.
7.
Click the Display Words button below the memory display.
8.
Click the IJVM Step button twice. This "primes the pump", so to
speak, allowing the machine language interpreter to perform its start-up routine.
9.
Execute the first six instructions by clicking the IJVM Step button
six times. The IJVM Instruction box will contain
"10: ISTORE 2".
10.
Click the Goto LV button. You should see that the first three variables
have been initialized:
11.
Click the IJVM Step button five times executing the next five
instructions. The disassembled instruction in the IJVM Instruction box will read
"21: INVOKEVIRTUAL method at 27". The
memory display reveals that the LV register points at the stack frame created as
a result of the method invocation. (For more information on the stack frame see
INVOKEVIRTUAL.)
12.
Step through the first five instructions in the perimeter method
(up to but not including
IRETURN) noting how the stack changes following each
instruction. Notice that the perimeter (the return value of the method) is on
the top of the stack just prior to the execution of the IRETURN instruction:
13.
Step through the IRETURN statement. Note that the stack frame has been removed,
the method return value (12) is on top of the stack, and that previous value of
LV has been restored and, once again, points to the variables in the main method.
14.
Click the IJVM Step button to execute the ISTORE instruction
following INVOKEVIRTUAL in main. Click Goto LV and confirm that the return value of the perimeter
method was written to the perimeter variable (offset 3 from LV):
15. Click
the IJVM Step button. The HALT instruction has been executed and
the program has terminated.
The next tutorial covers assembly language arithmetic.