Home > Coding, Mainframe > Return Codes…

Return Codes…

Ever wonder why return codes are usually multiples of 4? Well if so (and even if not), here’s the answer.

On IBM mainframes, return code handling after calling a sub routine in assembler is typically done using a branch table that is indexed into using the value in a register (usually 15), so you’d get something like this:

          CALL  SUBRTN            CALL A SUB ROUTINE
          B     NEXT(R15)         BRANCH INTO TABLE INDEXED BY R15
NEXT      B      OK               RC=0, OK
          B     RC4               RC=4, WARNING
          B     RC8               RC=8, ERROR
          B     RC12              RC=12, REAL BAD ERROR
          B     RC16              RC=16, QUIT

So why the multiple of 4?

Branch instructions to a label such as B OK as shown above occupy 4 bytes. The B NEXT(15) branches to the label NEXT + whatever is in R15, so when R15 contains zero, it branches to the first branch instruction that then branches to the label RC0. When R15 contains four, it branches to the second branch instruction (label next + 4) that then branches to the label RC4 and so on with each increasing return code representing a more sever error.

To keep the branch tables short, return codes were typically limited to zero through sixteen, thus giving 4 ‘error’ return codes in addition to zero (OK).

Obviously, if the sub routine returned with any other return code such as 2 for example, you would end up branching into some address other than a valid branch instruction with who knows what result!

So, now you know.

Categories: Coding, Mainframe
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: