Turbo Pascal and legacy code
last modified November 19, 2026 Free Pascal 3.2.2
Turbo Pascal dominated MS-DOS programming in the 1980s and early 1990s, and a
large amount of code from that era still exists. Free Pascal can compile much of
it in {$mode tp}, a compatibility mode that restores 16-bit integer
sizes, ShortString, old-style objects, the crt unit,
and the loose syntax of the original compiler. This article shows what the mode
changes, how the old constructs look, and how to move a unit to ObjFPC without
rewriting it from scratch.
What Turbo Pascal mode changes
The mode is selected with a compiler directive, so one file can use the old
dialect while the rest of the project stays in ObjFPC. The most visible
difference is the size of the basic types: in Turbo Pascal an
Integer is 16 bits, and a plain string is a
ShortString with a length byte and at most 255 characters.
program tpsizes;
{$mode tp}
begin
WriteLn('Integer: ', SizeOf(Integer));
WriteLn('Word: ', SizeOf(Word));
WriteLn('LongInt: ', SizeOf(LongInt));
WriteLn('Real: ', SizeOf(Real));
WriteLn('string: ', SizeOf(string));
end.
Integer and Word are two bytes, LongInt
is four, and string is 256 bytes. The program does not need
{$H+}, and it does not use a uses clause because
WriteLn is a compiler built-in. On a 64-bit machine the mode still
compiles to 64-bit code; only the type sizes and the syntax rules follow the
old dialect.
$ fpc tpsizes.pas $ ./tpsizes Integer: 2 Word: 2 LongInt: 4 Real: 8 string: 256
Screen and keyboard with crt
The crt unit provides the text-screen routines that Turbo Pascal
programs used: TextColor, TextBackground,
GotoXY, ClrScr, ReadKey, and
KeyPressed. The following program writes a coloured line at a
fixed position. When its output is a pipe or a file, the cursor movements have
no visible effect and only the text remains; in a terminal the line appears at
column 5 of row 3.
program tpcrt;
{$mode tp}
uses
crt;
begin
TextColor(Yellow);
GotoXY(5, 3);
WriteLn('Hello from Turbo Pascal mode');
TextColor(LightGray);
GotoXY(1, 5);
end.
ReadKey reads one character without waiting for Enter, which is
how old menus and games handled input. It needs a terminal for interactive use;
when standard input is a pipe it returns the character with code 0 at end of
input, so a converted program should still check KeyPressed or
replace the call with ReadLn. The unit is available on Linux
through the console driver, not only on DOS.
$ fpc tpcrt.pas $ ./tpcrt Hello from Turbo Pascal mode
Old-style objects
Before Delphi introduced classes, Turbo Pascal 5.5 added object
types: records with methods, allocated on the stack and passed by value unless
a pointer is used. The declaration and the method bodies are separate, and the
method bodies repeat the type name. The classic stack example shows the shape
of such code.
program tpstack;
{$mode tp}
type
TStack = object
Items: array[1..5] of Integer;
Top: Integer;
procedure Init;
procedure Push(Value: Integer);
function Pop: Integer;
function IsEmpty: Boolean;
end;
procedure TStack.Init;
begin
Top := 0;
end;
procedure TStack.Push(Value: Integer);
begin
Inc(Top);
Items[Top] := Value;
end;
function TStack.Pop: Integer;
begin
Pop := Items[Top];
Dec(Top);
end;
function TStack.IsEmpty: Boolean;
begin
IsEmpty := Top = 0;
end;
var
S: TStack;
begin
S.Init;
S.Push(10);
S.Push(20);
S.Push(30);
while not S.IsEmpty do
WriteLn(S.Pop);
end.
The function result is assigned to the function's own name, which is the
Turbo Pascal style; Result does not exist in this mode. The object
has no constructor, so Init initializes the fields explicitly.
There is no inheritance, no virtual method table, and no heap allocation
unless the object is pointed to.
$ fpc tpstack.pas $ ./tpstack 30 20 10
Absolute variables
The absolute clause places a variable at the same address as
another variable, or at a fixed address. It was used to inspect the parts of a
number, to overlay a structure on a buffer, and to reach memory-mapped hardware.
The example declares a record of two words and overlays it on a
LongInt.
program absvars;
{$mode tp}
type
TWords = record
Lo: Word;
Hi: Word;
end;
var
Value: LongInt;
Parts: TWords absolute Value;
begin
Value := $00010002;
WriteLn('Value: ', Value);
WriteLn('Lo: ', Parts.Lo);
WriteLn('Hi: ', Parts.Hi);
end.
The low word is 2 and the high word is 1, which reflects the little-endian
layout of x86. On a big-endian machine the two fields would be reversed; the
overlay describes memory, not arithmetic. A fixed address such as
absolute $B800:0000 is DOS-specific and must never appear in
portable code.
$ fpc absolute.pas $ ./absolute Value: 65538 Lo: 2 Hi: 1
Converting to ObjFPC
Most legacy units convert mechanically. Replace {$mode tp} with
{$mode objfpc}{$H+}, change object to
class, add a constructor, and use Result instead of
the function name. The stack below is the converted version of the previous
program; its behaviour is unchanged, but it now has a constructor, a
destructor inherited from TObject, and reference semantics.
program objstack;
{$mode objfpc}{$H+}
type
TStack = class
private
FItems: array[1..5] of Integer;
FTop: Integer;
public
constructor Create;
procedure Push(Value: Integer);
function Pop: Integer;
function IsEmpty: Boolean;
end;
constructor TStack.Create;
begin
FTop := 0;
end;
procedure TStack.Push(Value: Integer);
begin
Inc(FTop);
FItems[FTop] := Value;
end;
function TStack.Pop: Integer;
begin
Result := FItems[FTop];
Dec(FTop);
end;
function TStack.IsEmpty: Boolean;
begin
Result := FTop = 0;
end;
var
S: TStack;
begin
S := TStack.Create;
try
S.Push(10);
S.Push(20);
S.Push(30);
while not S.IsEmpty do
WriteLn(S.Pop);
finally
S.Free;
end;
end.
The class variable is a pointer, so it must be created before use and freed
afterwards. Fields are private and named with an F prefix, and the
methods use Result. The array still has the old fixed bounds
1..5; a proper conversion would add a bounds check or a dynamic
array, because the original silently overflowed when more than five values were
pushed.
$ fpc -gl objstack.pas $ ./objstack 30 20 10
Migration checklist
Convert in small steps and compile after each one. Start with the type sizes:
code that assumed a 16-bit Integer may need SmallInt
or LongInt to keep its behaviour. Replace absolute
overlays with records and explicit conversions where possible, and keep them
only for hardware access. Old objects become classes, and their pointers become
class references with Free.
The crt unit and direct screen writes belong to the user
interface, not to the data model; separate them before porting. DOS paths with
backslashes, drive letters, and 8.3 names must become platform-neutral paths.
Finally, compile with range and overflow checking (-Cr -Co) to
find the assumptions that the old compiler did not check. The
dialects article compares the four modes in
detail.
Source
Free Pascal: Turbo Pascal mode
In this article we compiled a program in Turbo Pascal mode, used the
crt unit, defined an old-style object, overlaid two variables with
absolute, and converted the stack to an ObjFPC class. The
dialects article shows the same source through
all four modes, and the
compiling and debugging article
explains the checks that make a port safer.
Author
List all Pascal tutorials.