The date is April 6th 2020. COVID-19 is spreading exponentially, the world is on lockdown, unemployment is projected to surpass the great depression, the razor-thin margins that scaffold the global economy have evaporated due to stagnant consumer spending, and the federal reserve’s printing press is only bottlenecked by the numbers of hours in a day. The current market’s most obvious bull trap in history indicates that the U.S. oligarchy and many of their constituents are living in delusion.

Not you — you’re smart. You’ve been short selling $TSLA since you heard the word “coronavirus” because you know that the economy is a house of cards on the verge of a catastrophic collapse. This collapse brings sweeping changes to the tech industry:

Gullible CTOs don’t have the budget to acquire yet another machine learning startup.

Artificial intelligence research rescinds into its fourth nuclear winter.

The advertising-tech bubble (FAANG) pops.

The weekly hype for the next javascript framework falls to a monthly cadence.

Innovation dies and the job market shifts to maintaining legacy software.

Enter Scene: COBOL

Environment Setup

COBOL is a compiled language. Depending on your package manager, install the compiler with one of the following:

apt install open-cobol yum install open-cobol

Verify the installation:

> cobc --version

cobc (OpenCOBOL) 1.1.0

Copyright © 2001–2009 Keisuke Nishida / Roger While

Built Aug 04 2016 15:56:22

Packaged Feb 06 2009 10:30:55 CET

Program Structure

Each COBOL is program is structured into four divisions (unrelated to the algebraic definition). These divisions are:

Identification Division: This is the only required division. The other three are optional. This contains user-defined program metadata, for instance: the program name, the author’s name, date written, etc.

Environment Division: Describes the hardware environment and required files for IO.

Data Division: Defines memory allocations (variables).

Procedure Division: Defines the CPU operations (executable text).

Hello, World!

Let’s start writing our first program with the only mandatory division: the identification division. Here’s our first code snippet with the program id of “FOO_BAR”:

IDENTIFICATION DIVISION.

PROGRAM-ID. FOO_BAR.

Save this snippet as demo.cob and compile it with cobc -x -free demo.cob :

-x tells the compiler to compile an executable program.

-free tells the compiler which syntax format we’re using. This is the most modern.

If compilation succeeds, you will now have an executable demo file. If you execute it, the output will be empty. Let’s have it print “hello world!” by introducing a procedure division.

IDENTIFICATION DIVISION.

PROGRAM-ID. FOO_BAR. PROCEDURE DIVISION.

DISPLAY "hello world!"

END-DISPLAY.

GOBACK.

Compile and execute the program to receive the following output:

hello world!

Ok, great.

Variables

Let’s now introduce the data division by defining a single numeric variable and printing it.

IDENTIFICATION DIVISION.

PROGRAM-ID. FOO_BAR. DATA DIVISION.

WORKING-STORAGE SECTION.

01 my-num PIC 9(5) VALUE 12345. PROCEDURE DIVISION.

DISPLAY my-num

END-DISPLAY.

GOBACK.

The DATA DIVISION can have up to three sections:

WORKING-STORAGE : Defines variables in main memory.

: Defines variables in main memory. FILE : Defines external files

: Defines external files LINKAGE : Defines variables in other COBOL programs.

In our current snippet, we have only used the WORKING-STORAGE to define a single variable named my-num . A variable definition is structured as follows:

Data level, 01

Name, my-num

Data type, PIC 9(5)

Value, VALUE 12345

The data level defines the semantics for how variables are grouped. In this article we will only use 01 to define singular (“elementary”) variables.

The name is the symbolic, human-readable name for the variable.

The data type defines the semantics for how the variable’s memory is to be interpreted. The PIC clause (“pictorial”) defines a data type with a type and size. The 9 is used for a numeric data type and the (5) indicates a numeric byte size of 5.

The remaining pictorial data types that may be used in place of 9 are:

A for alphabetic

for alphabetic X for alphanumeric

for alphanumeric S for sign

for sign V for implicit decimal

for implicit decimal P for assumed decimal

Finally, the VALUE 12345 clause initializes the variable to a value of 12345.

Compile and execute the program to receive the following output:

12345

Arithmetic

COBOL supports 4 verbs for arithmetic: add, subtract, divide, and multiply. Let’s compute (a + b) * c where a = 1 , b = 2 , and c = 3 .

IDENTIFICATION DIVISION.

PROGRAM-ID. FOO_BAR. DATA DIVISION.

WORKING-STORAGE SECTION.

01 var-a PIC 9(5) VALUE 1.

01 var-b PIC 9(5) VALUE 2.

01 var-c PIC 9(5) VALUE 3. PROCEDURE DIVISION.

ADD var-a TO var-b GIVING var-a

MULTIPLY var-a BY var-c GIVING var-a

DISPLAY "var-a: " var-a

END-DISPLAY.

GOBACK.

In the DATA DIVISION we defined an initialized var-a , var-b , and var-c to values 1 , 2 , and 3 , respectively.

Within our PROCEDURE DIVISION :

Line ADD var-a TO var-b GIVING var-a adds var-a and var-b and stores the result in var-a . Note that COBOL sufficiently abstracts the CPU registers so that we can use a single variable as both input and output in an arithmetic statement.

adds and and stores the result in . Note that COBOL sufficiently abstracts the CPU registers so that we can use a single variable as both input and output in an arithmetic statement. Line MULTIPLY var-a BY var-c GIVING var-a multiples var-a and var-c and stores the result in var-a .

Compile and execute the program to receive the following output:

var-a: 00009

File IO

Let’s focus just on the O in IO and write the result of our calculation to an output file.

IDENTIFICATION DIVISION.

PROGRAM-ID. FOO_BAR. ENVIRONMENT DIVISION.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT result ASSIGN TO 'result.txt'

ORGANIZATION IS SEQUENTIAL

ACCESS IS SEQUENTIAL. DATA DIVISION.

FILE SECTION.

FD result.

01 var-result PIC 9(5). WORKING-STORAGE SECTION.

01 var-a PIC 9(5) VALUE 1.

01 var-b PIC 9(5) VALUE 2.

01 var-c PIC 9(5) VALUE 3. PROCEDURE DIVISION.

ADD var-a TO var-b GIVING var-a

MULTIPLY var-a BY var-c GIVING var-a OPEN OUTPUT result.

MOVE var-a TO var-result

WRITE var-result

END-WRITE.

CLOSE result. GOBACK.

We have three new blocks of code. Let’s discuss the new ENVIRONMENT DIVISION :

ENVIRONMENT DIVISION.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT result ASSIGN TO 'result.txt'

ORGANIZATION IS SEQUENTIAL

ACCESS IS SEQUENTIAL.

This is the fourth and final division available in COBOL. The defines variable result as a file with the following attributes: named result.txt, organized sequentially (as opposed to an indexed organization), and IO is performed sequentially.

The new file section defines the memory layout of the file:

FILE SECTION.

FD result.

01 var-result PIC 9(5).

Similar to how we define variables in main-memory, we define variables in files. In COBOL, files are well organized as records — unlike modern languages that generally perform IO by char objects. Here, we organize our file with a single numeric integer of byte-length 5.

Finally, we write our output from (a + b) * c to the file:

OPEN OUTPUT result.

MOVE var-a TO var-result

WRITE var-result

END-WRITE.

CLOSE result.

Although we have our desired output in var-a , we move it into file buffer variable var-result before writing it. You could WRITE var-a instead, but the compiler will yell at you because it already allocated a var-result for this specific purpose.

Conclusion

Although most truth is said in jest, the introductory section was mostly sarcasm. This brief introduction to COBOL is for entertainment purposes only.