Subroutines
Internal Subroutine
subroutine that is located within the same CSECT as the calling routine
External Subroutine
Subroutine that is located outside of the calling routine
A separate CSECT
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:
must be a set of contiguous fullwords, each of which contains the address of a parameter to be passed
use address constants (adcons): label DC A(expr)
if expr is a non-negative integer, the generated fullword will contain the binary representation of the integer (same as doing F‘expr’)
if expr is a label or label+n, the generated fullword will contain the address of label or address of label+n
DC A(5) => 00000005
000100 SAVE DS F
DC A(SAVE) => 00000100
expr may be several non-negative integers or label separated by commas
DC A(5,SAVE) => 0000000500000100
A sample parameter list for a program with internal subroutines:
PARMLIST DC A(TABLE) DC A(EOT)
To call a subroutine:
If the subroutine requires passed in parameters, put the address of the parameter list into register 1
Use the Branch and Link Instruction
RX Format: label BAL R,D(X,B)
LINK: The address of the instruction immediately following the BAL instruction is placed in R
BRANCH: Branches to D(X,B), which is usually rtnName
LA 1,PARMLIST BAL 11,RTNNAME
To exit a subroutine:
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