Example of an Internal Subroutine

Here we have an internal subroutine called ABS which will compute the absolute value of an integer. It is relatively simple.

ABS has one input parameter, N, and one output parameter, P. The only problem to address is overflow. Why could we have overflow? Suppose the value of N is -2^32, the largest available negative integer. Its absolute value should be +2^32, but that value is too large and we have overflow. In the event of overflow, we will return the value P = -1.


In the main program, we have the following in storage:

N         DC    F'-37'
P         DS    F
PLIST     DC    A(N)           Parameter list
          DC    A(P)

and in its executable code we have:

          LA    1,PLIST        Reg.1 : address of PLIST
          BAL   11,ABS         Branch to ABS

The code for ABS looks like this:

ABS       DS    0H
          STM   2,4,ABSAVE     Save the registers
          LM    2,3,0(1)       Unload the parameters
          L     4,0(2)         Now reg. 4 contains N
          LPR   4,4            Make it >= 0 
          BNO   DONE           If not overflow, skip ahead,
          L     4,=F'-1'       else set reg. 4 to -1
DONE      DS    0H
          ST    4,0(3)         Store the value of P
          LM    2,4,ABSAVE     Restore the registers 
          BR    11             Return to the caller
ABSAVE    DS    3F             Save area for ABS


After the call to ABS, the variable P should contain the value +37 and register 11 contains the address of the instruction after the BAL. Those should be the only effects it has. In particular, register 1 still contains the address of PLIST and registers 2 through 4 should have the values they had before the BAL instruction.

Notice that the parameter list is a list of addresses: register 1 contains the address of PLIST, which itself contains two addresses. After the LM line, register 2 contains the address of N and register 3 contains the address of P.

The label "ABS" is declared as "DS 0H". Here "H" indicated "halfword" and the 0 indicates we want to actual bytes allocated. As all our instructions are 2, 4 or 6 bytes long anyway, we are already on a halfword boundary. The effect is simply to associate the label "ABS" with an address (which in this case is also the address of the STM on the next line).

The documentation for ABS should list its name, the parameters it expects to have as input or output, and a list of register usage.