Skip to content
systemcoding.dev
Mainframe

z/Architecture Registers, PSW and Instruction Formats for Beginners

Learn the z/Architecture registers, the program status word and the main instruction formats, with a hand-decoded example of how an HLASM line becomes bytes.

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

Writing HLASM without a picture of the machine is like driving without a dashboard. You can steer, but you cannot tell why the engine stopped. This post gives the picture for z/Architecture, the instruction set architecture of IBM Z that z/OS 3.1 runs on: the registers a program can use, the program status word that controls what the processor does next, and the instruction formats that decide how a line of assembler becomes bytes in storage. It ends with two instructions decoded by hand. The definitive source is the z/Architecture Principles of Operation, available from IBM's documentation site, and the assembler-side rules are in the HLASM Language Reference at the High Level Assembler documentation. It prepares the ground for the complete guide to IBM High Level Assembler on z/OS, and follows the first program in the post on writing and assembling your first HLASM program.

The registers

A program has sixteen general registers, numbered 0 to 15. In z/Architecture each is 64 bits wide. A 31-bit program uses the low 32 bits, called the low word, and ignores the high half unless it uses the 64-bit instructions covered later. The registers hold integers and addresses alike, and the hardware treats them the same way. Conventions give them roles:

RegisterConvention
0scratch; some instructions treat 0 as "no register" in an address field
1points to the parameter list on entry
2 to 11free for your use
12commonly the base register of the program
13points to the caller's save area on entry, and to your own while you run
14the return address
15the entry point on entry, and the return code on exit

Two facts catch beginners. When register 0 appears as a base or index register in an address, the hardware uses zero and ignores the register's content, so never rely on register 0 for addressing. And a few instructions, such as MVCL, use register pairs, an even register and the odd one after it.

Beyond the general registers, there are sixteen access registers, used for data spaces and rarely seen in application code; sixteen floating-point registers, which on current machines are part of a larger set of vector registers; and control registers, which only the operating system uses. A normal program cares about the general registers.

The program status word

The program status word, or PSW, is a 128-bit register that controls the processor. It holds the address of the next instruction and several control bits. Its important fields for a programmer are:

FieldMeaning
instruction addresswhere the next instruction is fetched from
condition code (CC), 2 bitsthe result of the last instruction that sets it, 0 to 3
program maskwhether some arithmetic exceptions interrupt the program
problem state bit1 for a problem program, 0 for supervisor state
addressing mode bitstogether select 24-bit, 31-bit or 64-bit addresses
storage keywhich storage the program may modify

The addressing mode comes from two bits. When both are zero the program runs in 24-bit mode, when the first is zero and the second is one it runs in 31-bit mode, and when both are one it runs in 64-bit mode. The AMODE statement in your source tells the system which mode the program expects.

Branch instructions change the instruction address. The condition code is the flag that tests read, and it works as a four-valued result rather than the one-bit flags of other architectures. A compare sets it to 0 for equal, 1 for first operand low and 2 for first operand high. An add sets it to 0 for a zero result, 1 for negative, 2 for positive and 3 for overflow. A conditional branch names the values of the condition code that make it take the branch, which is how BE, BNE, BL and BH work.

Instruction lengths and formats

An instruction is 2, 4 or 6 bytes long. The first two bits of the first byte say which: 00 means 2 bytes, 01 or 10 mean 4 bytes, and 11 means 6 bytes. The remaining bits of the first byte, and sometimes the next byte, are the operation code. The rest is operand fields, laid out in a format that has a name. These are the formats you will meet most:

FormatLengthTypical useExample
RR2 bytesregister to registerAR R1,R2
RX4 bytesregister and storage with an indexL R1,0(R2,R3)
RS4 bytesregister range and storageSTM R14,R12,12(R13)
SI4 bytesstorage and an immediate byteMVI 0(R5),C'A'
SS6 bytesstorage to storageMVC 0(8,R5),0(R6)
RI4 bytesregister and a 16-bit immediateAHI R1,5
RIL6 bytesregister and a 32-bit relative addressLARL R2,TABLE
RXY6 bytesRX with a 20-bit displacementLY R1,-8(R2)

A storage operand is written as D(X,B) or D(B): a displacement D added to the contents of a base register B and, in the RX forms, an index register X. The displacement is a 12-bit field, so it is 0 to 4095. That small range is why programs need a base register and why the assembler needs USING. The Y forms such as LY carry a 20-bit signed displacement, which reaches half a megabyte either way and removes the need for a second base register in many programs.

Decoding two instructions by hand

Take LR R12,R15. It is format RR, so it is 2 bytes. The operation code for LR is hexadecimal 18. The two operand fields are one hexadecimal digit each, the first register and the second register. R12 is C and R15 is F, so the bytes are:

Text
18 CF

Now take L R1,0(R2). It is format RX, so it is 4 bytes. The operation code for L is 58. The next byte holds the target register in its first digit and the index register in its second, which is 1 and 0, giving 10. The last two bytes hold the base register in the first digit and the 12-bit displacement in the other three, 2 and 000:

Text
58 10 20 00

You can see this in an assembler listing. HLASM prints the location, the object code and the source side by side, which is the quickest way to learn the encoding:

Text
000000 18CF                    10         LR    R12,R15
000002 58102000                11         L     R1,0(R2)
000006 5020C010                12         ST    R2,16(,R12)

The listing columns are the location counter, the object code in hexadecimal and the statement. ST R2,16(,R12) is also an RX instruction: operation code 50, register 2, index 0, base C, displacement 010 hexadecimal, which is 16. Reading the object code beside the source is the habit that makes assembler errors obvious, for example when a displacement of 4096 or more cannot fit and the assembler reports a problem.

What the branch tests read

A conditional branch is BC mask,target, where the mask is a four-bit value, one bit per condition code value. The leftmost bit stands for code 0, then 1, 2 and 3. The branch is taken when the bit for the current condition code is on. Extended mnemonics hide the mask: BE is mask 8, which selects code 0, BNE is mask 7, which selects 1, 2 and 3, BL is 4, BH is 2 and B is 15, the unconditional branch. Knowing the mask makes sense of a dump where the instruction shows as 47 80 ...: the 47 is BC, and the 8 in the next byte is the mask for "equal".

Why this matters in practice

When a program abends, the system gives you the PSW at the time and the registers. The instruction address points at the failing instruction, or just after it, and the registers show the values it used. With the picture above you can read a dump: find the instruction by its offset, decode its operand fields, and check whether the base register held what the code assumed. Most assembler bugs turn out to be a register that held the wrong value, a displacement that went past the end of a data area, or a condition code tested after an instruction that did not set it.

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.