Bulletin Board

What’s new at MAINFRAMES 360
(Updates)...
- Re-writing of select JCL and VSAM Tutorials is under-way, orientation would be more hands-on, and practical
- New Tutorials on COBOL-VSAM programs shall be put up pretty soon
- IMS DB/DC Tutorials to start up in April, 2010

Mainframes360 Search

Loading

Quick Links

Jump to :  

Thursday, August 6, 2009

COBOL Tutorial – Introduction, Writing a Simple Hello World COBOL Program


Q. What does COBOL stand for? What are the application areas where COBOL Programs written?

COBOL stands for Common Business Oriented Language. COBOL Programs are used for commercial data processing. COBOL is an English-like language. Writing a COBOL program is just like writing a passage in English.

Q. You said COBOL Programming language bears resemblance with English. Do COBOL Programs have paragraphs like in English?

A COBOL Program consists of 4 main divisions : IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION and PROCEDURE DIVISION. IDENTIFICATION DIVISION is used to identify the COBOL Program to the system. It tells the system, what’s the name of the program, who has written it, when the COBOL Program was written, when it was compiled etc. ENVIRONMENT DIVISION tells the system, about the environment, the computer equipment used to run the COBOL Program. It gives information about the input and output file-names used in the COBOL Program. DATA DIVISION declares any input-output areas and temporary work-areas of the program. PROCEDURE DIVISION is the starting point of the COBOL Program, where you will code the real COBOL instructions.

In COBOL, Divisions are divided into Sections, Sections into Paragraphs, and paragraphs into sentences. Every sentence must end with a period. Division names, Section Names and Paragraph Names must also be followed by a period. Each sentence contains are series of statements. A COBOL Statement has a COBOL Verb(operator) followed by 1 or more operands.

Q. Show me the basic skeleton of a COBOL Program then..

The basic skeleton of a COBOL Program is as follows :

IDENTIFICATION DIVISION.

ENVIRONMENT DIVISION.

DATA DIVISION.

PROCEDURE DIVISION.

Q. Paragraphs in the English language are indented. Do you have indentation/alignment rules in COBOL?

Just like paragraphs in English, COBOL Divisions and Paragraphs must follow strict alignment rules. The way we indent paragraphs in English, you must indent different Divisions, sections and paragraphs in a COBOL Program. When you write COBOL on the mainframe terminal, or on a coding sheet, the lines are divided into 80 columns. The column 8 is called column A. Column 12 is called column B. The area spanning from columns 8-11 is called Area A. The area spanning from columns 12-72 is called Area B.

All division, section, paragraph names, FD entries and 01-level entries must start in Area A. This means they must start from any column 8,9,10 or 11. The rest of the sentences should start from Area B.

All division and section names must begin on a separate line. There should be atleast one space between paragraph name and its sentences.

Column 7 is used as a special indicator. If you want to write COBOL comments to explain the meaning of a Line of Code in COBOL, you must put an asterisk (*) in the column 7.

Q. What is the IDENTIFICATION DIVISION used for?

The IDENTIFICATION DIVISION is used to identify the COBOL Program to the system. It has the following paragraphs :

PROGRAM-ID.
AUTHOR.
INSTALLATION.
DATE-WRITTEN.
DATE-COMPILED.
SECURITY.

The PROGRAM-ID gives the identity or name of the COBOL Program. The INSTALLATION would be the name of the company.DATE-WRITTEN gives the date when the COBOL Program is written. DATE-COMPILED gives the date when the COBOL Program was compiled. SECURITY gives the level of security. Remember, that only the PROGRAM-ID paragraph is mandatory, all the other entries are optional.

 
To properly highlight the COBOL program, you can type HI COBOL in ISPF.

Q. What does the PROCEDURE DIVISION look like? What’s the format/syntax. Can you elaborate..

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

this PROCEDURE DIVISION. I will now show you a simple Hello World Program in COBOL to illustrate the

process of writing a COBOL Program, compiling it, linking it and running the program.

In the COBOL Program, we want display output, or print some text. When we want to display output, or print text, we use the DISPLAY instruction/verb. The syntax of DISPLAY is as follows : DISPLAY text

The text must be enclosed in single quotes. For example, to print Hello World, we must write the instruction :

DISPLAY ‘HELLO WORLD’.

The PROCEDURE DIVISION of a COBOL Program can have many instructions. We can also divide these instructions into small groups/paragraphs. Thus, the PROCEDURE DIVISION has the following format -

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

Every COBOL Program has a statement/instruction STOP RUN. The STOP RUN instruction tells the mainframe system, that this is the end of the COBOL Program. So, STOP RUN is the point in the COBOL Program, where program execution stops/halts – the control exits out of the COBOL Program. Given below is a simple Program that prints HELLO WORLD to the Output.

 

Q. How do you compile a COBOL Program? What is meant by Linking a COBOL Program? Finally, how do you run the Program on a Mainframe System?


Compiling a COBOL Program :

COBOL is a high-level language. The Mainframe Computer System only understands binary Machine Language 0 and 1. Thus, your COBOL Program has to be translated from COBOL Language to Binary Machine Language.
So, you give the Program in COBOL Language as Input to the Translator(Compiler), and you get the Output Program in Binary Machine Language. The original COBOL Program is called Source Module and it is present as a dataset(file) inside a PDS which we refer to as Source Library. The output of COBOL Compiler – your program in Binary Machine Language is called OBJECT Module.

Linking a COBOL Program
Linking is the process of tying up/bundling up OBJECT MODULES together. You see, you Program may sometimes use(call) your friend’s Program for some task(like finding Square Root). Thus, your Program and your friend’s Program need to be Linked(Connected). Combining Object Modules together, so that they can find each other is called Linking, and this is performed by the Linker. The Linker takes OBJECT MODULE(s) as Input, puts them together and wraps them up as a single package, and gives this gift-package as the Output. This single package is called LOAD MODULE, and it is stored as a member of a PDS – we call LOAD LIBRARY.

Running a COBOL Program
To run a COBOL Program, you need to write a JOB Stream/JCL, which executes your COBOL Program(by specifying your LOAD MODULE in the EXEC PGM Job step), and providing the necessary DD Cards for Input Files, Output Files, Error Files etc.

Given below is a simple schematic, which explains the above process of Comilation, Link-edit, and running diagrammatically.


2 comments:

Don Higgins said...

Good overview for mainframe environment. For another example of "Hello World" COBOL demo program which students can download,compile, and execute on Windows or Linux in 4 different target platform languages at no cost, try zcobol which can be downloaded as open source included with z390 from www.z390.org. The COBOL demo can be compiled into executable mainframe HLASM compatible assembler, MS Visual Express C, J2SE Java, or Intel MASM compatible assembler. the zcobol compiler consists of a J2SE java regular expression parser and translator to HLASM macro source code which is then compiled into the target language using structured conditional macro code. For more on zcobol and current progress in passing all the NIST COBOL 1985 standard test program suite of 415 programs visit www.zcobol.org.

Poornima said...

can u pls help me with the explain with any real time project which make me understand better for the interviews,,,,

Post a Comment

Related Posts with Thumbnails

Quick Links

Jump to :  

Note :

© 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 mail at quasar.chunawalla@gmail.com
 
back to top