Subroutines

  1. Internal Subroutine

  2.  

  3. External Subroutine

Internal Subroutines

On entrance to a subroutine, register 1 holds the address of a parameter list (if the subroutine gets passed any parameters)

On entrance to a subroutine, the initial value in any register that will be altered by the subroutine should be saved

Before exiting a subroutine, the initial value in any register that was altered by the subroutine should be restored

Subroutine Name: rtnName DS 0H

Setting up a parameter list:

A sample parameter list for a program with internal subroutines:

PARMLIST   DC   A(TABLE)
           DC   A(EOT)

 

To call a subroutine:

  1. If the subroutine requires passed in parameters, put the address of the parameter list into register 1

  1. Use the Branch and Link Instruction

  2.  

LA    1,PARMLIST
BAL   11,RTNNAME

 

To exit a subroutine:

  1. Branch to the instruction immediately following the BAL instruction by executing a BR instruction with the register used in the calling routine BAL instruction

BR    11

 

Skeleton program with one internal subroutine:

MAIN     CSECT
         USING MAIN,R15
         STM   R0,R15,MAINSAVE
         ...
         BAL   R14,SUBRTN
         ...
         LM    R0,R15,MAINSAVE
         BR    R14
         LTORG
**** Storage for MAIN starts here ***
MAINSAVE DS    16F
         ...
*** Storage for MAIN ends here ***
SUBRTN   DS    0H                 SUBRTN starts here
         STM   R0,R15,SUBSAVE
         ...
         LM    R0,R15,SUBSAVE
         BR    R14
         LTORG
*** Storage for SUBRTN starts here ***
SUBSAVE  DS    16F
         ...
*** Storage for SUBRTN ends here ***
         END   MAIN