Example

In this example, a primary mode target program receives control from a calling program that provided a 72-byte save area pointed to by 32-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 31-bit addressing mode and is reentrant.

PGM   CSECT
PGM   AMODE  31
PGM   RMODE ANY
      STM    14,12,12(13)      SAVE CALLER'S REGISTERS IN CALLER-
*                              PROVIDED R13 SAVE AREA
      LR     12,15             SET UP PROGRAM BASE REGISTER
      USING  PGM,12
      GETMAIN RU,LV=72         GET MY REENTRANT SAVEAREA
      ST    13,4(,1)           SAVE CALLER'S SAVEAREA ADDRESS IN MY
*                              SAVEAREA (BACKWARD CHAIN)
      ST    1,8(,13)           SAVE MY SAVEAREA ADDRESS IN CALLER'S
*                              SAVEAREA (FORWARD CHAIN)
      LR    13,1               PUT MY SAVEAREA ADDRESS IN R13
*   END OF ENTRY CODE, BEGIN PROGRAM CODE HERE
⋮
*   BEGIN EXIT CODE
      LR    1,13               COPY MY SAVEAREA ADDRESS
      L     13,4(,13)          RESTORE CALLER'S SAVEAREA ADDRESS
      FREEMAIN RU,A=(1),LV=72  FREE MY REENTRANT SAVEAREA
      SLR   15,15              SET RETURN CODE OF ZERO
      L     14,12(,13)         RESTORE CALLER'S R14
      LM    2,12,28(13)        RESTORE CALLER'S R2-R12
      BR    14                 RETURN TO CALLER
      END