Skip to content
systemcoding.dev
Mainframe

Base Registers and the USING Statement Explained

Understand why HLASM needs a base register, how USING and DROP tell the assembler which register addresses which storage, and how to fix ASMA307E errors.

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

Every beginner's first real HLASM error is some form of "no active USING". The program looks right, the labels exist, the instructions are valid, and the assembler still refuses to turn a perfectly good symbol into an address. The reason is a rule of the architecture: an instruction cannot hold a full storage address. It holds a register number and a small offset, and the register supplies the rest. The USING statement is how you tell the assembler which register will hold which address when the program runs, so it can compute the offset. This post explains base-displacement addressing, writes the usual base register setup, shows what the assembler does with USING, and covers DROP, the limit of 4,096 bytes and the ways around it. It uses HLASM on z/OS 3.1, documented at the High Level Assembler documentation, and the registers and formats from the previous post on registers, the PSW and instruction formats. The language overview is in the complete guide to IBM High Level Assembler on z/OS.

Base and displacement

An instruction that refers to storage carries a 12-bit displacement and a base register number, and at run time the hardware adds the displacement to the contents of the base register. The displacement runs from 0 to 4095. So a program can address 4,096 bytes from each base register, and anything farther needs a different register.

You write the address symbolically, L R5,COUNT, and the assembler produces the register and displacement. It can only do that if it knows which register will hold an address near COUNT. That knowledge is the whole purpose of USING.

Setting up the base

At entry, register 15 holds the address of the program's first byte. The standard setup copies it into a register you choose, usually 12, and announces the fact:

BASEDEMOasm
BASEDEMO CSECT
         YREGS
         STM   R14,R12,12(R13)
         LR    R12,R15
         USING BASEDEMO,R12
         L     R5,COUNT
         AHI   R5,1
         ST    R5,COUNT
         LM    R14,R12,12(R13)
         SLR   R15,R15
         BR    R14
COUNT    DC    F'41'
         END   BASEDEMO

LR R12,R15 is executable: at run time it loads the address. USING BASEDEMO,R12 is not. It produces no machine code. It is an instruction to the assembler that means "from here on, assume register 12 contains the address of BASEDEMO". With that fact, L R5,COUNT becomes L R5,D(,R12) where D is the distance from BASEDEMO to COUNT, which the assembler computes. The listing shows the result, with register 12 as the base and the displacement as the last three digits:

Text
000008 5850C02A                 6         L     R5,COUNT

Here 58 is the operation code for L, 5 is the target register, 0 is no index, C is the base register 12 and 02A is the displacement, 42 bytes from the start of the section. The numbers depend on how much code precedes the label, and the listing shows yours.

The common alternative setup

Programs often avoid depending on register 15 and load the base from the instruction address with BALR or BASR. The instruction with a second register of zero does not branch, and only stores the address of the next instruction in the first register:

asm
         BASR  R12,0
         USING *,R12

USING *,R12 says that register 12 holds the address of the current location counter, the *. Because the BASR instruction is two bytes long, the next instruction's address is the value that register 12 receives, and * after the BASR evaluates to that same address. This form works in code that was not entered through a standard call, such as an interrupt handler, and it is the form older code uses. The post on BALR, BASR and BRAS explains the differences between those instructions.

What happens without USING

Remove the USING line from the first program and assemble it. The assembler cannot find a base for COUNT:

Text
ASMA307E No active USING in effect for this operand

The message text can differ slightly between releases, and its number identifies it. It is attached to the statement that used the symbol, and the object code for that statement is left without a valid base. A related error is ASMA044E Undefined symbol, which means the name is not defined anywhere, a misspelling or a missing DS or DC. Distinguish the two: 044 says the assembler does not know the name, and 307 says it knows the name but cannot reach it.

The other common cause of 307 is reach. The assembler can only use a base register whose range, 4,096 bytes from its address, includes the target. A program longer than 4 KB, with data beyond that point, loses addressability to the later part.

Using more than one base register

The answer to a large program is a second base register, whose value is the address of the next 4,096-byte region. Both registers go on one USING statement:

asm
         USING BIGPROG,R12,R11
         LR    R12,R15
         LA    R11,2048(,R12)
         LA    R11,2048(,R11)

The assembler assumes register 12 addresses BIGPROG through BIGPROG+4095 and register 11 addresses the following 4,096 bytes, BIGPROG+4096 onward. The two LA instructions at run time must establish that: each adds 2,048, and LA takes a displacement of at most 4,095, so one instruction cannot add 4,096. When an instruction refers to a symbol, the assembler picks whichever register gives a valid displacement. The cost is a mistake that is easy to make: if the run-time registers do not match the assumption in USING, the program uses wrong addresses without any error from the assembler. The post on multiple base registers and labeled USING shows the full pattern.

DROP

DROP R12 tells the assembler to stop using register 12 as a base from that point on. You use it when you reuse the register for something else, or at the end of a section that has its own base. A forgotten DROP is dangerous in the opposite direction: the assembler goes on computing displacements from a register that now holds unrelated data, and the program fails at run time. The listing includes a cross-reference of USING and DROP activity, which you read to check what was in effect at each statement. Ask for it with the USING(MAP) option.

Data areas and DSECTs

USING is not only for the program's own base. It also maps a data structure onto a register that holds the structure's address, which is how you read a control block or a record in a buffer:

asm
RECORD   DSECT
RECID    DS    CL6
RECNAME  DS    CL20
RECAMT   DS    PL5
RECLEN   EQU   *-RECORD
         ...
         USING RECORD,R6
         MVC   WORKNAME,RECNAME

RECORD is a dummy section: it defines symbols and offsets and generates no storage. USING RECORD,R6 says register 6 points at storage that has this layout, so RECNAME becomes 6(,R6), offset 6 from register 6. The post on mapping storage with DSECTs builds on this. It is the same mechanism with a different base.

Relative instructions need no base

Newer instructions avoid the problem. Relative branches such as J, BRC and BRAS and the relative load LARL compute their target from the current instruction address, so a program that uses only those and keeps its data addressed through them needs no base register at all. The post on baseless code with LARL develops it. Many modern programs still use a base register for data, since the classic storage-to-storage and L/ST forms require it.

Reading the cross-reference

The listing's symbol cross-reference shows each symbol's offset and the statements that refer to it. Combined with the register cross-reference, it answers the questions that come up when a program works until it grows: where does this reference get its base, and which statement in the program first falls outside the 4,096-byte range. When you see 307 errors that start at one statement and continue to the end of the program, that statement is the first one beyond the reach of your bases.

A debugging habit

When a program abends with a program check at an odd address, check three things in order. Does the base register in the failing instruction hold what the USING assumed? Was the register changed between the setup and the failure, by a macro that uses registers 0, 1, 14 and 15, or by a called routine that did not restore it? And is the failing address inside the program's own storage? Dump analysis starts from the PSW and the registers at the time, and a base register that holds the wrong value is the first thing it finds.

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.