In this example, a primary mode target program receives control from a calling program that provided a 144-byte doubleword-aligned save area pointed to by 64-bit GPR 13. The calling program can make the call through the following two instructions:
L 15,=V(PGM)
BASR 14,15
The target program saves its calling program's registers into the save area that the calling program provides. It uses the GETMAIN macro to obtain storage for its own save area. The code is in 64-bit addressing mode and is reentrant.
PGM CSECT
PGM AMODE 64
PGM RMODE 31
SYSSTATE AMODE64=YES
STMG 14,12,SAVF4SAG64RS14-SAVF4SA(13)
* Save caller's registers in caller-
* provided R13 save area
CNOP 0,4
BRAS 12,*+8
DC A(STATIC_DATA)
L 12,0(12,0) Set up to address of static data
USING STATIC_DATA,12
GETMAIN RU,LV=144 Get my reentrant savearea
STG 13,SAVF4SAPREV-SAVF4SA(,1)
* Save caller's savearea address in my
* savearea (backward chain)
STG 1,SAVF4SANEXT-SAVF4SA(,13)
* Save my savearea address in caller's
* savearea (forward chain)
MVC SAVF4SAID-SAVF4SA(4,1),=A(SAVF4SAID_VALUE)
* Set ID into savearea to indicate how
* caller's regs are saved
LGR 13,1 Put my savearea address in R13
* End of entry code. Begin program code here . . .
⋮
* Begin exit code
LGR 1,13 Copy my savearea address
LG 13,SAVF4SAPREV-SAVF4SA(,13)
* Restore caller's savearea address
FREEMAIN RU,A=(1),LV=144 Free my reentrant savearea
SLR 15,15 Set return code of zero
LG 14,SAVF4SAG64RS14-SAVF4SA(,13)
* Restore caller's R14
LMG 2,12,SAVF4SAG64RS2-SAVF4SA(13)
* Restore caller's R2-R12
BR 14 Return to caller
⋮
STATIC_DATA DS 0D
* Begin static data here
IHASAVER
END