What’s new at MAINFRAMES 360
(Updates May '11)...
- We are a cosmopolitan community of 400 members, from around the globe.
- Working on writing and publishing articles on CICS.
- Special article on "How do I learn Mainframe Programming"

Thursday, August 6, 2009

Introduction to COBOL Programming


Q. What does COBOL stand for?
COBOL stands for Common Business Oriented Language. COBOL Programs are used to process. Business-Data. COBOL Programs are easy to code and analyse, because it is an English-like language. Writing a COBOL program is just like writing a passage in English.
Q. Are COBOL Programs English-like?
Just as text in English is organised into sections and para’s, COBOL Programs can also be broken down into divisions, divisions further into sections, and sections into paragraphs. Each paragraph contain sentences, or COBOL Statements. Like the English language, sentences(statements) in COBOL should end with a period(full-stop).

When you start new paragraphs in COBOL, you need to indent them, just as in English you follow indentation rules. COBOL then is whole-lot English like. Even instructions in COBOL, are words borrowed from English, ADD, SUBTRACT, MULTIPLY, DIVIDE etc.
Q. What’s the basic skeleton of a COBOL Program
The basic skeleton of a COBOL Program is as follows :
 
     Image198[1]

A COBOL Program can be divided or broken down into 4 parts – IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION and PROCEDURE DIVISION. The PROCEDURE DIVISION contains the instructions to be executed(performed), one after the other, step-by-step till you reach the end of the Program and stop running it(STOP RUN).

Under a division, you may have several sections as well, and sections in turn can have several paragraphs. Divisions, sections and paragraphs help make the COBOL Program more structured, and the flow of the COBOL Program is easy to grasp.
Q. What are the Alignment(Indentation rules) in COBOL?
The lines in a COBOL Program are indented or aligned, as its done in English. When you write COBOL Program on the Mainframe terminal, you have 80 columns on a line to code on. The area spanning from columns 8-11 is called Area A. The area spanning from columns 12-72 is called Area B.

In COBOL, division names, sections and paragraphs begin in Area A. See below, how I’ve coded IDENTIFICATION DIVISION, ENVIRONMENT DIVISION,.. etc. starting on Column 8 -

Image199[1]

You code COBOL Instructions, COBOL Statements in Area B(that’s anywhere between Column 12 – 72). So, all your COBOL Instructions in the PROCEDURE DIVISION like ADD, SUBTRACT, MOVE should begin on or after Column 12. Look below, how I’ve coded COBOL Instructions in Area B.

Image200[1] 

An interesting bit of trivia, back in the early days, COBOL Programs were coded on a COBOL Coding-Sheet, before punching the Instructions onto a Punched-Card. Here's a picture of, how a Coding-Sheet. looked like.

image
Q. What is the IDENTIFICATION DIVISION used for?
The IDENTIFICATION DIVISION is used to identify the COBOL Program to the system. Under the IDENTIFICATION DIVISION, you can code several para-entries like PROGRAM-ID, AUTHOR etc. which supply extra-descriptive information about the COBOL Program.

The PROGRAM-ID para assigns a unique-name to the COBOL-Program. You simply write the name of your COBOL Program after the PROGRAM-ID Para. Here’s how you code it -

Image262

The AUTHOR Paragraph tells, who’s written this COBOL Program. If you’ve coded the COBOL Program, you can put your name as the AUTHOR of the program.

Image263

The INSTALLATION would be the name of the company, where the COBOL Software Program was designed and written. For example, if I work for TCS, I would code :

Image264

The DATE-WRITTEN is the date when the COBOL Source-Program was coded. When you choose not to write a date, the COBOL Compiler software would insert a date for free.

Image265

The DATE-COMPILED is the date when the COBOL-Program was compiled. Again, if you don’t supply a Compile date, the Compiler software inserts one for you.

Image266

Under the IDENTIFICATION DIVISION, you must compulsorily code the PROGRAM-ID Para, you can skip off coding the AUTHOR, INSTALLATION, DATE-WRITTEN and DATE-COMPILE Entries. PROGRAM-ID Para is must.

Q. How do you document a COBOL Program?

A well-documented COBOL Program is easy to analyse. You can quickly understand the functionality of the COBOL Program, if it contains comments. In COBOL, you code comments by putting an asterisk * in Column 7.

Image267

You must always supply document what the program-name is, which system, a brief description of the processing it does.

Q. What is PROCEDURE DIVISION?

The PROCEDURE DIVISION is the starting-point of the COBOL-Program. This is where the Mainframe begins to run(execute the COBOL instructions). Hence, all your instructions/programming logic must be written inside this PROCEDURE DIVISION.

Under the PROCEDURE DIVISION, you can write Instructions one-after-the other. The Mainframe executes, runs the instruction in the PROCEDURE DIVISION, one-by-one, step-by-step sequentially, till you say, STOP RUNning the Program. To stop running the COBOL Program, the last-instruction you code must be STOP RUN.

PROCEDURE DIVISION.
   Instruction-1
   Instruction-2
   Instruction-3
   Instruction-4
   ...
   Instruction-N
   STOP RUN.

Q. How do you display text on the Screen in COBOL?
To Display data on the Screen, in COBOL you code the DISPLAY Instruction. You code DISPLAY followed by the text that want to print on the Screen, enclosed in Single Quotes. Say for example, if I want to display Hello World on the Screen, I would write -

Image268
Q. How do I setup  Compile-Link Job?
To run and process data using a Program, you need to first Compile-Link a COBOL Program. The Free COBOL-Compiler Software on Mainframes is IGYCRCTL. The Compiler-Software works like a Translator, translates the Program from
COBOL to Binary Language.
                Image269

Like any other Program on Mainframes, the free Compiler IGYCRCTL also expects you to supply some Input-Output Files
                     Image270
To run the Compiler-Software IGYCRCTL, I set-up a job like this – I’ve set //SYSIN DD Input-File to point to my COBOL Source Program which resides at SYSADM.DEMO.SRCLIB(PROG04). //SYSLIN DD Output-File, where the compiler stores Binary Object code points to a Temporary-File LOADSET. The //SYSPRINT DD File is set to point to the Spool SYSOUT=*. This contains the Compile-Listing. I look at the Compile-Listing, and know if my program successfully compiled, or the compiler complained about my Program(its faulty, has syntax errors). //SYSUT1 to //SYSUT7 are temporary-work-Files.

Image271

The Output Binary-Object-Code generated by the Compiler-Software IGYCRCTL, cannot  run on its own, standalone. To get it running, it needs other Helper Programs. The Linker-Software packs and bundles together my Binary-Program along-with any other Helper-Programs, it may need into one composite Load-Module. My Program is all set to go! The Free COBOL-Linker Software on Mainframes is HEWL. It expects the following files - 
                     Image272

To run the Linker Software HEWL, I added a LKED Step to my job. I have set the //SYSLIN DD Input-File to point to Binary Object-Code stored at the
Temporary-Dataset LOADSET created in the COMPILE Step. Bundled-up Output Load Module of the Linker HEWL is stored in //SYSLMOD File. I have made the //SYSLMOD DD File point to the dataset SYSADM.DEMO.LOADLIB(PROG04). The Load-Module PROG04 would be stored in my personal Load-Library SYSADM.DEMO.LOADLIB.

Image273
Q. I’ve hit SUB on the Compile-Link Job. What next?
Hit SUBmit on the Compile-Link job after you’ve set it up.

Image274 
Start SDSF to see the Output of the Compile-Link Job in the Spool, and check Compile SYSPRINT-Listing for any errors.

Image275

Here’s how, the Compile-Listing looks. It shows that I running Enterprise COBOL Version. The SSRANGE, XREF, LIST, MAP, OFFSET Compiler options have been turned ON.

Image277

The Compiler Software IGYCRCTL leaves behind  signature, a Two-Digit Return-Code as a signal, to indicate if the COBOL-Program compiled successfully, or it contained errors. Check the Return-Code set by the Compiler, by scrolling to the bottom of the Compile-Listing. The Return-Code set by the Compiler is RC=04. A Zero RC suggests, the COBOL Program is correct, an RC=04 denotes Warnings(W). This is acceptable. When the Compiler sets the RC=08, the COBOL Program contains Errors(E), and an RC=12 says the COBOL Program has
Severe-Errors(S). When the Compiler complains with Errors(E) and
Severe Messages(S), you need to go back and correct your COBOL Program.

Image276
Q. How do I run my Program?
To run your program , you need to write a Job. I’d run my Program, by setting up a job like this -

Image278

I need to run the Program PROG04, so I write EXEC PGM=PROG04. The Mainframe Computer should pickup this Load Module from my Personal Load Library SYSADM.DEMO.LOADLIB, so I added a //JOBLIB DD Statement pointing to my
Load-Library. When I SUBmit this Job, the Output Listing in the Spool, shows up as -

Image279
Related Posts Plugin for WordPress, Blogger...

Note :

Protected by Copyscape Online Copyright Protection
© Copyright – Quasar Chunawalla, 2010.
Note : The copyrights of all the material, text and pictures posted in this website belong to the author. Any instance of lifting the material from this website, shall be considered as an act of plagiarism. For any clarifications, please drop me a line at
 
back to top