Skip to content
systemcoding.dev
Mainframe

Compare and Branch With CLC, CLI and BRC in HLASM

Compare character fields with CLC and CLI, read the condition code, and branch with BC, BRC and the extended mnemonics like BE, JNE and JL in HLASM programs.

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

Decisions in assembler are two steps: set the condition code with a compare, then branch on it. There is no IF. That makes the logic explicit and gives you full control, and it is also where most beginner bugs live, because the branch must test the condition code that the compare set and not one left over from something else. This post covers the character compares CLC and CLI, how the condition code encodes the result, the conditional branches BC and BRC with their extended mnemonics, and the pattern of a small decision table, with the output shown. It targets HLASM on z/OS 3.1, documented at the High Level Assembler documentation. It builds on the post on registers and instruction formats, and the hub is the complete guide to IBM High Level Assembler on z/OS.

Two compares for characters

CLC is Compare Logical Character. It compares two storage operands byte by byte, from the left, for a length you give, up to 256 bytes. The compare is logical, which means the bytes are treated as unsigned binary values, so in EBCDIC the letter A (C1) is less than B (C2) and any letter is less than any digit, because the digits are F0 to F9.

asm
         CLC   RECTYPE,=C'HDR'

The first operand is a field whose length the assembler takes from its definition, three bytes here. The second is a literal. If the lengths differ, you give an explicit length on the first operand with CLC RECTYPE(3),=C'HDR'.

CLI is Compare Logical Immediate. It compares a single byte in storage with a constant in the instruction itself:

asm
         CLI   RECTYPE,C'H'

CLI is smaller and faster than CLC with a one-byte literal, because the constant is part of the instruction and no literal pool entry is needed.

The condition code

Both compares set the condition code, a 2-bit value in the PSW, as follows:

Condition codeResult of the compare
0the operands are equal
1the first operand is lower
2the first operand is higher

Code 3 is not used by these compares. A conditional branch names which values it accepts through a 4-bit mask, one bit per value: mask bit 8 stands for code 0, bit 4 for code 1, bit 2 for code 2 and bit 1 for code 3. BC 8,target branches on equal, BC 4,target on low and BC 2,target on high. Add masks to accept several: 7 is every code but zero, so BC 7 is "not equal".

Branch instructions

BC is Branch on Condition, with an address built from a base register and displacement. BRC is Branch Relative on Condition, which takes a signed halfword offset from the branch itself, counted in halfwords, so it reaches 64 KB either way and needs no base register. The assembler lets you write the target as a label and works out the offset. Both are usually written with extended mnemonics that hide the mask:

MnemonicMaskBranches when
B / J15always
BE / JE8equal
BNE / JNE7not equal
BL / JL4low
BH / JH2high
BNL / JNL11not low
BNH / JNH13not high

The B-prefixed forms are BC with a mask, and the J-prefixed forms are BRC. Modern code prefers the J forms when the target is within range, because they need no base register and are shorter, 4 bytes instead of 4 as well but not tied to a USING. They still require that the target is in the same section within 64 KB, and the assembler reports an error if it is not.

A decision table

The program below classifies a record by its first byte and sets a return code. It uses CLI, CLC and several branches:

CLASSIFYasm
CLASSIFY CSECT
         YREGS
         STM   R14,R12,12(R13)
         LR    R12,R15
         USING CLASSIFY,R12
         CLI   REC,C'H'
         JE    ISHDR
         CLI   REC,C'T'
         JE    ISTRL
         CLC   REC(2),=C'D1'
         JE    ISDTL
         CLC   REC(2),=C'D2'
         JE    ISDTL
         LA    R2,8
         J     EXIT
ISHDR    LA    R2,1
         J     EXIT
ISTRL    LA    R2,2
         J     EXIT
ISDTL    LA    R2,3
EXIT     ST    R2,RCODE
         LM    R14,R12,12(R13)
         L     R15,RCODE
         BR    R14
RCODE    DS    F
REC      DC    CL80'D2 ORDER 001'
         END   CLASSIFY

Reading it: if the first byte is H the record is a header, if T a trailer, and if the first two bytes are D1 or D2 a detail. Anything else is code 8. The code is built in register 2 and parked in the storage word RCODE before the registers are restored. The LM R14,R12,12(R13) reloads registers 14 through 12, including register 2 and register 15, from the caller's save area, so a return code left in either register would be overwritten. Reading it back with L R15,RCODE after the restore is the safe order, and it is the same rule as in the post on linkage conventions.

With REC holding D2 ORDER 001, the two CLI compares fail, CLC REC(2),=C'D1' is unequal, the compare with D2 is equal and control reaches ISDTL, so the step ends with return code 3. The job log shows:

Text
IEF142I USERID CLASSIFY - STEP WAS EXECUTED - COND CODE 0003

Labels can be up to 63 characters long in HLASM, but the opcode must still start after a blank, and long labels make the source hard to read in an 80-column member. Keep them short, and keep a comment line above each decision.

Comparing registers and numbers

For binary numbers the compare instructions are C (fullword in storage), CR (two registers), CH, CHI (register and a 16-bit immediate) and CL/CLR for logical, unsigned compares. The condition code values are the same, 0 equal, 1 low and 2 high. The signed or unsigned choice matters when the top bit is set: as signed numbers X'FFFFFFFF' is -1 and low, as unsigned it is the largest value and high. Use the logical forms for addresses and the signed forms for arithmetic.

What sets the condition code

Not every instruction sets it. Compares do, and most arithmetic does, and L, ST, LR and LA do not, which is why you can slip a load between a compare and its branch without disturbing the result. Instructions that do set it will overwrite your compare. Put the branch immediately after the compare, or at least check that nothing between them changes the code. When you read someone else's listing and a branch looks wrong, the first question is which instruction set the condition code last.

Loops with BRCT and BCT

A count-controlled loop uses the compare-free forms. BCT R3,LOOP subtracts one from register 3 and branches if the result is not zero, and BRCT is the relative form. They save a compare and are the standard way to write a fixed-count loop in assembler.

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.