User Tools

Site Tools


turbo_pascal_cookbook

Turbo Pascal Cookbook

Here is a collection of programs and tutorials for using Turbo Pascal on DOS. I recommend using either FreeDOS or DosBox-X. DosBox-X is easier to get started with and will run on any host operating system like Windows, MacOS, or Linux. FreeDOS will require you to use a virtual machine like VirtualBox or installing it directly on to physical hardware.

Download

You can get a copy of Turbo Pascal and the books from The Internet Archive. You can also find copies on WinWorld.

Installation and setup

See the dedicated page: Installing Turbo Pascal on DOSBox-X

What comes with the installation?

Here is a high level look at what comes with a fresh install.

  • TP\BGI\ - Borland Graphics Interface, drivers, fonts, tools for drawing graphics like shapes, text, and images.
  • TP\BIN\ - Executable programs like TURBO.EXE (the IDE itself), the TPC compiler, GREP and MAKE utilities.
  • TP\DOC\ - Documentation and help files
  • TP\EXAMPLES\ - Examples for learning and reference
  • TP\SOURCE\ - Source code for the standard units
  • TP\UNITS\ - Standard units (libraries)
  • TP\README.COM - Executable version of README
  • TP\FILELIST.DOC - Doc with complete list and description of all files
  • TP\README - How to get help, system requirements, installation notes,
  • TP\UNZIP.EXE - Unzip utility

Running the examples

The first thing I'd do to get familiar and see what Turbo Pascal can do is run some of the examples that come with the installation. This will help you understand how to build and run programs and get a peek at source code.

Standalone PAS files

The simplest examples are single standalone .PAS files. You can open these in the IDE and go to Run → Run in the menu. You can open them directly from the command line like this:

turbo C:\TP\EXAMPLES\CRTDEMO.PAS

Or you can open Turbo Pascal and go to File → Open and navigate to the examples.

Once the example program is loaded, go to Run → Run or press CTRL+F9.

Examples with multiple files

Some examples require multiple files to be compiled. Move in to that directory first, either before launching turbo using the cd command or from within the IDE with File → Change dir in the main menu.

For example, the BREAKOUT.PAS example in TP\EXAMPLES\BREAKOUT\. There are multiple files in that directory and the program needs all of those files compiled. If you try to run BREAKOUT.PAS by itself from outside the directory you will get an error like Error 15: File not found (SCREEN.TPU).. This is because SCREEN.PAS is another file in that directory that needs to be compiled into a unit (.TPU)) first so it's available to the main program. If you change in to that TP\EXAMPLES\BREAKOUT\ directory and then try to run it, it will correctly compile all the files in the directory.

Another option, once you have compiled all the supporting files is to manually set the directories under Options → Directories. Here you can set the EXE & TPU directory so it knows where to find the .TPU files. I'd avoid doing this method though and stick with changing to the right working directory.

Difference between Compile, Make, Build, and Run

The programs that consist of many different files need to have all files compiled in order to run. You may need to run Make instead of just Compile. Especially if you see an error like Error 15: File not found (SOME.TPU). Running Make will build all the files in the directory including some support files that may create unit files (.TPU) that support the program. Here is an explanation of the difference between all these commands:

  • Compile - compiles only the current file by itself.
  • Build - builds all files in the project from scratch.
  • Make - similar to build, but will only compile files that have changed.
  • Run - performs the Make command, and then runs the program.

Compiling to disk vs memory

Some examples can't compile to memory and must compile to disk.

In the Compile menu you will see an item named Destination. It will either say Destination Memory or Destination Disk. Click on the Destination item in the menu to flip between these two settings. Compiling to memory is faster and saves disk IO time and wear. Compiling to disk creates a compiled file that will persist. Some programs will require you to compile to disk and won't run if you only compile to memory. You can permanently save this setting once set by going to Options → Save TP\BIN\TURBO.TP which will write the default configuration file. Note this will also save whatever open files you have as default so I recommend doing it on a fresh load or after closing all files so it doesn't always open the same file when you start.

Accesssing built-in help and more examples

In the IDE (TURBO.EXE), go to the Help menu in the top. You can press ALT+H to open the menu. Explore the help tools there to learn more about the language and the libraries. There are many examples there to learn from.

Cookbook programs

Hello, world

hello.pas
program hello;
 
begin
  Writeln('Hello, world!);
end.

Now run directly in the IDE or compile and run in the CLI:

tpc hello.pas
hello

Clear screen

cls.pas
program cls;
 
Uses Crt;
 
begin
  clrscr;
end.

Get text input from user

getname.pas
program getname;
 
Uses Crt;
 
var
   myString : String;
 
begin
   Write('Enter your name: ');
   Readln(myString);
   Writeln('Hello, ', myString, '!');
end.

Command line arguments

cli_args.pas
program cli_args;
 
var
   I : Word;
 
begin
     {ParamCount has the number of CLI args. 0 if none are provided}
     {ParamStr has the arguments provided}
     Writeln('You provided ', ParamCount, ' parameters.');
     for I := 1 to ParamCount do
         Writeln(ParamStr(I));
end.

Inline Assembly

asmAdd.pas
{ Example of using inline assembly. You can directly reference Pascal variables in the assembly.}
program asmAdd;
 
var
  x, y, sum : Integer;
 
begin
  x := 10;
  y := 5;
 
  Asm
    mov ax, x
    add ax, y
    mov sum, ax
  end;
 
  Writeln('x + y = ', sum);
end.

Wait and read keypress (using Crt)

waitkey.pas
{ Wait for a key press, using the Crt unit. }
program waitkey;
 
Uses Crt;
 
var
   c : Char;
 
begin
  {Note if you press a function key like F2, or an arrow key,
   it sends two key codes
   so it will trigger BOTH ReadKey calls even though you pressed 1 key.}
  Writeln('Press any key to continue...');
 
  c := ReadKey;
  if c = #0 then { Check if char received was null }
     { keycode recieved was null, meaning it's an extended keycode sent in
      two bytes, like a function key F1 or an arrow. }
    Writeln('Extended keycode received. Getting second code.');
    c := ReadKey;
  Writeln('You pressed: ', c);
 
end.

Is a key pressed?

iskeydow.pas
{ Loops and handles key presses }
program iskeydown;
 
Uses Crt;
 
var
  key : Char;
 
begin
  clrscr;
  Writeln('Press any key to continue. Press "q" to quit.');
  repeat
    if KeyPressed then
    begin
      Writeln('Key pressed!');
      key := ReadKey; { KeyPressed will continue to be true until ReadKey }
      if key = #0 then { If key is null, 2-byte keycode }
        Writeln('Extended key: 00 + ', ReadKey)
      else
        Writeln('Key: ', key);
    end;
    delay(100); { reduce CPU load a little }
 
  until key = 'q';
  Writeln('Quitting.');
end.

Wait and read keypress (using BIOS interrupt)

waitkey2.pas
program waitkey2;
{ Waits for keypress without using Crt unit.
 
  Uses BIOS interrupt 16h which is the keyboard interrupt.
 
  In the high part of AX reg (AH), provide the function code to
  specify what you want to do.
 
  00h = Read key
  01h = Check key status
  02h = Get shift flags
  10h = Read expanded keyboard buffer
  11h = Obtain status of expanded keyboard buffer
  12h = Get expanded keyboard status
}
 
var
  keyCode, extKeyCode : Char;
 
begin
  Writeln('Press any key to continue...');
 
  Asm
    xor ax, ax {Set AX to 00h. Alternatively use mov AH, 00h}
    int 16h
    mov keyCode, al {Store AL in Pascal variable}
    mov extKeyCode, ah
  end;
 
  if keyCode = #0 then
  begin
    Writeln('Key was null, so extended key was received.');
    Writeln('Checking AH for second byte.');
    Writeln('Extended ScanCode: ', extKeyCode);
  end
  else
    Writeln('You pressed, "', keyCode, '"');
 
{ NOTE if the mouse cursor breaks after running this program, include the
  "uses Crt;" and it will properly restore the mouse. }
 
end.

To do

  • functions and procedures ( & passing by reference )
  • random numbers
  • Working with binary (hex, binary, decimal, base64 conversion and printing)
  • Read/write text files & binary files
  • Making a re-usable unit
  • Using units like Crt for terminal control, GotoXY or via BIOS interrupts/assembly
  • Playing PC speaker
  • Calling interrupts (using asm vs Intr()) - changing screen mode
  • Catching interrupts (BIOS vs DOS interrupts) - catching mouse or keyboard events
  • Port[]
  • Getting mouse input using Int33h & Resetting mouse for IDE
  • Direct memory access
    • Text mode VGA
    • Graphics mode VGA
    • Using BDA (BIOS data area)
    • Pointers
    • Switching grahpics mode and drawing straight to graphics memory (and switching back and recapturing mouse)
  • Using BGI
  • Using turbo vision
  • Exploring other standard units
  • Playing sound blaster/adlib audio
turbo_pascal_cookbook.txt · Last modified: by nanodano

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki