Build a Branch Table for Dispatching in HLASM
Dispatch on a function code in HLASM with a branch table: range-check and scale the index, then branch through B instructions or an address table.

A program that receives a function code and has to run one of several routines has to choose. In a high-level language that is an EVALUATE or a switch. In High Level Assembler (HLASM) on z/OS there is no such statement, and the usual answer is a branch table: an array of branch instructions, or of addresses, indexed by the function code. The table turns a choice among N routines into a single computed branch, with no chain of compares. It is how many system routines and exits dispatch, it is the thing you meet when you read an assembler dump, and the idioms around it, scaling the index, checking its range and keeping the code addressable, are the same ones that cause the abends diagnosed in the complete guide to z/OS abend codes and how to fix them. This post builds a complete dispatcher for z/OS 3.1 and High Level Assembler release 6, in the two common styles, and shows the checks that keep it safe.
The idea
A branch table is a sequence of fixed-size entries. Entry 0 is at the start of the table, entry 1 is one entry length later, and entry n is at the start plus n times the entry length. Given a function code n, the program computes n × entry length, adds it to the table's address, and branches there. If each entry is itself a branch instruction, the program lands on an instruction that sends it on to the routine. The two styles differ in the entry:
A table of branch instructions, each a 4-byte
Bto a routine, which the program enters by branching into the table.A table of addresses, each a 4-byte address constant, from which the program loads the target into a register and branches to it.
Style 1: a table of branch instructions
The function code is a number 0, 1 or 2 for add, subtract and multiply. The program reads it, scales it by 4, checks the range, and branches into the table.
DISPATCH CSECT
DISPATCH AMODE 31
DISPATCH RMODE ANY
STM R14,R12,12(R13) Save caller's registers
LR R12,R15 Base register = entry point
USING DISPATCH,R12
L R2,FUNCODE Function code, 0 to 2
C R2,=F'0' Below the first entry?
BL BADCODE
C R2,=F'2' Above the last entry?
BH BADCODE
SLL R2,2 Scale: each entry is 4 bytes
B TABLE(R2) Branch into the table
TABLE B DOADD Entry 0
B DOSUB Entry 1
B DOMUL Entry 2
DOADD WTO 'DISPATCH: ADD SELECTED'
B DONE
DOSUB WTO 'DISPATCH: SUBTRACT SELECTED'
B DONE
DOMUL WTO 'DISPATCH: MULTIPLY SELECTED'
B DONE
BADCODE WTO 'DISPATCH: INVALID FUNCTION CODE'
LA R15,8
B RETURN
DONE SR R15,R15
RETURN L R14,12(R13)
LM R0,R12,20(R13) Restore R0 to R12
BR R14
FUNCODE DC F'1'
LTORG
ENDThe register equates R0 to R15 come from a YREGS statement or are defined by the installation, and are omitted here for brevity: add YREGS before the CSECT. With FUNCODE set to 1, the run writes one message to the console and the job log, and ends with return code 0:
DISPATCH: SUBTRACT SELECTEDThe message shows in the job's message log, with the format of the leading characters depending on the system, and the job ends with a condition code of 0.
Read the code in three parts. The range check comes first: two compares reject any code below 0 or above 2 before it can reach the table. This is the safety of a branch table. Without the check, a function code of 10 branches 40 bytes past the table, into whatever instructions or data follow, and the result is an S0C1 operation exception or, worse, silent execution of the wrong code. The scaling is SLL R2,2, a shift left by two bits, which multiplies by 4, because each B instruction in the table is 4 bytes. The branch is B TABLE(R2), an unconditional branch to the address TABLE + R2. Execution lands on the n-th B instruction and branches again to the routine.
The STM and LM pair saves and restores the caller's registers in the caller's save area, the standard linkage, and BR R14 returns. The LTORG places the literals, =F'0' and =F'2', where they are addressable by R12, which holds the base.
The cost of the second branch
Style 1 executes two branches per dispatch: one into the table and one out of it. A branch is cheap, but on a hot path the extra one is noticeable, and the table must hold instructions, which restricts the entry size to what a B can reach: 4 bytes, within the range of the base register.
Style 2: a table of addresses
The second style stores 4-byte addresses of the routines and loads the target into a register:
L R2,FUNCODE
C R2,=F'0'
BL BADCODE
C R2,=F'2'
BH BADCODE
SLL R2,2 Scale to a 4-byte entry
L R15,ADDRTAB(R2) Load the routine's address
BR R15 Branch to it
ADDRTAB DC A(DOADD) Entry 0
DC A(DOSUB) Entry 1
DC A(DOMUL) Entry 2DC A(DOADD) assembles the address of DOADD into a fullword. The L R15,ADDRTAB(R2) loads it, and BR R15 goes there, a single branch to the routine with no second hop. The address constants are relocated by the program loader or the linkage editor, so the table stays correct wherever the module is loaded. An address in 31-bit mode has the high-order bit as the addressing-mode bit, which DC A() sets according to the AMODE of the module, so BR keeps the mode.
Style 2 is the common choice for a larger table or a hot path, and it allows entries that point to routines in other modules, using V(MODNAME) constants resolved at link time. Its cost is the extra data, 4 bytes per entry, and the care that the table holds addresses and not code.
Using a table with an extra symbolic layer
A function code of 0, 1, 2 is easy to get wrong once there are twenty of them. A common practice names the codes with equates, so that the table, the callers and the range check agree:
FC_ADD EQU 0
FC_SUB EQU 1
FC_MUL EQU 2
FC_MAX EQU 2 Highest valid codeand the range check uses FC_MAX. The table is then built to match, in the same order, and a comment on each entry restates its code. A more careful form computes the number of entries from the table itself, with ENTRIES EQU (*-ADDRTAB)/4, so adding a line to the table updates the limit.
ADDRTAB DC A(DOADD) FC_ADD
DC A(DOSUB) FC_SUB
DC A(DOMUL) FC_MUL
NENTRY EQU (*-ADDRTAB)/4 Number of entriesThe check then compares against NENTRY and branches if the code is not lower, unsigned, which also catches negative codes in one test. A negative code, as an unsigned number, is a large number, so CL R2,=A(NENTRY) followed by BNL BADCODE rejects both below zero and above the last entry:
CL R2,=A(NENTRY) Unsigned compare
BNL BADCODE Not lower: out of rangeCL is compare logical, an unsigned comparison, and that one pair of instructions replaces the two signed compares.
Entries that do nothing
A table with gaps, for codes that are reserved or unused, should send each gap to an error routine, not to zero. An entry of 0 in an address table makes BR branch to address 0, which abends with a protection exception or an operation exception. Fill every unused slot with DC A(BADCODE), so the unexpected code is handled the same way as an out-of-range one.
What goes wrong
Four failures account for most trouble with branch tables.
No range check. A function code out of range branches into data or into another routine. The abend is usually S0C1 or S0C4 at an address that makes no sense, and the clue is that the failing address is just past the table. Always check.
Wrong scaling. Scaling by 4 when the entry is 8 bytes, or scaling twice, lands in the middle of an entry. The program executes half an instruction. Define the entry length once, with an equate, and use it.
An addressability error. A branch into the table uses the base register to resolve TABLE, and a table placed more than 4,095 bytes from the base register's range needs another base register, or LARL and relative branches. The assembler reports an addressability error at assembly time as an ASMA307E message when it cannot resolve a reference. Do not ignore it.
Mode mix-ups. A branch table in a 31-bit module that is called from 24-bit code, or entries with an address that lacks the mode bit, can switch the program into the wrong addressing mode. Keep AMODE and RMODE consistent, and use BASSM and BSM where routines in different modes call each other.
Reading one in a dump
Branch tables show up in dumps of assembler programs. If a program abends with an S0C1 right after a branch through a table, find the table in the dump near the failing address, and check the index register: a value outside the table's length is the bug. If the program died in the routine, the register that held the scaled index at the branch, if it was not overwritten, shows which entry was taken, and dividing by the entry length gives the function code. For a COBOL program, the compiler generates similar code for GO TO DEPENDING ON, which uses a branch table, and an out-of-range value there falls through to the next statement and is not a failure, a behavior that is documented and different from the assembler idiom.
When not to use one
A table pays when there are many routines and the codes are dense, with no large gaps. For two or three choices, a pair of compares is clearer and as fast. For sparse codes, such as 10, 200 and 4000, a table wastes space or needs a search, and a table of code and address pairs with a loop, or a binary search, is the better design. And for code that will be maintained by people who do not read assembler, a COBOL EVALUATE is more readable, and the dispatch cost is rarely the bottleneck.
The instructions and the assembler language are described in the High Level Assembler Language Reference and the z/Architecture Principles of Operation on the IBM Documentation site.


