Assembly Language Arithmetic


This tutorial is intended to follow the tutorial on assembly language variables.

The IJVM instruction set provides three instructions that perform integer arithmetic. Two of these instructions (IADD and ISUB) pop the top two elements from the stack, perform the indicated arithmetic (addition and subtraction, respectively), and push the result onto the stack. The third instruction (IINC) increments a variable.

Integer multiplication can be implemented using repeated addition and integer division (and modulus or remainder) can be implemented using repeated subtraction. A crude multiplication algorithm is provided in the tutorial on branching.

 

IADD and ISUB

 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 declares two variables, initializes them, and finds their sum and difference.

.main
    .var
        a
        b
        a_plus_b
        a_minus_b
    .end-var

    // a = 25
    // b = 32

    bipush 25
    istore a
    bipush 32
    istore b

    // a_plus_b = a + b

    iload a
    iload b
    iadd
    istore a_plus_b

    // a_minus_b = a - b

    iload a
    iload b
    isub
    istore a_minus_b

    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. 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. This "primes the pump", so to speak, allowing the machine language interpreter to perform its start-up routine.

Initialize the Variables

 9. Execute the first four instructions by clicking the IJVM Step button four (4) times.

 10. Click the Goto LV button. Variable "a", at offset zero, has a value of 25 and variable "b", at offset 1, has a value of 32:

Find the Sum (a_plus_b = a + b)

 11. Click the IJVM Step button. Look at the memory window and confirm that 25 (the value of "a") is at the top of the stack (SP = 4097).

 12. Click the IJVM Step button. Look at the memory window and confirm that 32 (the value of "b") is at the top of the stack (SP = 4098).

 13. Click the IJVM Step button. Look at the memory window and confirm that the top two elements have been popped from the stack and their sum is now at the top of the stack (SP = 4097 and points at the value 57).

 14. Click the IJVM Step button. The sum (57) has been popped from the stack (SP = 4096).

 15. Click the Goto LV button. The sum has been stored in a_plus_b at offset 2:

Find the Difference (a_minus_b = a - b)

 16. Click the IJVM Step button. Look at the memory window and confirm that 25 (the value of "a") is at the top of the stack (SP = 4097).

 17. Click the IJVM Step button. Look at the memory window and confirm that 32 (the value of "b") is at the top of the stack (SP = 4098).

 18. Click the IJVM Step button. Look at the memory window and confirm that the top two elements have been popped from the stack and their difference is now at the top of the stack (SP = 4097 and points at the value -7).

 19. Click the IJVM Step button. The difference (-7) has been popped from the stack (SP = 4096).

 20. Click the Goto LV button. The difference has been stored in a_minus_b at offset 3:

 21. Click the IJVM Step button. The HALT instruction has been executed and the program has terminated.

 

IINC

The IINC instruction increments a local variable. This instruction has two operands: a 1-byte unsigned offset and a 1-byte signed integer value. The offset is added to the value in the LV register to determine the location of the variable that is to be incremented. The signed integer is added to the value of that variable: Mem[LV + offset] = Mem[LV + offset] + value.

If the IINC instruction is preceded by the WIDE instruction, the first operand is a 2-byte offset. This larger offset makes it possible to address more than 256 variables.

When writing assembly code, the first operand must be a variable or parameter identifier defined within the method containing the IINC instruction. The second operand is a decimal integer in the range -128 to 127.

 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 declares two variables, initializes them, increments the first by a positive amount, and increments the second by a negative amount.

.constant
    initialValue 20
.end-constant

.main
    .var
        a
        b
    .end-var

    // a = initialValue
    // b = initialValue

    ldc_w initialValue
	dup
	istore a
    istore b

    // a = a + 10

    iinc a 10

    // b = b - 10 = b + (-10)

    iinc b -10

    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 allowing the machine language interpreter to perform its start-up routine.

Initialize the Variables

 9. Click the IJVM Step button. The initialValue constant (20) is at the top of the stack.

 10. Click the IJVM Step button. The value at the top of the stack has been duplicated; the top two elements on the stack are both 20.

 11. Click the IJVM Step button two (2) times. The top two values have been popped from the stack.

 12. Click the Goto LV button. Variable "a" (at offset 0) and variable "b" (at offset 1) both have a value of 20.

Positive Increment: a = a + 10

 13. Click the IJVM Step button. The value of "a" at offset zero has been incremented by 10:

Negative Increment: b = b + (-10)

 14. Click the IJVM Step button. The value of "b" at offset one has been incremented by -10:

 15. Click the IJVM Step button. The HALT instruction has been executed and the program has terminated.

The next tutorial covers assembly language branching.