3
1

More than 3 years have passed since last update.

MacでCOBOLのプログラムを動かすまで

Posted at

基本情報や応用情報の合格発表がSNSで散見される季節になりましたね。
工業高校生の頃にCOBOLで基本情報技術者試験を受験したのを思い出して、久しぶりに触ってみたくなりました。

もういまはCOBOLは選択できなくなって、Pythonに代わったらしいですね。

macOS Big Sur バージョン11.1
で作業しました。

余談ですが、BigSurではThunderbirdが激重です。
Thunderbirdユーザーの方は気をつけてください。

cobolのパッケージの検索

% brew search cobol 
==> Formulae
gnu-cobol

cobolのパッケージのインストール

% brew install gnu-cobol
Updating Homebrew...
・・・
🍺  /usr/local/Cellar/gnu-cobol/3.1.2: 64 files, 2.6MB

パッケージのバージョンの確認

% cobc -V
cobc (GnuCOBOL) 3.1.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Keisuke Nishida, Roger While, Ron Norman, Simon Sobisch, Edward Hart
Built     Dec 23 2020 19:29:52
Packaged  Dec 23 2020 12:04:58 UTC
C version "Apple LLVM 12.0.0 (clang-1200.0.32.28)"

サンプルソース

ちょっとおしゃれにDIVIDEを使ってみました

hello.cob
       IDENTIFICATION    DIVISION.
       PROGRAM-ID.       COUNT.
       DATA              DIVISION.
       WORKING-STORAGE   SECTION.
       01 CNT   PIC 9(3) VALUE 0.
       01 A     PIC 9(3) VALUE 0.
       01 B     PIC 9(3) VALUE 0.
       PROCEDURE         DIVISION.
          PERFORM 100 TIMES
              ADD 1 TO CNT
              DIVIDE CNT BY 5 GIVING A REMAINDER B
              IF B = 0 THEN
                DISPLAY "COUNT = " CNT
              END-IF
          END-PERFORM
          STOP RUN.

コンパイルと実行

% cobc -x -Wall -debug hello.cob
% ./hello 
COUNT = 005
COUNT = 010
COUNT = 015
COUNT = 020
COUNT = 025
COUNT = 030
COUNT = 035
COUNT = 040
COUNT = 045
COUNT = 050
COUNT = 055
COUNT = 060
COUNT = 065
COUNT = 070
COUNT = 075
COUNT = 080
COUNT = 085
COUNT = 090
COUNT = 095
COUNT = 100
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1