Write and Assemble Your First HLASM Program
Write a minimal High Level Assembler program that prints a console message, then assemble it with ASMA90, link-edit it with the binder and run it as a job step.

Assembler on z/OS is the language closest to the machine you can write without typing hexadecimal. Every statement you write becomes one machine instruction, or a short, predictable group of them. People reach for it to write system exits, to understand a dump, or to learn what the compilers do for them. This post takes you through the whole path with the smallest program that does something visible: write the source, assemble it with IBM High Level Assembler (HLASM), link-edit it into a load module with the binder, and run it as a job step. Every term is defined as it appears. The tools are High Level Assembler for z/OS, the binder and z/OS 3.1, documented in the HLASM manuals at the High Level Assembler documentation and in the z/OS library on the z/OS documentation site. The language as a whole is introduced in the complete guide to IBM High Level Assembler on z/OS.
The program
The program writes one line to the console and returns control to the system with a return code of zero. Save it as member HELLO in a source library such as USERID.ASM.SRC, with fixed 80-byte records:
HELLO CSECT
HELLO AMODE 31
HELLO RMODE ANY
YREGS
STM R14,R12,12(R13)
LR R12,R15
USING HELLO,R12
WTO 'HELLO FROM HLASM',ROUTCDE=11
LM R14,R12,12(R13)
SLR R15,R15
BR R14
END HELLOThe column layout is part of the language. A label, when there is one, starts in column 1. The operation code, such as STM or WTO, usually starts in column 10. The operands start in column 16, and a comment can follow after a blank. The statement ends in column 71 at the latest, and column 72 is reserved for a continuation mark. HLASM is not case sensitive for instruction names, but the source for z/OS is conventionally uppercase.
What each line does
HELLO CSECT begins a control section, the unit of code the linker places in the load module, and names it HELLO. The two lines that follow set its addressing mode, AMODE 31, meaning the code expects 31-bit addresses, and its residency mode, RMODE ANY, meaning it can load above or below 16 megabytes.
YREGS is a macro that defines the symbols R0 through R15 so you can write R12 instead of the bare number 12. Registers are the sixteen fast storage locations of the processor, each 32 bits wide for ordinary 31-bit code. Most instructions work on them.
The next three lines are the standard entry. When z/OS calls a program, register 13 points to a save area owned by the caller, register 14 holds the return address and register 15 holds the entry point address. STM R14,R12,12(R13) stores registers 14 through 12 in the caller's save area, starting at offset 12, so the program can restore them on exit. LR R12,R15 copies the entry point into register 12. USING HELLO,R12 tells the assembler that register 12 holds the address of HELLO, so it can turn symbolic references into the base-plus-displacement form the machine needs. The post on base registers explains why.
WTO is a macro, not an instruction. It generates the code that writes a message to the operator console, and ROUTCDE=11 sends it to the programmer's job log. The macro expansion uses registers 0, 1, 14 and 15, which is why the program saved them first.
The exit is the mirror of the entry. LM R14,R12,12(R13) reloads the registers, SLR R15,R15 clears register 15 to zero because z/OS reads the return code from it, and BR R14 branches to the return address. END HELLO ends the source and names the entry point.
Assemble it
HLASM is a program called ASMA90. It needs a work file, a library to find macros such as YREGS and WTO, a place to write the object module and a place to print the listing:
//ASMHELLO JOB (ACCT),'ASSEMBLE',CLASS=A,MSGCLASS=X
//ASM EXEC PGM=ASMA90,PARM='OBJECT,NODECK,LIST(133)',REGION=0M
//SYSLIB DD DISP=SHR,DSN=SYS1.MACLIB
// DD DISP=SHR,DSN=SYS1.MODGEN
//SYSUT1 DD UNIT=SYSDA,SPACE=(CYL,(5,5))
//SYSPRINT DD SYSOUT=*
//SYSLIN DD DSN=&&OBJ,DISP=(NEW,PASS),UNIT=SYSDA,
// SPACE=(TRK,(5,5)),DCB=(RECFM=FB,LRECL=80,BLKSIZE=0)
//SYSIN DD DISP=SHR,DSN=USERID.ASM.SRC(HELLO)SYSLIB is searched for macros, and SYS1.MACLIB and SYS1.MODGEN hold the system macros. SYSIN is the source. SYSLIN receives the object module, passed to the next step as the temporary data set &&OBJ. PARM='OBJECT,NODECK' asks for an object module on SYSLIN and no punched deck. A clean assembly ends with return code 0 and prints in SYSPRINT a summary:
No statements flagged in this assembly
HIGH LEVEL ASSEMBLER, 5647-A01, RELEASE 6.0
Assembler Done No Statements FlaggedThe exact banner text varies with the release and service level. If a statement has an error, the listing shows it with an ASMA message number, such as ASMA044E Undefined symbol, directly under the statement, and the return code is 8 or higher.
Link-edit it
The object module is not yet runnable. The binder, called here by its program alias IEWL, resolves references and writes a load module to a library:
//LKED EXEC PGM=IEWL,PARM='LIST,MAP,XREF,RENT',REGION=0M
//SYSLIN DD DSN=&&OBJ,DISP=(OLD,DELETE)
//SYSLMOD DD DISP=SHR,DSN=USERID.LOAD(HELLO)
//SYSUT1 DD UNIT=SYSDA,SPACE=(CYL,(2,2))
//SYSPRINT DD SYSOUT=*SYSLMOD names the load library and the member, which must be a partitioned data set with RECFM=U. The RENT option marks the module as reentrant, which is true here because the code never modifies itself. A binder return code of 0 means success.
Run it
Execute the member by naming it as the program, with the load library on STEPLIB:
//RUN EXEC PGM=HELLO
//STEPLIB DD DISP=SHR,DSN=USERID.LOADThe message appears in the job log of the run step, in the JESMSGLG section of the output:
HELLO FROM HLASM
IEF142I USERID RUN - STEP WAS EXECUTED - COND CODE 0000If the step ends with a nonzero code, the first suspect is register 15. The line SLR R15,R15 sets it to zero before the return, and without it the code would be whatever the WTO macro left there.
Why a macro for the message
You could write the message directly with the machine's own services, but the system macros exist to hide the details of the service call, which can change between releases. The WTO macro expands into a parameter list and a supervisor call instruction. A listing option, PRINT GEN in the source, shows the generated lines, each flagged with a plus sign in the listing, so you can read exactly what the macro produced. Looking at that expansion once is the quickest way to see the difference between an instruction you wrote and one a macro wrote for you.
Common first mistakes
The first is column alignment: an opcode in column 1 is read as a label, and the assembler reports an unrecognized operation. The second is forgetting USING, which leaves every symbolic reference without a base and produces errors on the first statement that refers to storage. The third is overwriting registers 13 and 14 inside the program without saving them, which crashes on the way out. The fourth is a missing END, which assembles with a warning and no entry point. When a program loops or abends, the assembler listing is your map: its offsets match the offsets in the dump.


