Exploring Borland Turbo Pascal for DOS

I’ve been having some more adventures in VirtualBox, messing around with DOS software. This time I thought I’d explore Borland Turbo Pascal. I have an affinity for ancient programming languages – Pascal, Fortran, BASIC, Lisp, etc. I like them for basically the same reason I like vintage computers and old operating systems. It’s like exploring ancient ruins from a bygone era, uncovering the mysteries of a distant past. I wasn’t alive back then, so obviously I’m driven more by the sense of mystery than by nostalgia.

Pascal was invented in 1970 by Niklaus Wirth. Wirth is one of the most prolific programming language inventors in history, having created an entire family of languages known as the Wirth languages. These started with Algol W – a spin-off of Algol 60 invented in 1966. Wirth then went on to create Pascal, Modula, Modula 2, Modula 3, and Oberon. These languages are all syntactically similar, and have similar programming constructs and data types.

The structure of Pascal and the other Wirth languages is rather different from the languages we are used to today. It was designed to be lightweight and easy to learn and to encourage structured programming. To that end, each Pascal program is divided into discrete sections devoted to different types of statements. Some of Pascal’s features are relatively unique to the Wirth languages – most notably the presence of set types and set theoretical operations, which I haven’t seen in any other languages.

Borland Turbo Pascal is Borland’s implementation of the Pascal programming language. It includes an editor, compiler, linker, assembler, and debugger. I got my copy from this download page on WinWorldPC.com. The installation looks like basically any other Borland product, though I didn’t need a product key this time.

Installing Borland Turbo Pascal

Installing Borland Turbo Pascal

Installing Borland Turbo Pascal

Readme

I added C:\TP\BIN to my PATH variable and then typed turbo at the command prompt. This brings me to an interface that looks like this:

Starting Borland Turbo Pascal

When I first used this program, I couldn’t figure out why it wasn’t generating executable files when I ran the compile command. Later I realized that there was a certain option I had to toggle in the Compile menu. This menu item allows you to specify whether the compiled binary is to be stored in memory or on the disk (the default is memory).

Compile to memory

Compile to disk

Now we’re ready to write programs and compile them into executable files.

Next thing to do is figure out how to write a basic Hello World program. I started looking at the online documentation for Borland Turbo Pascal, as well as some resources on the web. Borland Turbo Pascal provides ample documentation from the Help menu. It actually provides multiple resources, including a reference manual as well as a guide to the language itself.

Online documentation

Online documentation

A Hello World program in Pascal would look like this:

Hello World program

Now let’s save the file…

Saving a program

Compiling and running the code produces the following output. Success!

Compiling a program

Running a Hello World program

Aaaand for some reason it says “Destination: Memory” in the screenshot. I guess I forgot to change it that time around (I will often go through a process multiple times – once to test it and a second time to get screenshots to post in the Internet). In any case, I did get the binary and managed to run it successfully. šŸ™‚

Next I thought I would write a somewhat more complex (though still rather trivial) program and try out the debugging features in Borland Turbo Pascal. I wrote a program that takes two numbers as input, adds them together, and displays the result:

Debugging

I then went to the Debug menu and started adding breakpoints.

Debugging

Debugging

Debugging

Debugging

Debugging

I set two breakpoints on lines 9 and 10 respectively. Next I went to the Run menu and selected Run.

Debugging

Debugging

The program runs until it reaches the first breakpoint, at which point it stops and returns to the IDE, with the location of the breakpoint selected.

Debugging

Now you can examine various aspects of the program execution at various points in time. You can look at the call stack for example:

Debugging

Debugging

At this point the only thing on the call stack is the main program: Add. Had I set the breakpoint inside a function or procedure, there would be more items on the call stack.

You can also examine the contents of the registers:

Debugging

Debugging

When we’re done examining the current instruction, we can step over to the next instruction and examine that one.

Debugging

Debugging

Now if we run the program again, it starts at the last breakpoint and goes to the next one.

Debugging

Though it turns out this is not actually necessary as we can simply step over all the remaining instructions one by one and examine each one in turn, after which we use the Program Reset command to remove the breakpoints and rewind the program back to the beginning.

So anyway, that’s the basic process of debugging a program in Borland Turbo Pascal. I assume the process is almost identical in other Borland IDE’s such as Borland Turbo C, Turbo C++, Turbo Basic, etc.

Next I thought I’d look at some of the other menus. There are a number of options provided by Borland Turbo Pascal, controlling many different aspects of the software, from the compiler to memory usage to the look and feel.

Compiler options

Memory options

Color preferences

Preferences

Preferences

There’s also an Information command, which shows a dialogue box with all the important information about the program and the current settings:

Information

Moving on to some more programming, I read some more of the tutorials and learned about the CRT library, which is used to produce visual effects on the screen. Libraries in Pascal are called units, and they have the .tpu extension. You can include a TPU file in a Pascal program by using the Uses keyword.

The following program changes the background color to blue and the text color to bright white, then displays the message “Hello, World!” and waits half a second to return to the IDE or prompt.

Using the CRT library

Using the CRT library

Using the CRT library, I wrote a display hack that progressively draws multicolored stars against a black background.

Using the CRT library

Here’s a complete listing of the program for reference:


 1 {Display hack that draws stars on the screen}
 2 Program Sparkle;
 3 Uses Crt;
 4 Var
 5    Counter, X, Y, Ch, Color : Integer;
 6 Begin
 7      Clrscr{Clear screen}
 8      Textbackground(Black); {Background color}
 9      Randomize{Seed the randomizer}
10      For Counter := 1 to 1000 do
11      Begin
12           X := Random(80); {X position}
13           Y := Random(25); {Y position}
14           Color := Random(7); {Star color}
15           Ch := Random(4); {Star character}
16           Gotoxy(X,Y); {Move cursor}
17           {Select color:}
18           If Color = 0 Then Textcolor(Red);
19           If Color = 1 Then Textcolor(Green);
20           If Color = 2 Then Textcolor(Blue);
21           If Color = 3 Then Textcolor(Yellow);
22           If Color = 4 Then Textcolor(Cyan);
23           If Color = 5 Then Textcolor(Magenta);
24           If Color = 6 Then Textcolor(White);
25           {Draw star:}
26           If Ch < 2 Then Write(' ');
27           If Ch = 2 Then Write('.');
28           If Ch = 3 Then Write('+');
29           Gotoxy(0,0); {Home cursor}
30           Delay(20); {Delay 20 milliseconds}
31       End;
32 End.

And a screenshot of one frame of the display hack:

Using the CRT library

To round out this article, I want to go through three more Pascal language features that I learned from the tutorials: functions, file handling, and set types.

The following program demonstrates how functions are used in Pascal. Pascal has two kinds of subprograms: functions and procedures. The difference between the two is that functions always have a return value whereas procedures never do. This program is a fairly simple demonstration of calculating the length of the hypotenuse of a triangle using the Pythagorean theorem.


 1 {Calculates the hypotenuse length of a triangle
 2  using the Pythagorean theorem}
 3 Program Py;
 4 Uses Crt;
 5 Var SizeA, SizeB : Real;
 6 Function Pythagorean(A: Real; B: Real) : Real;
 7 Begin
 8      Pythagorean := SQRT( A * A + B * B );
 9 End;
10 
11 Begin
12      Write('Enter the size of Side A: ');
13      Readln(SizeA);
14      Write('Enter the size of Side B: ');
15      Readln(SizeB);
16      Writeln('Side C: ',Pythagorean(SizeA,SizeB));
17      Readkey;
18 End.

The following program demonstrates the use of file handling functions. Like the last two programs it is nontrivial and actually has some practical use. It counts the lines in a file.


 1 {Cbunts lines in a file}
 2 Program LnCount;
 3 Uses Crt;
 4 Var
 5    Count : Integer;
 6    UserFile : Text;
 7    FileName, TLine : String;
 8 Begin
 9      Writeln('Enter filename: ');
10      Readln(Filename);
11      Assign(UserFile,Filename);
12      Reset(UserFile);
13      Count := 0;
14      Repeat
15            Readln(UserFile,TLine);
16            Count := Count + 1;
17      Until Eof(UserFile);
18      Close(UserFile);
19      Writeln('Line count: ',Count);
20      Readkey;
21 End.

Finally, I want to look at set types, because I think these constitute one of the most interesting features of Pascal and the other Wirth languages. Sets are data structures that contain members of a certain type and within a certain range. The range must be restricted to 255 members, as this is the maximum size of a set type in Pascal. The following screenshots show a rather trivial program that demonstrates the operations of union, intersection, and set difference on sets of character values.

Using set types

Using set types

Well, that’s about it for now. See ya.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s