First steps in COBOL

Written May 24th, 2020 by

Recently, a lot has been heard about the COBOL language, due to the need of some States to update their computer systems and the difficulty in finding programmers with some experience in using this programming language. Not knowing the language, if not for “hearsay”, I thought to deepen its knowledge a little, more out of personal curiosity than anything else.

To take the first steps in COBOL I chose the “old school” approach. I recovered the Microsoft COBOL compiler for CP/M and the related manual, installed it on my Z80 RC2014 computer, and started writing the typical “Hello World” with which you start programming with any language.

000010 IDENTIFICATION DIVISION.
000020 PROGRAM-ID. HELLO.
000030
000040*    SAMPLE HELLO WORLD PROGRAM.
000050
000060 ENVIRONMENT DIVISION.
000070
000080 DATA DIVISION.
000090
000100 PROCEDURE DIVISION.
000110 MAIN.
000120     DISPLAY "HELLO WORLD".
000130     STOP RUN.

The next step was to write a slightly more complex program by porting the Roman numeral converter that I wrote in BASIC.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.

      *    DECIMAL TO ROMAN NUMBER CONVERSION.

       ENVIRONMENT DIVISION.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 CONST VALUE "1000M 0900CM0500D 0400CD0100C 0090XC0050L 0040XL0
      -    "010X 0009IX0005V 0004IV0001I ".
           02 ROMAN OCCURS 13 TIMES INDEXED BY I.
               03 VAL PIC 9(4).
               03 STR PIC A(2).

       77 NUM PIC 9(4).
       77 RES PIC X(14).
       77 A   PIC 9(4).

       PROCEDURE DIVISION.
       MAIN.
           DISPLAY "DECIMAL TO ROMAN NUMBERS CONVERTER".
       L2. DISPLAY "ENTER NUMBER TO CONVERT".
           ACCEPT NUM.
           IF NUM IS NOT ZERO,
                   PERFORM DEC-TO-ROMAN THROUGH END-DEC-TO-ROMAN
                   DISPLAY RES
                   GO TO L2.
           STOP RUN.

       DEC-TO-ROMAN.
           MOVE NUM TO A.
           MOVE " " TO RES.
           SET I TO 1.
       L1. IF A IS NOT LESS THAN VAL(I),
                   STRING RES STR(I) DELIMITED BY SPACE INTO RES
                   COMPUTE A = A - VAL(I)
                   GO TO L1.
           IF I LESS THAN 13,
                   SET I UP BY 1
                   GO TO L1.
       END-DEC-TO-ROMAN.

Unlike the BASIC program, this only converts from Arabic (decimal) numbers to Roman numbers.

I must say that COBOL is a fairly interesting language even if the approach with the Microsoft compiler is not the best, since being quite outdated, many of the examples available on the Internet, which use more modern constructs, do not work. To deepen your knowledge of the language it is certainly better to use a compiler such as GNU Cobol, available for Linux and Windows.

Image Gallery

cpm_cobol1cpm_cobol2
cpm_cobol3cpm_cobol4
cpm_cobol5