Skip to content
systemcoding.dev
Mainframe

Use STM, LM, LA, L and ST to Move Data Between Registers and Storage

Learn the HLASM instructions that move data between registers and storage, L, ST, LR, LA, STM and LM, with a traced example showing every register value.

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

Nearly every HLASM program is a conversation between registers and storage. Data waits in storage, comes into a register to be worked on, and goes back. Five instructions carry most of that traffic: L and ST for one fullword, LR for register to register, LA for addresses and small constants, and STM and LM for ranges of registers. They look alike and behave differently in ways that matter: L reads storage, LA does not, and LM and STM move many registers in one instruction. This post defines each, shows them in one short program with a trace of the register values after every step, and lists the mistakes that cost the most time. The instructions are those of z/Architecture in 31-bit mode on z/OS 3.1, assembled with HLASM and documented at the High Level Assembler documentation. The cluster hub is the complete guide to IBM High Level Assembler on z/OS.

The instructions

InstructionDoesFormat
L R1,addrload a fullword (4 bytes) from storage into a registerRX
ST R1,addrstore the register's low 4 bytes into storageRX
LR R1,R2copy register 2 into register 1RR
LA R1,addrload the address itself, not what is stored thereRX
STM R1,R3,addrstore registers R1 through R3 into consecutive fullwordsRS
LM R1,R3,addrload registers R1 through R3 from consecutive fullwordsRS
LH, STHload and store a halfword, 2 bytesRX
IC, STCinsert and store one character, 1 byteRX

The range in STM and LM wraps: STM R14,R12,12(R13) stores 14, 15, 0, 1 up to 12, which is the entry sequence from the earlier posts, fifteen registers in one 4-byte instruction.

L and LA are not the same

This is the distinction beginners miss. L R5,COUNT goes to the storage at COUNT and puts the four bytes found there in register 5. LA R5,COUNT puts the address of COUNT in register 5 and never touches the storage. The address is what a base register or a parameter list needs, and the value is what arithmetic needs. Mixing them up produces a program that adds a number to an address and wonders why the totals are huge.

LA has a second life. Because it computes base plus index plus displacement and puts the result in a register, it works as a small adder that does not touch the condition code: LA R1,5 loads the constant 5, LA R2,1(,R2) adds one to register 2, and LA R3,0(R4,R5) puts the sum of two registers in register 3. In 31-bit mode LA clears the high-order bit of the result, so it is not a safe general add for large values, but it is idiomatic for counters and pointer steps.

A traced program

The program below declares three fullwords and a work area, and uses all five instructions. It does nothing visible, so the trace is how you see it:

MOVESasm
MOVES    CSECT
         YREGS
         STM   R14,R12,12(R13)
         LR    R12,R15
         USING MOVES,R12
         L     R2,VALUE1
         L     R3,VALUE2
         LR    R4,R2
         LA    R5,VALUE1
         LA    R6,10(,R3)
         ST    R6,RESULT
         LA    R7,SAVEBLK
         STM   R2,R4,0(R7)
         SLR   R2,R2
         SLR   R3,R3
         SLR   R4,R4
         LM    R2,R4,0(R7)
         LM    R14,R12,12(R13)
         SLR   R15,R15
         BR    R14
VALUE1   DC    F'100'
VALUE2   DC    F'250'
RESULT   DS    F
SAVEBLK  DS    3F
         END   MOVES

Walk through it. L R2,VALUE1 loads 100 into register 2, and L R3,VALUE2 loads 250 into register 3. LR R4,R2 copies register 2 into register 4, so both hold 100. LA R5,VALUE1 puts the address of VALUE1 in register 5. That is an address inside the program, so its exact value depends on where the system loaded it. LA R6,10(,R3) adds 10 to register 3 through the address adder and stores 260 in register 6. ST R6,RESULT writes that 260 to storage.

Then the block move. LA R7,SAVEBLK points register 7 at a three-fullword area. STM R2,R4,0(R7) stores registers 2, 3 and 4 in order into that area. The three SLR instructions clear them, and LM R2,R4,0(R7) brings the three values back.

Here are the register values after each marked step, with the address in register 5 shown as a made-up example:

Text
after L R2,VALUE1      R2=00000064
after L R3,VALUE2      R3=000000FA
after LR R4,R2         R4=00000064
after LA R5,VALUE1     R5=00006038   (address varies by load)
after LA R6,10(,R3)    R6=00000104
after the three SLR    R2=R3=R4=00000000
after LM R2,R4,0(R7)   R2=00000064 R3=000000FA R4=00000064

Values are in hexadecimal: 100 is 64, 250 is FA and 260 is 104. The storage word RESULT holds 00000104. The save-and-restore through SAVEBLK is exactly what the program's entry and exit do with the caller's registers, only on a smaller scale.

Alignment

On z/Architecture, L, ST, LM and STM work on any address, aligned or not. Aligned fullwords, on a multiple of four, are faster on most models and are the convention. The assembler aligns constants for you when you define them with DC F'100', which forces fullword alignment and may insert padding before it. A data area defined as DS CL4 has no alignment, so a program that loads a fullword from it works but can be slower and, in some contexts such as a few specialized instructions, can raise a specification exception. Use DS 0F before an area you want to be fullword aligned:

asm
         DS    0F
COUNTER  DS    CL4

The 0F allocates zero bytes but aligns the location counter to a fullword boundary, a standard idiom.

Halfwords and bytes

When a field is two bytes or one byte, use the matching instruction, or you read neighbors. LH R1,HALF loads a halfword and propagates the sign bit through the register, so a stored X'FFFE' loads as -2. STH R1,HALF stores the low two bytes. For a single character, IC R1,CHAR inserts one byte into the low-order byte of register 1 without touching the other three, and STC R1,CHAR stores the low byte. Clear the register first with SLR R1,R1 if you want the whole register to hold the byte. A common bug is IC into a register that holds old data, which leaves the upper bytes unchanged and makes a later full compare unequal.

64-bit versions

The 64-bit equivalents add a G: LG, STG, LGR, LMG and STMG move 8-byte values, and a few add a F for fullword extension, such as LGF, which loads a fullword and sign-extends it to 64 bits. Programs in 31-bit mode on a 64-bit machine can use these to work with larger numbers, and the post on 64-bit grande instructions covers the details.

Common mistakes

Using L where LA is meant, or the reverse, is the first, and the symptom is a wrong value or an address-sized number in arithmetic. The second is STM with the wrong offset or LM from a different offset than the matching STM, so the registers return shuffled. The third is forgetting that LM over a range that includes register 13 or 15 overwrites them, as the exit sequence does deliberately and application code must do on purpose. The fourth is assuming a register keeps its value across a macro. System macros such as WTO, GET and PUT use registers 0, 1, 14 and 15, so values you need after one must live in registers 2 through 12 or in storage.

Checking your work

Read the listing. The object code beside each line lets you confirm the base and displacement. And in a dump, find the instruction at the failing offset, decode its register fields, and compare them with the registers shown, which is the practice from the post on instruction formats.

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.