0

Here is a short C file cu.c:

int main(int argc, char **argv) {
  (void)argc; (void)argv;
  return 0;
}

When I compile it with OpenWatcom to a DOS .com file, the result is almost 3 KiB:

$ owcc -bcom -mcmodel=t -fno-stack-check -Os -s -march=i86 -W -Wall -Wextra -o cu.com cu.c
$ ls -l cu.com
-rwxr-x--- 1 pts pts 2938 Jun 20 19:26 cu.com

The smallest possible DOS .com file which does the same (just exits) is 1 byte long: it contains a single ret instruction (byte 0xc3).

How can I compile my cu.c file (with possibly some modifications, I don't care about argc or argv) to a smaller DOS .com file, without the OpenWatcom C library (libc), preferably less than 100 bytes? I will call DOS API functions (int 0x21) directly from my program, thus I won't need any of the OpenWatcom C library functions.

Please note that COM executables with Open Watcom doesn't answer my question, because the solutions presented there all include the OpenWatcom C library.

pts
  • 80,836
  • 20
  • 110
  • 183
  • Used to use a DOS utility called `EXE2BIN` which would convert a "tiny" or "small" (??) model .exe to .com. I'm not sure that you can exit with `ret` though, it might need to call the terminate function. – Weather Vane Jun 20 '20 at 18:06
  • exe2bin is cool, but it's unlikely to make a working .com file which is smaller than the .exe. The reason why I want a .com file is to save ~40 bytes of header. – pts Jun 20 '20 at 18:18

1 Answers1

0

I was able to solve it by using format dos com instead of system com in my custom .lnk file. By doing so, the libfile cstart_t.obj in system begin com in link.lnk wasn't used, thus the startup code in the OpenWatcom C library wasn't referenced.

The resulting .com file is only 3 bytes (xor ax, eax; ret), which I'm happy with. To make it actually run, I have to prepare and add my .obj file though which contains ..start: and sets up segments.

I'm still looking for a solution which doesn't need a custom .lnk file.

pts
  • 80,836
  • 20
  • 110
  • 183