Curious Minds

Clear answers to science and history questions

z/OS CALL Macro: How It Works and When to Use It

· 7 min read · Science

The z/OS CALL macro is an assembler macro used in IBM mainframe assembly language programs to invoke a subroutine or program that follows standard operating system linkage conventions. It generates the instructions needed to pass control to another program, set up parameters, and return control to the caller. In practice, CALL is a convenient shorthand for the older BALR-based calling sequence, and it is part of the broader set of z/OS assembler macros that simplify system programming tasks.

Background: Why CALL Exists

In early System/360 and System/370 assembly language, programmers wrote subroutine calls by hand: load a base register, use BALR or BASR to branch and link, and manage save areas manually. This was error-prone, especially when programs needed to be reentrant or when they called routines in other load modules. IBM introduced structured macros like CALL, SAVE, and RETURN to standardize these sequences. The CALL macro, documented in IBM's z/OS Assembler Language Reference, generates a standard calling sequence that conforms to the linkage conventions used by most z/OS compilers and system routines.

The macro is not a function in the high-level-language sense; it is a code-generation tool. When you write CALL MYSUB, the assembler expands it into a series of machine instructions. The exact expansion depends on the operands you specify, such as whether you are calling a program in the same load module or an external module, and whether you need to pass parameters.

How the CALL Macro Works

The CALL macro produces a branch-and-link instruction (typically BALR or BASR) along with supporting code. The standard sequence involves:

  • Loading the address of the target routine into a register (often register 15).
  • Placing the address of the parameter list into register 1.
  • Issuing a branch-and-link to the target, which saves the return address in register 14.
  • The called routine then establishes its own save area and processes the parameters.

When you use the CALL macro, you can specify a parameter list using the PLIST operand or let the macro build one from a list of addresses. For example:

         CALL  MYPROG,(PARM1,PARM2),VL

This generates code to call MYPROG with a variable-length parameter list containing the addresses of PARM1 and PARM2. The VL operand indicates that the last parameter address has the high-order bit set, a convention for variable-length lists.

The macro also handles the details of loading the entry point address. If the target is in the same load module, the assembler can resolve the address at assembly time; if it is external, the linkage editor or binder resolves it, and the program fetches the address from the external symbol's V-constant (a fullword containing the entry point address).

How to Choose: CALL vs. Other Linkage Methods

Assembly programmers have several ways to transfer control to another routine. The choice depends on the environment and requirements:

  • CALL macro: Generates a standard OS linkage sequence. Use it when calling programs that expect register 1 to point to a parameter list and register 15 to hold the entry point address. It is the most portable choice for routines that follow IBM's standard conventions.
  • BALR/BASR directly: Gives full control but requires you to manage registers, save areas, and parameter lists manually. Useful for small, local subroutines where overhead matters or when you need non-standard conventions.
  • LINK macro: Similar to CALL but also manages save areas and can pass parameters more flexibly. LINK is often used for calling programs that may not be reentrant or that need a new save area.
  • ATTACH/XCTL: For invoking separate tasks or programs in a multitasking environment. These are heavier-weight and used when you need a new task or address space.

In modern z/OS environments, most high-level languages (COBOL, PL/I, C) generate their own calling sequences, and assembler routines that interface with them must follow the appropriate conventions. The CALL macro remains relevant for assembler programs that call other assembler routines or system services that use standard OS linkage.

Common Problems and Pitfalls

Even though CALL simplifies calling, several issues can arise:

  • Parameter list mismatch: The called routine expects a specific number and format of parameters. If the caller passes a different list, the routine may abend or produce incorrect results. Always verify the documented interface.
  • Register preservation: The CALL macro does not automatically save registers other than the return address. The called routine is responsible for saving and restoring any registers it uses, per standard conventions. If the called routine is not reentrant or does not follow conventions, registers may be corrupted.
  • Save area management: CALL does not allocate a save area. The caller must provide one, typically via the SAVE macro or by defining a save area in the program. The called routine uses the save area pointed to by register 13. If register 13 is not set correctly, the program may abend with a protection exception or storage violation.
  • AMODE/RMODE considerations: In 64-bit or mixed-mode environments, the CALL macro may generate code that assumes 31-bit addressing. For programs running in AMODE 64, you may need to use CALL with the appropriate options or use BASR with 64-bit registers. IBM's documentation provides guidance on calling conventions in different addressing modes.
  • External symbol resolution: If the target program is in a separate load module, the binder must resolve the external reference. If the module is not available at run time, the program will fail with a load error. Ensure that all called modules are included in the load library or link pack area.

Comparisons: CALL and Other Macros

The CALL macro is often compared with the LINK and LOAD macros. LINK is similar but includes additional functionality: it can manage save areas and provides a more complete interface for calling programs that may not be reentrant. However, LINK is older and less flexible in some respects. CALL is generally preferred for new assembler code because it generates more efficient code and is easier to use with modern linkage conventions.

The SAVE and RETURN macros are complementary to CALL. SAVE creates a save area and stores the caller's registers; RETURN restores them and returns control. A typical subroutine written in assembler uses SAVE at entry and RETURN at exit, while the caller uses CALL to invoke it. This combination ensures proper register and save area management.

In contrast, high-level languages use their own prologs and epilogs. For example, a COBOL program compiled with the standard calling convention expects a parameter list in register 1 and a save area in register 13, which is exactly what CALL provides. Thus, CALL is the natural choice for assembler routines that are called from or call COBOL programs.

Costs and Practical Considerations

The CALL macro itself is part of the z/OS assembler and does not incur additional cost beyond the standard software licensing. However, the way you use it can affect performance. Each CALL generates a few instructions, so the overhead is minimal. The bigger performance factors are the called routine's efficiency and the cost of loading external modules. If the called program is in the same load module, the call is a simple branch; if it is external, the system may need to load it on first use, which can add latency. For frequently called routines, consider linking them into the same load module or using a resident routine.

From a maintenance perspective, using CALL makes code more readable and portable than hand-coded BALR sequences. It also reduces the risk of errors in register and parameter setup. IBM's documentation provides detailed guidance on the macro's operands and usage, and many mainframe programming textbooks cover it as a standard technique.

Frequently Asked Questions

What is the difference between CALL and BALR?

CALL is a macro that generates a standard calling sequence, including loading the entry point address and setting up the parameter list. BALR is a single machine instruction that branches to an address in a register and saves the return address. CALL typically expands to include a BALR or BASR plus additional instructions, making it easier to use correctly.

Can I use CALL to invoke a program written in COBOL?

Yes, if the COBOL program is compiled with the standard calling convention (for example, using the NODYNAM or DYNAM options appropriately). The CALL macro sets up register 1 with the parameter list and register 15 with the entry point address, which matches what COBOL expects. However, you must ensure that the parameter list format matches the COBOL program's USING clause.

Does CALL automatically save registers?

No. The CALL macro does not save general-purpose registers other than the return address in register 14. The called routine is responsible for saving any registers it modifies, according to standard linkage conventions. The caller should provide a save area and set register 13 to point to it before issuing CALL.

What is the VL operand in the CALL macro?

VL stands for variable-length parameter list. When specified, the CALL macro sets the high-order bit of the last parameter address in the list. This tells the called routine that it has reached the end of the list, which is useful when the number of parameters varies.

Is the CALL macro still used in modern z/OS programming?

Yes, although high-level languages generate their own calls, assembler programs still use CALL for interfacing with system routines and other assembler modules. It remains part of the z/OS assembler and is documented in current IBM manuals.

Can I call a program in another address space with CALL?

No. CALL is for calling programs within the same address space. To invoke a program in another address space, you need to use cross-memory services or other inter-address-space communication mechanisms, such as ATTACH with the appropriate options or PC routines.

In this article
  1. Background: Why CALL Exists
  2. How the CALL Macro Works
  3. How to Choose: CALL vs. Other Linkage Methods
  4. Common Problems and Pitfalls
  5. Comparisons: CALL and Other Macros
  6. Costs and Practical Considerations
  7. Frequently Asked Questions