This is the first assembly language tutorial.
A constant is a memory location whose value cannot be changed during the execution of the program. In IJVM, constants are global. They can be accessed by any method. Constants are declared and initialized in the constant section of an assembly language program:
General Format | Example |
.constant id1 value1 id2 value2 ... .end-constant |
.constant one 1 a 'a' .end-constant |
Constant identifiers begin with a letter and consist of letters, numerals, and underscore characters. Constant values are decimal integers or a single character enclosed by apostrophes. The value of a character is its code value. All constant values are stored as 4-byte signed integers.
The only instruction that can access a constant is LDC_W which loads (copies) a constant value onto the top of the stack. In an assembly language program, its operand is a constant identifier:
General Format | Example |
ldc_w constId | ldc_w one |
Constants are stored in the constant pool; the block of memory pointed to by the CPP (constant pool pointer) register. The constants are stored in the order in which they are declared in the source code.
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. This
program defines two constants and then loads them onto the stack.
.constant
one 1
a 'a'
.end-constant
.main
ldc_w one
ldc_w a
halt
.end-main
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. Go to
the computer simulator window and click on the Goto CPP button
below the memory window. Notice the the first constant value is 1 and the second
is 97 (the character code for 'a'):
6.
Make sure that the machine language interpreter ("ijvm.mic") is currently loaded
into the control store. Check
the Single-Step through IJVM Code checkbox below the input/output window.
7. Click
the Reset button.
8. Click
the IJVM Step button. The value currently in the MBR is the opcode
of the next instruction. Since the MBR is reset to zero when the computer is
reset, the next instruction is a NOP.
9. Click
the IJVM Step button. The NOP instruction has been executed and the next
instruction is the first LDC_W.
10. Click
the IJVM Step button. The first LDC_W instruction has been executed and the next
instruction is the second LDC_W. The constant "one" (whose value is 1) has been
loaded onto the top of the stack:
11. Click
the IJVM Step button. The second LDC_W instruction has been
executed and the next instruction is HALT. The constant "a" (whose value is 'a'
or 97) has been loaded onto the top of the stack:
12. Click
the IJVM Step button. The HALT instruction has been executed and
the program has terminated.
The next tutorial covers assembly language variables.