Modernization Hub

BAL - Branch and Link instruction

Enhanced Definition

`BAL` (Branch and Link) is an IBM System/360 (and subsequent architectures like z/Architecture) assembly language instruction used to transfer control to a specified address while simultaneously saving the address of the next sequential instruction in a designated general-purpose register. It is primarily employed for calling subroutines, allowing the called routine to return control to the instruction immediately following the `BAL` call.

Key Characteristics

    • Function: It performs two main actions: stores the address of the instruction immediately following the BAL in the first operand register, and then branches to the address specified by the second operand.
    • Register Usage: In standard z/OS linkage conventions, BAL R14, target_address is commonly used, where R14 (the Link Register) receives the return address, and target_address is the entry point of the subroutine. R15 often holds the entry point address of the called routine.
    • Subroutine Calls: BAL is fundamental for implementing modular programming through subroutines, enabling code reuse and structured program design in assembly language programs.
    • Program Linkage: It forms a core component of the z/OS program linkage conventions, facilitating the transfer of control between different program modules or subroutines.
    • Instruction Format: It is an RX (Register-and-Indexed-Storage) type instruction. Its register-to-register counterpart, BALR (Branch and Link Register), is an RR type instruction.

Use Cases

    • Calling Internal Subroutines: A common use is to call a local subroutine within the same program module, such as a routine to perform a specific calculation or data manipulation.
    • Invoking Common Service Routines: Programs often use BAL to branch to shared utility routines (e.g., for I/O operations, error logging, or data formatting) that are part of the same load module.
    • Implementing Control Flow: It can be used to implement complex control flow structures, where a section of code needs to be executed and then control returned to the point of call.
    • Error Handling: A BAL instruction might be used to branch to a specific error handling routine, with the return address saved to allow for potential recovery or logging before returning to a designated point.

Related Concepts

BAL is closely related to other branch instructions like B (Branch), BR (Branch Register), BAS (Branch and Save), and BASR (Branch and Save Register). While B and BR simply transfer control, BAL and BAS (and their register variants) additionally save the return address, making them suitable for subroutine calls. It is a cornerstone of assembly language programming on z/OS and is integral to the program linkage conventions, which define how programs pass control and data between modules, often involving the use of save areas and specific register assignments (e.g., R13 for save area pointer, R14 for return address, R15 for entry point).

Best Practices:
  • Adhere to Linkage Conventions: Always use R14 as the link register for saving the return address and R15 for the entry point address of the called routine, as per z/OS linkage conventions.
  • Pair with BR for Return: Ensure that every BAL call is eventually matched by a BR R14 (Branch Register using

Related Vendors

IBM

646 products

Broadcom

235 products

Trax Softworks

3 products

Related Categories