Pascal Compilers

This document will explain the basics about compilers as well as provide links to well-known Pascal compilers and explain how to set up Free Pascal.

About Computer Languages and Compilers

When talking about computer languages, there are basically three major terms that will be used.

  1. Machine language -- actual binary code that gives basic instructions to the computer's CPU. These are usually very simple commands like adding two numbers or moving data from one memory location to another.
  2. Assembly language -- a way for humans to program computers directly without memorizing strings of binary numbers. There is a one-to-one correspondance with machine code. For example, in Intel x86 machine language, ADD and MOV are mnemonics for the addition and move operations.
  3. High-level language -- permits humans to write complex programs without going step-by step. High-level languages include Pascal, C, C++, FORTRAN, Java, BASIC, and many more. One command in a high-level language, like writing a string to a file, may translate to dozens or even hundreds of machine language instructions.

Microprocessors can only run machine language programs directly. Assembly language programs are assembled, or translated into machine language. Likewise, programs written in high-level languages, like Pascal, must also be translated into machine language before they can be run. To do this translation is to compile a program.

The program that accomplishes the translation is called a compiler. This program is rather complex since it not only creates machine language instructions from lines of code, but often also optimizes the code to run faster, adds error-correction code, and links the code with subroutines stored elsewhere. For example, when you tell the computer to print something to the screen, the compiler translates this as a call to a pre-written module. Your code must then be linked to the code that the compiler manufacturer provides before an executable program results.

With high-level languages, there are again three basic terms to remember:

  1. Source code -- the code that you write. This typically has an extension that indicates the language used. For example, Pascal source code usually ends in ".pas" and C++ code usually ends in ".cpp"
  2. Object code -- the result of compiling. Object code usually includes only one module of a program, and cannot be run yet since it is incomplete. On DOS/Windows systems, this usually has an extension of ".obj"
  3. Executable code -- the end result. All the object code modules necessary for a program to function are linked together. On DOS/Windows systems, this usually has an extension of ".exe"

More About Compilers

The de facto standard in DOS and Windows-based compilers is Borland Pascal. Before it came out, most Pascal compilers were clumsy and slow, strayed from the Pascal standard, and cost several hundred dollars. In 1984, Borland introduced Turbo Pascal, which sold for less than $100, compiled an order of magnitude faster than existing compilers, and came with an abundance of source code and utility programs.

This product was a great success and was prominent for almost a decade. But in the 1990s, the world was moving to Windows. In 1993, the last version of Turbo Pascal, version 7 for DOS, came out. After that, the demand for DOS programs plummetted and Borland (renamed Inprise, then back to Borland) focused on producing Windows compilers.

This tutorial will only deal with console-based programming, where the computer prints lines of data to the screen and the user interacts with the program using a keyboard. The goal of the tutorial is to teach how to program in Pascal. Once you've learned that, you can easily look at a reference book or another web page and pick up graphics and windowing systems on your own.

Although old commercial Pascal compilers are often available for download, Turbo Pascal 5.5 from Embarcadero's Antique Software and Symantec Think Pascal (Macintosh) from Mac GUI, computers have progressed much since the 1980s and early 1990s. We are no longer stuck with 8.3 filenames on DOS or non-preemptive multitasking on Mac OS. Using an old compiler is fun in the same sense as playing an old game on an emulator, but the open source movement has produced good compilers for modern operating systems, and a beginner will find it much easier to use those.

Open Source Compilers

The two main open-source compiler projects are:

Free Pascal is generally considered friendlier for novices, and strives to emulate Borland Pascal in many ways, though both will serve fine for learning Pascal. The easiest way for a novice to get started with Pascal programming is to download the Lazarus IDE, which provides a friendly, GUI-based environment for the Free Pascal compiler.

Here's how to get started, step by step, on Windows. Lazarus and Free Pascal are available on other operating systems too; some steps will differ slightly.

  1. Download Lazarus. If you're not sure whether you're running 32-bit or 64-bit, select 32-bit since it is compatible with both.
  2. Run through setup, accepting all the defaults.
  3. Open Lazarus from the Lazarus folder in the Start menu.
  4. Select the File-New... menu item, then select Program under the Project subtree.
  5. Lazarus will create a source file with a bunch of stuff filled in. This is more complicated than you need so far, so select all the content and delete.
  6. Type in a program (flip to the next lesson to get a "Hello, world." program).
  7. Save the file with File-Save As..., somewhere where you can find it easily later on. For example, I saved the file as hello in a directory on my desktop. Lazarus will create two files: hello.lpr which contains what I've typed in; and the project file, hello.lpi. Later, you can double-click on either file to open the program in Lazarus.
  8. Run the program from the Run menu. This will automatically compile the program if you've made any changes, then run the program. It will also run the program without compiling if you've not made any changes since the last time you compiled.

With programs that don't expect user input, you'll see a window flash up and disappear. This is because the program finishes running in the blink of an eye, and closes itself. You are returned to the IDE without seeing the results of your work. There are two ways around this:

  • Add a readln statement at the end of every program. Now the program will wait for you to press the Enter key before it closes. If you're not familiar with the Command Prompt, this is probably the easiest way to get what you want.

  • Note that a .exe file was created in the directory where you saved your program. This is the executable. You can go to the Command Prompt, change to the directory, and run this executable straight. You can also double-click on it in Windows Explorer (and it will still flash by quickly if it ends without requiring user input).

We'll be learning only the fundamentals of Pascal in this tutorial, so we'll be sticking to console programs that print text to the screen and interact with the keyboard. However, Lazarus and Pascal are capable of producing programs as complicated as any that you have on your computer right now. A web browser? A spreadsheet? A game? All possible, with thousands (or millions!) of man-hours of effort.