First Program

Editors

Like most programming languages, a Cobol program is just a text file, which you edit in your favourite text editor. Fortunately, to avoid any religious wars, both vim and Emacs work with Cobol.

Emacs

There is no Cobol mode which comes as standard for GNU Emacs. Instead download a copy of cobol.el from the Cobol for GCC project. You can download the file directly from their CVS at http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/gcc/cobol/cobol.el?cvsroot=CobolForGCC.

Open up Emacs and compile the Lisp file with M-x byte-compile-file. Move the resulting Cobol.elc file to /usr/share/emacs/20.7/lisp/textmodes/ or the equivalent for your installation. Finally add this code to the .emacs file (or .gnu-emacs in some distributions) in your home directory:

(autoload 'cobol-mode "cobol" "COBOL Editing mode" t)
    (setq auto-mode-alist
          (append '(("\\.cpp$" . c++-mode)
                    ("\\.hpp$" . c++-mode)
                    ("\\.lsp$" . lisp-mode)
                    ("\\.scm$" . scheme-mode)
                    ("\\.pl$"  . perl-mode)
                    ("\\.cbl$" . cobol-mode)
                    ("\\.CBL$" . cobol-mode)
                    ("\\.COB$" . cobol-mode)
                    ("\\.cob$" . cobol-mode)
	            ("\\.CPY$" . cobol-mode)
                    ("\\.cpy$" . cobol-mode)
                   ) auto-mode-alist))

;; Auto font lock mode
(defvar font-lock-auto-mode-list
  (list 'c-mode 'c++-mode 'c++-c-mode 'emacs-lisp-mode 'lisp-mode
'perl-mode 'scheme-mode 'scribe-mode 'shell-script-mode 'cobol-mode
'dired-mode)
  "List of modes to always start in font-lock-mode")

(defvar font-lock-mode-keyword-alist
  '((c++-c-mode . c-font-lock-keywords)
    (perl-mode . perl-font-lock-keywords)
    (cobol-mode . cobol-font-lock-keywords)
    (dired-mode . dired-font-lock-keywords))
  "Associations between modes and keywords")

(add-hook 'cobol-mode-hook
          '(lambda ()
             (set (make-local-variable 'dabbrev-case-fold-search) t)
             (set (make-local-variable 'dabbrev-case-replace) t))

Open up a Cobol file in Emacs and type `M-x cobol-mode' and `M-x font-lock-mode' to get pretty colours. Thanks to the Cobol for GCC team for doing this Emacs mode.

VIM

For a guide to doing Cobol in VIM go to http://dimensional.com/~sitaram/cobol/.

Others

There are plenty of other editors out there. An interesting one is THE, an editor which mimics mainframe editors such as Xedit. It's available from http://www.lightlink.com/hessling/

First Program

Here is a Hello World program in Cobol:

* Hello World Program
* GPL Copyleft Jonathan Riddell 2001
       IDENTIFICATION DIVISION.
       PROGRAM-ID.    hello.
       ENVIRONMENT DIVISION.
       DATA DIVISION.

       PROCEDURE DIVISION.
           DISPLAY "hello ," WITH NO ADVANCING 
           DISPLAY "world!"
           STOP RUN.

The structure of the program is quite simple, four sections two of which have content. The PROGRAM-ID is a simple name for the program, you can optionally have other information in the IDENTIFICATION DIVISION. The ENVIRONMENT DIVISION can (optionally now) contain information about your configuration, such as the compiler you're using. The DATA DIVISION contains any variable declarations we might need. Finally the PROCEDURE DIVISION has the actual program instructions.

You might notice that there is a lot of capitals in the code. Using capitals for Cobol's reserved words keeps the program more backwards compatible with other compilers and ensures that the reserved words stand out against literals and variables. With Tiny Cobol you are perfectly free not to use capitals which will stop your Caps Lock wearing out so fast.

The other thing to notice is the spacing. Back in the days of punched cards Cobol kept a strict control on where parts of the code should be:

column 1-6   : line number.
column 7     : indicator area,		
               asterisk for comment lines,
               minus for continuation lines,
               slash for page skip on compilation listing
               otherwise space.
column 8-11  : Margin A, here starts division, section,
               paragraph identifiers and some level numbers.
column 12-72 : Margin B, for everything that does not belong
               in Margin A.
column 73-80 : program identification area.  

Tiny Cobol doesn't force you to keep to this regime but it might keep your programs more tidy if you do, it also stops any other compiler fussing about spacing and it keeps the Cobol mode in Emacs happy. Tiny Cobol does, however, insist on having the asterisk that denotes a comment in the first or second column.

Now on with compiling the program. Save the code above as hello.cob and type the following command:

	  htcobol hello

Chances are that htcobol will warn that $COBDIR isn't set, as long as you installed into the default directory this shouldn't be a problem but if you changed the directory or if you get annoyed by extraneous warning messages then add `export COBDIR=/usr/local/share/htcobol' or similar to your .bashrc or .profile file. It will also say that it is `Processing compiler parameters', this is a good thing.

htcobol will produce the files hello.lis and hello.s. hello.lis is a conversion of the Cobol code into something more compiler friendly. hello.s is in assembly code, the human readable equivalent of machine code.

Update: Tiny-Cobol no longer compiles to assembly but direct to an executable programme. The next two steps can therefore be left out.

If your shell can't find a program called htcobol firstly check that your $PATH variable points to /usr/local/bin/htcobol or wherever you installed it to (type echo $PATH). If it is there then double check that it configured, compiled and installed correctly.

If you mistyped the code above you'll have noticed that Tiny Cobol isn't yet very good at reporting errors. In fact all you'll get is a Segmentation Fault. Double check that you spelled everything correctly, didn't miss out any punctuation and the asterisks of the comments are in the first column.

The next step is to compile the assembly file hello.s into machine code. The command for this is:

as -o hello.o hello.s

as is part of the GNU Compiler Collection, GCC, and if you don't have it installed you have a very funny distribution indeed (given that you've already compiled Tiny Cobol today). Check your distribution documentation for how to install it.

hello.o is an object file, but not a runnable executable because it hasn't been linked with the necessary libraries. This is the final command:

gcc -o hello hello.o -lhtcobol -lm

If you've got this far you're unlikely to have had any problems with this, but if gcc hasn't found the libraries libhtcobol or libm then you might have to update the libraries database with the ldconfig command.

Finally there is a working program which can be run with:

./hello

And you should see the output:

Hello, world!

Which was quite predictable. If you look at the program code it should be obvious what DISPLAY does and what WITH NO ADVANCING does (prints without a line break).