Compiled COM files with empty project is over 10 KiB large in Turbo Pascal

220 Views Asked by At

I have a problem with the binary's size of old Pascal versions.

We need very small simple programs. We would like to use Turbo Pascal 2 in MS-DOS (higher is the same problem) to compile COM files. But the size is always 10 KiB and larger, even for an empty project like:

begin
end.

Compiled file sizes 10052 bytes. I do not understand why. I tested compiler commands, changed stack/heaps with no results.

Compilation output:

Compiling --> c:emtpy.com
  3 lines

code: 0002 paragraphs (32 bytes), 0D7B paragraphs free
data: 0000 paragraphs (0 bytes), 0FE7 paragraphs free
stack/heap: 0400 paragraphs (16384 bytes) (minimum)
            4000 paragraphs (262144 bytes) (maximum)

Is it possible to get a smaller COM file, and is it possible to convert the Pascal code automatically into ASM code?

2

There are 2 best solutions below

0
On

Any version of Turbo Pascal up to 3.02 will result into an executable file which includes the whole Run-Time Library. As you discovered, the size of it for TP2 on your target operating system is about 10,050 bytes.

We need very small simple programs.

... then Turbo Pascal 2 is not a good option to start up. Better try with any version from 4 up, if you want to stick with Pascal and are targeting MS-DOS. Or switch to C or assembly language, which will be able to produce smaller executables, at the cost of being more difficult to develop.

[...] is it possible to convert the Pascal code automatically into ASM code.

It can be done using Turbo Pascal but it is not practical (basically you need a disassembler; IDA is such a tool, used nowadays; the version you need is not free.) Also you won't gain much by smashing some bytes from an already compiled application: you will end much better starting it straight in assembly language.

Anyway, the best course to achieve it is to drop Turbo Pascal and go to Free Pascal, which compiler produces .s files, which are written in assembly language (although maybe not in the the same syntax as you are used.) There is (was?) a sub-project to target the 16-bit i8086 processor, which seems reasonably up-to-date (I never tried it.)

Update

You mentioned in a comment you really need the .COM format (which Turbo Pascal 4-7 does not support directly). The problem then is about the memory model. .COM programs are natively using the so-called tiny model (16-bit code and data segments overlapping at the same location), but it can be somewhat evaded for application (not TSR) which can grab all the available memory; TP 1-3 for MS-DOS uses a variant of the compact model (data pointers are 32-bit "far" but code pointers are 16-bit "near", which caps at 64 Ki bytes of code); TP 4-7 are instead using the large model where each unit have a separate code segment. It could be possible to rewrite the Run-Time Library to use only one code segment, then relink the TP-produced executables to convert the FAR CALLs into NEAR CALLs (that one is easy since all the information is in the relocation table of the .EXE). However, you will be home sooner using directly Free Pascal, which supports natively the tiny memory model and can produce .COM executables; while still being highly compatible with Turbo Pascal.

0
On

Depending upon your compiler version, and switches. 10kb is not possible. I write my own Turbo Pascal compiler clone as I still do a lot of DOS legacy projects for companies.

Program Empty;

Begin
End.

My source is basically the same, except for my compiler line 1 is required so I know the output type.

TPC v7.0.1b: {mine}
03/19/2023  10:33 PM             1,504 EMPTY.EXE
03/19/2023  10:33 PM                29 empty.pas
4 lines, 0.00 seconds, 1360 bytes code, 668 bytes data.

TPC v7.0: {Borland}
03/19/2023  10:36 PM             1,632 EMPTY.EXE
03/19/2023  10:33 PM                29 empty.pas
4 lines, 1472 bytes code, 668 bytes data.

BPC v7.0 {Borland}
03/19/2023  10:37 PM             1,632 EMPTY.EXE
03/19/2023  10:33 PM                29 empty.pas
4 lines, 1472 bytes code, 668 bytes data.

The only difference between my compiler and Borland's is I do better optimization (techniques learned over 30 years since TP existed). As far as I remember, you cannot produce a .COM with Turbo Pascal since v3.0 existed.