Example

In this example, an AR mode target program receives control from a calling program that provided a 216-byte doubleword-aligned save area pointed to by GPR 13.

The target program saves the calling program's registers (GPRs and ARs) into the save area that the calling program provides. It uses the STORAGE OBTAIN macro to obtain storage for its own save area. The code is in 31-bit addressing mode and is reentrant.

PGM CSECT
PGM AMODE 31
PGM RMODE 31
       SYSSTATE AMODE64=NO,ASCENV=AR
       STMG 14,12,SAVF7SAG64RS14-SAVF7SA(13) 
*                               Save caller's registers in caller-
*                               provided R13 save area
       STAM 14,12,SAVF7SAAR14-SAVF7SA(13) Save caller's ARs
       IAC  2                   Save caller's ASC mode
       SAC  512                 Make sure we are in AR mode
       LARL  12,STATIC_DATA     Set up to address of static data
       LAE   12,0(12,0)         Make sure AR12 is 0
       USING STATIC_DATA,12
       STORAGE OBTAIN,LENGTH=216 Get my reentrant savearea
       STG  13,SAVF7SAPREV-SAVF7SA(,1) 
*                               Save caller's savearea address in my
*                               savearea (backward chain)
       ST    2,SAVF7SAASC-SAVF7SA(,1) Save caller's ASC mode
       STAM  13,13,SAVF7SAAR13-SAVF7SA(1) Save caller's AR13
       STG 1,SAVF7SANEXT-SAVF7SA(,13) 
*                               Save my savearea address in caller's
*                               savearea (forward chain)
       MVC  SAVF7SAID-SAVF7SA(4,1),=A(SAVF7SAID_VALUE)
*                               Set ID into savearea to indicate how
*                               caller's regs are saved

       LAE 13,0(0,1)            Put my savearea address/ALET in R13
*    End of entry code. Begin program code here . . .
⋮
* Begin exit code
       LGR 1,13                 Copy my savearea address
       LAM 2,2,SAVF7SAAR13-SAVF7SA(13) Fetch caller's AR13
       L   2,SAVF7SAASC-SAVF7SA(,13)  Fetch caller's ASC mode
       LG  13,SAVF7SAPREV-SAVF7SA(,13)  
*                               Restore caller's savearea address
       STORAGE RELEASE,ADDR=(1),LENGTH=216 Free my savearea
       SLR   15,15              Set return code of zero
       SAC   0(2)               Restore caller's ASC mode
       CPYA  13,2               Restore caller's AR13
       LAM   14,12,SAVF7SAAR14-SAVF7SA(13) Restore caller's ARs
       LG    14,SAVF7SAG64RS14-SAVF7SA(,13) Restore caller's R14
       LMG   2,12,SAVF7SAG64RS2-SAVF7SA(13) Restore caller's R2-R12
       BR    14                 Return to caller
⋮
       STATIC_DATA DS 0D
* Begin static data here
       IHASAVER
       END