mushroompot

mushroompot

全栈,全干,全菜。 be a fullstack man, just for fun!
twitter

Back to Thirty Years Ago (1) COBOL

World trends, vast and mighty, prosper if followed, perish if resisted. In the first few months of 2023, under the blessing of LLM, it seemed like "a day in the sky, a year on the ground". However, this rapid development inevitably caused infinite anxiety, so I decided to write a series of articles to alleviate my anxiety and return to the leisurely "slow life" of thirty years ago.

It's not because someone always asks me to write strange things in strange languages.

What is COBOL#

COBOL stands for Common Business Oriented Language, which is often used for processing business data in industries such as finance and accounting. From today's perspective (2023), COBOL may be on the path to extinction, but many financial core systems still use COBOL. There are two main reasons for this: one is the strong stability of COBOL itself, and the other is the high cost of changing existing systems (in many scenarios, it is not cost-effective to use other languages to refactor these systems).

The COBOL section of this series of articles will briefly introduce the basics of COBOL and provide a simple COBOL tutorial. (Of course, I don't recommend spending too much time studying COBOL, after all, the ultimate fate of this language is to become a cyber fossil.)

Taking the First Step#

When it comes to COBOL, we have to mention IBM and mainframes. COBOL is designed to run on mainframes, but does that mean we can't run COBOL without a mainframe? Actually, no. We now have various methods to simulate mainframes and run our programs on personal computers. If you really want to try COBOL, you can refer to the following tutorials for configuration:

Hello COBOL#

Every programming language needs a hello world, and COBOL is no exception. But due to the uniqueness of COBOL, we need to first understand the structure of a complete COBOL program.

2.jpg

This figure shows the hierarchical relationship from top to bottom:

  • A program consists of multiple DIVISIONs.
  • A DIVISION consists of multiple SECTIONS.
  • ...
000100  IDENTIFICATION DIVISION. 
000200  PROGRAM-ID. HELLO.
000300  ENVIRONMENT DIVISION.
000400  DATA DIVISION.
000500  PROCEDURE DIVISION.          
000600      DISPLAY 'HELLO WORLD'
000700      STOP RUN.

From this short program, we can feel that the syntax of COBOL is quite different from the programs we are familiar with today, but it has a strong resemblance to assembly language. Considering the time of its birth, this is not surprising. Next, let's break down the structure of this program. Let's start with individual lines:
1. jpg
The format requirements for COBOL programs are quite strict, and each line can be divided into five parts:

  • Columns 1-6: Sequence area, used to identify line numbers.
  • Column 7: Indicator area, if this position is *, it means the line is a comment; if it is -, it means continuation; if it is /, it means page break.
  • Columns 8-11: All COBOL divisions, sections, paragraphs, and some special entries must start from Area A.
  • Columns 12-72: All COBOL statements must start from Area B.
  • Columns 73-80: Can be used according to the programmer's needs, add modification codes and other identifiers here.

IDENTIFICATION DIVISION: Declares the identification division.
ENVIRONMENT DIVISION: Declares the environment division.
DATA DIVISION: Declares the data division.
PROCEDURE DIVISION: Declares the procedure division.

A COBOL program must have an identification division, but it can omit the environment division and data division.
Let's analyze the parts of the program that are not declaration.
Among them, PROGRAM-ID xxxxx. indicates the ID of the program (can be understood as the identifier of the program).
DISPLAY, similar to the print function in Python, prints the following content.
STOP RUN. indicates the end of the program.

For the hello world program, we may not be able to correlate it with the COBOL program structure mentioned earlier. This is because this COBOL program does not require reading or writing files, and does not even need to store any intermediate variables, so many declarations have been omitted. We will introduce a complete program in the next article.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.