STM 14,12,12(13) Save GPRs @ entry
BALR 12,0
USING *,12
ST 13,SAVE+4 Save GPR 13(Save area of OS) in 2nd full word
LA 13,SAVE Passing save area address thru GPR 13 to SUB2
ST 13,SAVE+8 ( Is it required)
Call to SUB
.
.
.
L 13,SAVE+4 Retrieve OS save area address
LM 14,12,12(13) Restore GPRs
BR 14 Return to OS
SAVE DC 18Fâ0â
My Question:
1) ST 13,SAVE+8 ( Is it required)
Is it really required/optional, if optional what is the purpose of this and when will we use this?
2) Above code is main program, is it correct to say that we are saving GPRs @ entry into OS save area?
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
While it is technically not a requirement to set up save area chaining with the code you question, it is a very good idea to do so. Why? Because some system routines assume proper save area chaining exists. If an abend occurs and the save area chain is not set up, a second abend occurs (and overlays the first) indicating a save area chain corruption. Then you cannot find out about the original abend problem because of the save area chain abend.
Your exit routine does an LM 14,12,12(13) to "restore GPRs". While there is nothing intrinsically wrong with that instruction, it will destroy ( restore the original contents of ) GPR 15, which is designed to contain the RETURN-CODE of the module. When R15 does NOT contain a value of ZERO upon exiting a module, depending on how the module was invoked, it may cause problems. If the module was CALLED by another module, the CALLING module may interpret a non-zero return code as meaning that the CALLED module detected an error; if invoked by the OS ( via JCL ) it may result in the setting of a high return code, or even an abend, for the step.
The exit routing I use most looks like this:
Code:
EXIT L R13,DSAVE+4 RESTORE CALLING REGS
L R14,12(R0,R13)
L R15,RETCOD LOAD RETURN CODE
LM R0,R12,20(R13)
BR R14 RETURN TO CALLING PROGRAM
where DSAVE is my program's save area and RETCOD is a fullword in which I set the programs return-code ( zero if all went well ).
HELLOTSO START 0
* PRINT NOGEN
BEGIN SAVE (14,12)
LR 12,15
USING TYPE,12
ST 13,SAVE+4 (It is saving the previous SA address in the current SA ie called program)
LA 11,SAVE
ST 11,8(13) (It is saving the current SA in the Prevoius SA ie calling program)
LR 13,11
I got this code in allinterview.com. As per this code, It is saving the current SA in the Prevoius SA ie calling program.
I am confused like SAVE+8 should contain the Next save area address or Current save area address?
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
Register 13 points to the save area of the calling program. SAVE is the save area for the called program. Storing register 13 in SAVE+4 establishes the backwards chain from the called program save area to the calling program save area. Storing the address of SAVE at the address pointed to by register 13 + 8 bytes establishes the forwards chain from the calling program save area to the called program save area. If your program calls another program, THAT program will store its save area address in SAVE+8 -- otherwise it will be not be initialized.