2

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?

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
Born34
  • 49
  • 3
  • 1
    You can try alternative compilers like nowaday's [FPC](https://wiki.freepascal.org/DOS). Most likely Turbo Pascal is adding in-language routines, unbound to ever be used/called or not. And 10052 is below 10 [Ki](https://en.wikipedia.org/wiki/Binary_prefix#Consumer_confusion), because 10052 / 1024 = 9.82 Ki. – AmigoJack Dec 25 '22 at 11:37
  • 1
    Just tried the same with TP7. The size of the program was 1.59 kB. – LU RD Dec 26 '22 at 09:48
  • Yes thats true, but it is and EXE that I need to covert. Not an original. Or you create an COM in TP7? How it works? – Born34 Dec 26 '22 at 13:31
  • Try renaming the exe file to com. Depending on DOS version that might work. – LU RD Dec 26 '22 at 14:52
  • Look at the compiler source code. You will find many asm files. Some of the RTL functions could be omitted for a smaller program footprint since it is possible to recompile the RTL. – LU RD Dec 27 '22 at 13:34
  • 1
    The question is: What exactly are you trying to achieve? And then... why does it have to be a COM file? – Thomas Kjørnes Mar 21 '23 at 22:45
  • Earlier versions of TP weren't linking compiled programs with libraries(there even were no `unit`s before TP4 in the language). They were just putting all you may need right in the executable. So, that's why it's so large. TP4+ executable size would be 1/10th of TP1-3 executable. – user28434'mstep Apr 24 '23 at 01:07

2 Answers2

3

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.

AntoineL
  • 888
  • 4
  • 25
1

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.

Ozz Nixon
  • 159
  • 1
  • 8