Arithmetic Expression Practice Exercise

This exercise is intended to help you understand the process of turning a formula into assembly-language code.

It is not a homework assignment and will not be graded.


You need to write a program.

  1. Declare some variables:
    NUM1     DC    F'317'
    NUM2     DC    F'-9'
    NUM3     DC    F'40'
    NUM4     DC    F'18'
    NUM5     DC    F'3'
    RESULT   DS    F

  2. Suppose we want to do a calculation:
        RESULT = (NUM1 - NUM2) / NUM4 + NUM3 * NUM5

    As this is binary arithmetic, we will need to use some registers. As multiplication and division are both involved, we will need at least 4 registers, 2 for each operation.

    You may want to insert XDUMP into the code here and there to watch what is happening to the values in the registers.

  3. We will start with the part on the left:
             L     5,NUM1              5 now contains the number 317
             S     5,NUM2              Subtract
             M     4,=F'1'             Extend the sign to a 64-bit value
             D     4,NUM4              Divide

    We now have the quotient in register 5. Notice we had to plan ahead, as division requires an even-odd pair of registers.

  4. Now let's continue with the part on the right:
             L     7,NUM3              7 now contains the number 40
             M     6,NUM5              Multiply

    The product is now in registers 6 and 7, 64 bits long. As the numbers are small, only the part in register 7 is significant.

  5. We now need to combine the two parts and store the value:
             AR    5,7                 Add
             ST    5,RESULT            Store the sum

  6. Now you can write the code to evaluate this expression as well:
        RESULT = NUM1 / (NUM2 + NUM3) + NUM4 / NUM5