I wrote COBOL ~ Execute the program from the environment construction by Ubuntu and GnuCOBOL ~

Hi, this is Yuichi, an N high school student.

This time I wrote COBOL with interest in mind. What kind of technology is the reason why I decided to touch COBOL, which I am not familiar with?

I found out while researching various things, so I will explain for beginners.

Overview

When creating a COBOL program, create the program after building the environment of Ubuntu or GnuCOBOL (formerly openCOBOL).

I'm Windows 10 and I don't even have Ubuntu, let alone GnuCOBOL. So, it is a form to prepare from scratch.

Premise

--Use Windows 10 --Do it on Ubuntu. --Use the vi editor.

1. Install Ubuntu 20.04 LTS on Windows 10

WSL is required to use GnuCOBOL on Windows, so install WSL and Ubuntu.

1-1. Enable WSL

Search for Turn Windows features on or off. When the Windows Features screen appears, check Windows Subsystem for Linux. The language may change depending on the environment, so if you do not have the above check item, look for the item Windows Subsystem for Linux and check it.

ac43d6e8c363541fa23b972757072e3c.png

1-2. Install Ubuntu 20.04 LTS

Search for "Ubuntu 20.04 LTS" from the MicroSoft Store → Get → Start

無題.png

After installation, you will only be prompted for your username and password for the first boot. The password also includes entering the confirmation password, so enter the password as well.

Enter new UNIX username: <username>
New password: <password>

This completes the setup for the time being.

[Bonus] I tried to renew the terminal

It's okay to start Ubuntu terminal, but I'll change it to a terminal that has a Mac feel.

Search for "Windows Terminal Preview" in the MicroSoft Store → Get → Launch.

無題.png

When you start it, a mac-like terminal will be displayed.

image.png

Click the down arrow (drop-down menu) in the title bar to display the menu. The terminals available on that PC are reflected here, and you can start any terminal. (PowerShell, command prompt, etc.) All you have to do is select Ubuntu 20.04 and you're done.

無題.png

This completes the new terminal.

image.png

2. Install GnuCOBOL

Execute the following command on the terminal.

--For Windows 10

Use WSL to use GnuCOBOL on Windows. Since I prepared Ubuntu for installation etc. earlier, execute the following command.

sudo apt update
sudo apt install open-cobol

--For macOS

Home Brew is used on macOS. You can install it from here.

brew install gnu-cobol

It will take more than 10 minutes to complete, so make good use of your time by reading a book during that time.

3. Create a COBOL program

Finally the main subject. Let's actually write a program using COBOL. The procedure is as follows.

3-1. Write Hello, World!

  1. Check the location of the current directory.

Execute the pwd command.

$ pwd
/mnt/c/Users/user
  1. Create an arbitrary directory Here, create a source file called dev directory, hello.cbl. As expected, execute the vi command and write the program with the vi editor.
$ mkdir dev
$ cd dev
$ vi hello.cbl //Create COBOL source file
  1. Write the source code.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.
       PROCEDURE DIVISION.
       DISPLAY "Hello, World!".
       STOP RUN.
  1. Compile The -x option is an option for creating an executable file.
$ cobc -x ./hello.cbl
  1. An executable file is created. The -x option I mentioned earlier created a file named hello as an executable file.
$ ls
hello  hello.cbl
  1. Run the program. It is clear when the result is output!
$ ./hello
Hello, World!

Add and display from 3-2.1 to 10.

       IDENTIFICATION DIVISION.
       PROGRAM-ID.loop.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 I PIC 9(2).
       01 CNT PIC 9(2).
       PROCEDURE DIVISION.
       MAIN.
        MOVE 0 TO I
        MOVE 0 TO CNT
           PERFORM UNTIL I >= 10
            ADD 1 TO I
            ADD I TO CNT
            DISPLAY "I=" I ", CNT=" CNT
           END-PERFORM.
           DISPLAY "CNT=" CNT
           STOP RUN.
           END PROGRAM loop.

Declare variables under DATA DIVISION. and WORKING-STORAGE SECTION.. The range from PERFORM to END-PERFORM repeats the process, Write as PERFORM UNTIL condition.

After that, when you execute it, it will be as follows.

I=01, CNT=01
I=02, CNT=03
I=03, CNT=06
I=04, CNT=10
I=05, CNT=15
I=06, CNT=21
I=07, CNT=28
I=08, CNT=36
I=09, CNT=45
I=10, CNT=55
CNT=55

3-3. Write a FizzBuzz problem


       IDENTIFICATION DIVISION.
       PROGRAM-ID. FizzBuzz.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 N PIC 9(3) VALUE 1.
       01 FIZZ PIC 9(1).
       01 BUZZ PIC 9(1).
       PROCEDURE DIVISION.
           PERFORM 100 TIMES
                   COMPUTE FIZZ = FUNCTION MOD(N 3)
                   COMPUTE BUZZ = FUNCTION MOD(N 5)
                   IF FIZZ = 0 AND BUZZ = 0 THEN
                           DISPLAY "FizzBuzz"
                   ELSE IF FIZZ = 0 THEN
                           DISPLAY "Fizz"
                   ELSE IF BUZZ = 0 THEN
                           DISPLAY "Buzz"
                   ELSE
                           DISPLAY N
                   END-IF
                   END-IF
                   END-IF
                   ADD 1 TO N
           END-PERFORM.
           STOP RUN.

With PERFORM 100 TIMES, it repeats 100 times. COMPUTE FIZZ = FUNCTION MOD (N 3) assigns the remainder of N divided by 3 to the variable FIZZ.

↓ Execution

001
002
Fizz
004
Buzz
Fizz
007
008
Fizz
Buzz
011
Fizz
013
014
FizzBuzz
...

Try it out

That's all from building the COBOL environment to writing the program. By design, COBOL is characterized by redundant syntax, and I think it's a language with a strong habit. The code of the FizzBuzz problem is not so different from other programming languages ​​such as the if else part.

For those who want to write in VScode, you can use syntax highlighting and input completion by using an extension called "COBOL", so please use it. (Is there anyone who wants to write COBOL?)

Reference site

-COBOL is a hot topic in corona ?! Languages ​​over 60 years ago are a hot topic recently -Install Ubuntu 20.04 LTS on Windows 10 -[COBOL] Until even a first-time user can easily build a development environment (OpenCOBOL) and create and execute Hello World programs

Recommended Posts

I wrote COBOL ~ Execute the program from the environment construction by Ubuntu and GnuCOBOL ~
Protobuf and gRPC C ++ environment construction on Ubuntu 18.04
21 Load the script from a file and execute it
Prepare the environment for java11 and javaFx with Ubuntu 18.4
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
Try switching from the RHEL environment to the Ubuntu environment Server installation
I tried to build the environment little by little using docker
How to remotely debug a javaFX program executed by Raspberry Pi from Windows 10-From environment construction to debug execution ①-