Skip to content
systemcoding.dev
Mainframe

Write, Compile and Run Your First COBOL Program on z/OS

Compile and run a first COBOL program on z/OS with the IGYWCLG cataloged procedure, with the JCL, the compiler options, the output and the return codes to read.

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

On a PC, you save a source file and run one command. On z/OS, the same job is three programs in a row: the compiler turns source into an object module, the linkage editor (the binder) turns that into an executable load module, and the system then runs it. You describe all three in JCL, the Job Control Language, and the compiler product comes with a ready-made JCL procedure so you do not have to write the steps. This post takes the small program from the divisions post and runs it with Enterprise COBOL for z/OS 6.4 on z/OS 3.1. It assumes you can sign on to TSO and edit a data set in ISPF, and it defines each JCL term as it appears.

What you need first

You need a user ID with access to a source library, usually a partitioned data set (PDS) of 80-byte records. Create one in ISPF option 3.2, for example USER01.COBOL.SOURCE, with record format FB, record length 80 and a few tracks of space. Your site also has the Enterprise COBOL compiler installed, and your systems programmer can tell you the high-level name of its libraries, such as IGY.V6R4M0. The procedure uses that name as a symbolic parameter.

The program

Put this in a member called HELLO of the source library. The code must start in column 8 or later:

hello.cblcobol
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-MESSAGE   PIC X(30) VALUE 'HELLO FROM COBOL ON Z/OS'.
       PROCEDURE DIVISION.
           DISPLAY WS-MESSAGE
           GOBACK.

It has one variable and two statements. Nothing in it is specific to z/OS, which is what makes it a good first run.

The JCL

jcl
//HELLOJOB JOB (ACCT),'FIRST COBOL',CLASS=A,MSGCLASS=X,
//             MSGLEVEL=(1,1),NOTIFY=&SYSUID
//CLG      EXEC IGYWCLG,LNGPRFX='IGY.V6R4M0',
//             LIBPRFX='CEE',PARM.COBOL='LIST,MAP,XREF'
//COBOL.SYSIN DD DISP=SHR,DSN=USER01.COBOL.SOURCE(HELLO)
//LKED.SYSLMOD DD DISP=SHR,DSN=USER01.COBOL.LOAD(HELLO)

Read it line by line:

  • The JOB statement names the job, gives accounting information, a class that selects an initiator, and a message class that sends the output to a held queue so you can read it. NOTIFY=&SYSUID sends you a message when it ends.

  • EXEC IGYWCLG runs a cataloged procedure with that name, which IBM supplies with the compiler. IGYWCLG stands for compile, link-edit and go. It contains three steps named COBOL, LKED and GO.

  • LNGPRFX is the prefix of the compiler libraries and LIBPRFX the prefix of the Language Environment libraries, which are the runtime support. The names are specific to your site.

  • PARM.COBOL passes compiler options to the COBOL step. LIST prints the generated code, MAP prints a data map and XREF prints a cross reference of names.

  • COBOL.SYSIN supplies the source. A DD statement with a step-qualified name overrides that DD inside the procedure.

  • LKED.SYSLMOD says where the load module goes. The load library USER01.COBOL.LOAD must exist, with RECFM=U.

The procedures that come with the compiler are IGYWC to compile, IGYWCL to compile and link-edit, IGYWCG to compile and go, and IGYWCLG to do all three.

Submit and read the result

Submit the job from ISPF edit with the SUB command. In SDSF, option ST, find your job and open it with S. The job log shows the steps:

Text
IEF142I HELLOJOB COBOL - STEP WAS EXECUTED - COND CODE 0000
IEF142I HELLOJOB LKED - STEP WAS EXECUTED - COND CODE 0000
IEF142I HELLOJOB GO - STEP WAS EXECUTED - COND CODE 0000

A condition code of 0 means success for that step. Under the GO step's SYSOUT, the program's output appears:

Text
HELLO FROM COBOL ON Z/OS

The COBOL step's listing shows your source with line numbers, the data map and the cross reference, and ends with a summary line saying the compilation had no statements flagged and a return code of 0. The LKED listing shows the binder's messages, which should include the module name and an ENTRY POINT line.

How the condition codes work

Each step returns a number. The common values are:

CodeMeaning
0Completed with no diagnostics
4Warnings. The program may be correct, but read them
8An error. The compiler may still produce an object module, and the link step may run, but the module can misbehave
12A severe error. Output is not usable
16A critical error. The compiler stopped

In a procedure with several steps, the system usually runs a later step only if the earlier one's code is low enough. The standard procedures set a condition so that the link step is skipped after a failed compile.

When it goes wrong

A misspelled division name or a statement in the wrong column gives a compiler message with a number beginning IGYPS, IGYDS or IGYPA. The message has a severity letter, W for warning, E for error and S for severe, and the line number in your source. Fix the first message and run again, since later ones often follow from it.

A job that fails with JCL ERROR in the job log and a message beginning IEF or IEFC has a problem in the JCL, such as a data set that does not exist or a misspelled procedure name. Messages with the id IEFC explain the line. A NOT CATALOGED message at the SYSLMOD DD means the load library name is wrong, or the library was not created.

If the GO step ends with a code such as S0C7, the program ran and failed at execution time. That is an abend: a data exception in this case, usually bad numeric data. It cannot happen in this program, since it has no numeric fields.

Running the load module later

Once the load module exists, you do not need to compile again. A short job runs it:

jcl
//RUNHELLO JOB (ACCT),'RUN HELLO',CLASS=A,MSGCLASS=X
//STEP1    EXEC PGM=HELLO
//STEPLIB  DD DISP=SHR,DSN=USER01.COBOL.LOAD
//SYSOUT   DD SYSOUT=*

EXEC PGM=HELLO names the program, and STEPLIB tells the system which library to search. The Language Environment runtime must also be available, either through the system link list or through a STEPLIB concatenation.

Why three steps

The split reflects history and flexibility. The compiler produces an object module for one program. The binder combines several object modules, such as your program and its subprograms and runtime pieces, into one load module and resolves the calls between them. The system loader then reads the load module into storage and starts it. In a production shop the compile and link may run in a build job, and the run step on its own in the nightly schedule.

The procedures and options are documented in the Enterprise COBOL for z/OS Programming Guide, under compiling and running.

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.