Skip to content
systemcoding.dev
Mainframe

Follow Standard z/OS Linkage Conventions With Save Areas

Write assembler programs that call and return the standard z/OS way, with registers on entry, an 18-word save area chain and a return code in register 15.

6 min read
Summarize in AI
Flat illustration of a 3270 terminal showing JCL beside a mainframe.

Assembler programs rarely run alone. A job step starts one, which calls another, which calls a COBOL routine, which calls a system service. For that to work, every program must follow the same rules about which registers carry what, where the caller's registers are saved and how a return code travels back. Those rules are the standard linkage conventions. They are not enforced by hardware, only by agreement, and breaking them produces the worst kind of bug: a program that works alone and fails when something calls it. This post lays out the conventions for z/OS 3.1 in 31-bit mode, builds a main program that calls a subroutine, draws the save area chain and shows how return codes flow. It uses HLASM, documented at the High Level Assembler documentation, and the conventions in the z/OS MVS Programming: Assembler Services Guide on the z/OS documentation site. It follows the post on base registers and the USING statement, and belongs to the cluster rooted at the complete guide to IBM High Level Assembler on z/OS.

The contract

When a program receives control through a standard call, the registers hold:

RegisterContent on entry
1the address of the parameter list, a list of addresses, or 0 if none
13the address of an 18-word save area provided by the caller
14the return address
15the entry point address of the called program

When the program returns, it must restore registers 0 through 14 to their values at entry, put a return code in register 15, and branch to the address in register 14. Register 1 and register 0 may be used as the program likes for a return value, but the common convention is a return code only in 15.

The save area

A save area is 18 fullwords, 72 bytes, owned by the program that is calling. The layout is fixed:

OffsetContent
0used by the language environment and PL/I; not used by simple programs
4address of the caller's save area (backward pointer)
8address of the called program's save area (forward pointer)
12register 14
16register 15
20register 0
24 to 68registers 1 through 12, in order

The first instruction of a called program saves the caller's registers into the caller's save area, at offset 12 from register 13, which is why STM R14,R12,12(R13) is the classic first line. Then the program creates its own save area and links the two together, so that any later routine, a debugger or a dump tool, can follow the chain backward from the current program to its callers.

A program that calls a subroutine

The main program below calls ADDTWO with two numbers and prints the sum. First the subroutine, which follows the standard entry and exit and uses a static save area:

ADDTWOasm
ADDTWO   CSECT
         YREGS
         STM   R14,R12,12(R13)
         LR    R12,R15
         USING ADDTWO,R12
         LA    R2,SAVE
         ST    R13,4(,R2)
         ST    R2,8(,R13)
         LR    R13,R2
         L     R3,0(,R1)
         L     R4,0(,R3)
         L     R5,4(,R1)
         A     R4,0(,R5)
         ST    R4,RESULT
         L     R13,4(,R13)
         LM    R14,R12,12(R13)
         LA    R15,0
         BR    R14
SAVE     DC    18F'0'
RESULT   DS    F
         END   ADDTWO

The first three lines are the entry from the previous posts. Then comes the save area work. LA R2,SAVE gets the address of this program's own save area. ST R13,4(,R2) stores the caller's save area address in the backward pointer. ST R2,8(,R13) stores this program's save area address in the caller's forward pointer. LR R13,R2 makes register 13 point at this program's area, so anything it calls will find it there.

The body reads its parameters through register 1, which points to a list of addresses. L R3,0(,R1) loads the address of the first number, L R4,0(,R3) loads the number itself, L R5,4(,R1) loads the address of the second number and A R4,0(,R5) adds it. At the exit, L R13,4(,R13) restores register 13 from the backward pointer, LM R14,R12,12(R13) restores the caller's registers, LA R15,0 sets the return code to zero, and BR R14 returns.

Notice the order: restore 13 first, then reload the registers from the caller's save area, then set 15, because the LM overwrites register 15 with the value saved at entry. If you want to return a nonzero code, set register 15 after the LM.

The calling side

A main program calls with a parameter list and a branch. The CALL macro builds both:

MAINPROGasm
MAINPROG CSECT
         YREGS
         STM   R14,R12,12(R13)
         LR    R12,R15
         USING MAINPROG,R12
         LA    R2,MYSAVE
         ST    R13,4(,R2)
         ST    R2,8(,R13)
         LR    R13,R2
         CALL  ADDTWO,(NUM1,NUM2),VL
         LTR   R15,R15
         BNZ   FAILED
         WTO   'ADDTWO RETURNED ZERO',ROUTCDE=11
FAILED   DS    0H
         L     R13,4(,R13)
         LM    R14,R12,12(R13)
         BR    R14
MYSAVE   DC    18F'0'
NUM1     DC    F'40'
NUM2     DC    F'2'
         END   MAINPROG

CALL ADDTWO,(NUM1,NUM2),VL expands to code that loads the address of each parameter into a list in storage, puts the list address in register 1, loads register 15 with the entry point of ADDTWO and branches with BALR R14,R15. The VL operand sets the high-order bit of the last address in the list, a marker that tells the called program where the list ends. Because the call is by name, the linker must resolve ADDTWO, which means including its object module in the link-edit step. LTR R15,R15 tests the return code, and BNZ branches if it is not zero.

When the program runs, the console shows the message and the step ends with condition code 0000. The WTO text is the only output.

Reading the chain

The save area chain is the reason dumps are readable. In a dump taken inside ADDTWO, register 13 points at ADDTWO's save area. Its offset 4 holds the address of MAINPROG's save area, whose offset 4 points to the system's save area for the step. Each area's offset 12 onward holds the registers of the caller as they were at the call, so a dump analyst can see what each routine was doing when it made its call. This is why programs should keep the backward pointer, even when no one else will use the forward one: it is the trail of breadcrumbs.

What the conventions do not cover

They say nothing about which registers a routine may change: all of 0 to 14 must be restored, so you can use any of them freely inside as long as the exit puts them back. They also do not describe how data is passed: addresses in a list is the general case, and the called and calling programs agree on the meaning of each parameter. Fixed-format programs written in COBOL, PL/I or C follow the same rules, which is why an assembler subroutine can serve all of them. Language Environment programs use a different save area format for the dynamic storage area, and 64-bit and XPLINK programs use their own conventions, which the post on 64-bit instructions touches.

Reentrancy is a different problem

The save area above is part of the program, static data. If two tasks ran the same copy of the program at once, they would share the one save area and corrupt each other. Programs that must be reentrant obtain the save area in dynamically allocated storage instead, which the post on reentrant programs and the one on STORAGE OBTAIN describe. For batch programs that run in one task, the static area is sufficient, and it is the right form to learn first.

Also inAssembly
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.