This tutorial is intended to follow the tutorial on assembly language arithmetic.
The IJVM instruction set includes four branching statements. One of these (GOTO) is an unconditional branch and the other three (IFEQ, IFLT, and IF_CMPEQ) are conditional branches. Each must be followed by a program label that is defined in the same method as the branching instruction itself.
General Format | Examples |
goto label ifeq label iflt label if_cmpeq label |
goto top ifeq top iflt top if_cmpeq top |
The GOTO is an unconditional branch (it is always successful).
The IFEQ branch is successful if the value at the top of the stack is equal to zero. IFEQ pops the top element from the stack and sends it to the ALU in order to set the Z flag. If the flag is set (Z=1 means the value is equal to 0) then the branch is successful; otherwise the branch fails.
The IFLT branch is successful if the value at the top of the stack is less than zero. This instruction pops the top element from the stack and sends it to the ALU in order to set the N flag. If the flag is set (N=1 means the value is less than 0) then the branch is successful; otherwise the branch fails.
The IF_ICMPEQ branch is successful if the two top values on the stack are equal. This instruction pops the top two elements from the stack, subtracts them, and checks the Z flag. If the flag is set (Z=1 means the difference is 0) then the branch is successful; otherwise the branch fails.
The operand for each of these branching instructions is a two-byte signed offset. If the branch is successful, this offset is added to the byte address at which the instruction opcode was found and the sum is placed into the PC as the address of the next opcode in the instruction stream. Otherwise, the PC is incremented by two in order to skip past the two-byte operand.
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 implements integer multiplication using repeated addition.
.main .var a b product temp .end-var // a = 20 // b = 10 bipush 20 istore a bipush 10 istore b // product = a*b bipush 0 // product = 0 istore product iload b // temp = b istore temp top: // while (temp != 0) { iload temp ifeq end iload product // product = product + a iload a iadd istore product iinc temp -1 // temp = temp - 1 goto top // } end: halt .end-main
4.
Assemble the program (tap the F2 function key). If you do not want to
save this program, 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 two (2) times to allow the machine language interpreter to perform its start-up
routine.
9.
Click the IJVM Step button four (4) times and then click the
Goto LV button. The values of "a" and "b" have been initialized at 20 and 10 respectively:
9.
Click the IJVM Step button four (4) more times. The product has
been initialized at zero and the value of "b" has been copied to "temp":
10. Click
the IJVM Step button two (2) times (up to and including the IFEQ
instruction). The branch failed (the next instruction is
21: ILOAD 2). If the branch had succeeded,
the next opcode would have been at byte-address 34.
11. Click
the IJVM Step button six (6) times (up to and including the
31: GOTO 16
instruction). Note that after the first pass through the loop, "product" is 20
and "temp" is 9:
11. Click
the IJVM Step button eight (8) times (up to and including the
31: GOTO 16
instruction). Note that after the second pass through the loop, "product" is 40
and "temp" is 8:
12. Click
the Run button and let the program run until it terminates (when
"temp" reaches 0). Click the Goto LV button. The value of "product" is 200:
13. Click
the Go to: button immediately below the memory window to go to
address 0. Click the Display
Bytes button. Change the contents of byte address 1 (the operand of
the first BIPUSH instruction) from 20 to 40. Click the Reset
button and the Run button. When the program terminates,
click the Goto LV button and confirm that the product (at offset
two) is now 400.
14. Click
the Go to: button immediately below the memory window to go to
address 0. Click the Display
Bytes button. Change the contents of byte address 5 (the operand of
the second BIPUSH instruction) from 10 to 40. Click the Reset
button and the Run button. When the program terminates (it
may take awhile), click the Goto LV button and confirm that the
product (at offset two) is now 1600.
14. Check
the checkbox labeled Turn Off Data Path Animation. Make sure that
memory is displaying words and then click the Reset button and the
Run button. The program should execute more quickly with
data path animation turned off. You will still be able to see the memory window
updated as the program runs.
15. Check
the checkbox labeled Turn Off Memory Animation. Click the Reset
button and the Run button. The program should run even more
quickly with both animation features turned off. Only the registers are updated
as the program executes.
Note: You can click on the checkboxes and memory display mode buttons even while the simulator is running your program.
The next assembly language tutorial covers assembly language input/output.