Charles Brannon, Program Editor
Programming In Modula-2
There are a plethora of programming languages for the Amiga, giving programmers and developers a wide choice of programming styles and systems. There are two versions of BASIC (MetaComCo's ABasiC and Microsoft's Amiga BASIC), two C compilers (Manx Aztec C and Lattice C), a macro assembler/editor, two versions of Pascal, and even an implementation of LISP. Numerous programming tools, such as editors and debuggers, are also available.
A relative newcomer to the scene, TDI Modula-2, is now getting some attention. Some programmers consider it easier to learn and use than C--since it shares many of the high-level aspects of Pascal--while still retaining a machine-level interface for maximum efficiency.
Modula-2 is a descendent of the language Modula, which in turn is a descendent of Pascal. Nicklaus Wirth, the inventor of Pascal, designed Modula from the roots of Pascal, but purposely kept it very simple so that it could be used with very small computers--primarily for controlling hardware devices such as robot arms. The original Modula had little application outside a very specialized world, so Wirth put back most of the features of Pascal to create Modula-2. TDI has worked directly with Wirth to implement versions of Modula-2 for the Amiga and Atari ST.
The concept of Modula-2 is echoed in its name. It is a language designed specifically for the techniques of modular programming, just as Pascal was designed to make structured programming convenient and elegant. Modular programming--the art of breaking a large, complex problem into small, independent tasks--is at the heart of all programming, but Modula-2 tries to bring to software the modularity inherent in computer hardware, based on off-the-shelf chips and components. With "software chips," Wirth envisioned, software technology could advance apace with the remarkable speed of hardware evolution.
If software chips are possible, they have to be based on program modules that can be truly independent, hence, individually testable. You can compile a module without having to recompile the entire program. A module, once developed, becomes a "black box" routine that accepts input and/or provides output. You no longer need to know how this module works internally to use it--you just plug it in and go. Writing a program becomes a task of putting together these building blocks in the right way without ever needing to reinvent the wheel. Why solve a problem when someone else has already found the solution?
Modula-2 comes with a standard library containing modules for input/output, math routines, and access to special machine features. You use only the routines you're interested in, and only these routines (and the underlying routines they are based on) need to be included in your compiled code. This lets you control the size of your final program.
You can easily add your own library modules. First, you write the definition module, which simply contains the procedure headers that specify the inputs and outputs of a module. The definition module primarily specifies the names of these procedures. It compiles to a symbol file for use by the compiler. The implementation module contains the actual code of the module. You compile the definition module separately from the implementation module.
You can change and recompile the implementation module without changing the definition module, as long as your procedure headings remain the same. When you re referencing library modules, the compiler can check the compact, compiled symbol file rather than the full-length definition module, speeding up compilation. After compilation, a linker combines your main program with the compiled implementation modules to create the final executable program.
Reminiscent Of Pascal
One of the best ways to learn about a language is to study an example program. The program accompanying this column is written to demonstrate some of the features of Modula-2 without getting bogged down in tricky algorithms. It's a simple guess-my-number game. The RandomNumbers module thinks of a number from 1 to 100. The program then gives you ten tries to guess the number, helping out with hints. If you guess too high, the program recommends that you try a smaller number. If you guess -too low, you should try a higher number.
Here's how the program works. The first line declares the name of the module. Next, the IMPORT statements specify which external library calls we'll be using. Then we declare the variables. We define the procedure SkipEOL, used to strip away the rest of a line after getting a single-character response. The main loop follows, enclosed by the keywords BEGIN and END. (All Modula-2 keywords must be typed in uppercase, which can be annoying.)
Most of the program looks very much like Pascal, especially the use of : = for assignments and the required semicolon at the end of each logical line. Also, you won't find GOTO anywhere in this or any Modula-2 program. Instead, you can control looping and program execution with statements like LOOP-EXIT-END, WHILE-END, REPEAT-UNTIL, and IF-THEN-ELSE-END.
You might be interested to know that this program compiles in 35 seconds when the source code is stored in the RAM disk; it takes 37 seconds to compile when the source code is stored on a floppy disk. Linking takes 45 seconds from the RAM disk, and just one minute from a floppy disk. This is quite a bit faster than Lattice C and compares well with Aztec C.
There's much more to Modula-2 than this discussion can encompass. The language even permits procedures to run as multitasking programs. Our example doesn't show how easily Modula-2 can take advantage of the Amiga operating system--even a small program would be too large to demonstrate here--but the interface is similar to C's, using Pascal-style RECORDs instead of C structures. It's possible to develop modules that support the Amiga operating system on a higher level, using calls like Screen(320,200,5) to open a custom screen as opposed to filling in the blanks of a NewScreen structure, opening the Intuition library, and calling OpenScreen( ). Some high-level modules are included in the library. When these modules are developed and shared between Modula-2 programmers, Amiga programming in Modula-2 can seem almost as easy as in BASIC, but with every advantage of a modern compiled language.