DOS COM files

DOS specific questions.
bojan.dosen
Posts: 166
Joined: May 14, 2007 12:20
Location: Zagreb, Croatia

DOS COM files

Postby bojan.dosen » Apr 18, 2013 18:40

Is it possible to create com files with FreeBASIC and how? Without exe header and other linked files, just binary code.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: DOS COM files

Postby MichaelW » Apr 19, 2013 8:58

I think the short answer is no. The as version included with FreeBASIC 0.24.0 (2.21.53) supports .code16, and in my tests, assembling an Intel-syntax assembly source module, it did cause as to generate 16-bit code.

Code: Select all

.intel_syntax noprefix
.globl start
.code16
  start:
    xor ax, ax
    xor eax, eax
    mov ax, OFFSET var0
    mov eax, OFFSET var0
    call var0
  var0:
    .int 0
.code32
    call var1
  var1:
    .int 0

Note in the listing below that a 66h operand-size prefix is used to override the default operand size, and its placement indicates that the default operand size is 16 bits. And note that the 16-bit call is encoded with a 16-bit displacement (0000h), and the 32-bit call with a 32-bit displacement (00000000h).

Code: Select all

GAS LISTING 16bit.asm          page 1


   1                 .intel_syntax noprefix
   2                 .globl start
   3                 .code16
   4                   start:
   5 0000 31C0            xor ax, ax
   6 0002 6631C0          xor eax, eax
   7 0005 B81100          mov ax, OFFSET var0
   8 0008 66B81100        mov eax, OFFSET var0
   8      0000
   9 000e E80000          call var0
  10                   var0:
  11 0011 00000000        .int 0
  12                 .code32
  13 0015 E8000000        call var1
  13      00
  14                   var1:
  15 001a 00000000        .int 0
 GAS LISTING 16bit.asm          page 2


DEFINED SYMBOLS
                            *ABS*:00000000 fake
           16bit.asm:4      .text:00000000 start
           16bit.asm:10     .text:00000011 var0
           16bit.asm:14     .text:0000001a var1

NO UNDEFINED SYMBOLS

The first problem with using FreeBASIC to generate a 16-bit com file is that it generates 32-bit code that uses 32-bit addresses, and these addresses are not compatible with 16-bit code that is restricted to 16-bit addresses, and this is not a problem that .code16 can fix. The second problem is that, as far as I have been able to determine, ld does not support linking 16-bit executables. So while you may be able to use as to assemble a 16-bit com file, you would need some tool other than ld to link it.
marcov
Posts: 2752
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: DOS COM files

Postby marcov » Apr 19, 2013 9:22

(afaik most people experimenting with such things use the openwatcom linker)
bojan.dosen
Posts: 166
Joined: May 14, 2007 12:20
Location: Zagreb, Croatia

Re: DOS COM files

Postby bojan.dosen » Apr 19, 2013 21:32

Huh... okay. Thank you!
I needed to know that, so I will use assembly for that stuff then. I'm writing some kind of boot-loader which runs code from binary file (.bin, .com... whatever) in root folder and i thought that fb can help me to write that kind of code (for binary file ofc).
DOS386
Posts: 798
Joined: Jul 02, 2005 20:55

Re: DOS COM files

Postby DOS386 » Apr 20, 2013 10:36

> it did cause as to generate 16-bit code

Code: Select all

   mov edx, [eax+8*ecx]


YES this does compile in FASM with use16 ... but it will not work on 8086, and still you won't get more than 64 KiB ... and FreeBASIC will not deal with segments.

bojan.dosen wrote:Is it possible to create com files with FreeBASIC and how?


You can't because FreeBASIC is a 32-bit compiler. It creates 32-bit extended DOS executables. This is also the reason why "Hello World" size is cca 100 KiB. Theoretically, you could use the -gen gcc feature and feed the output into WATCOM and brew a COM, or hack the ASM and link stage as MichaelW suggested ... but in both cases you would have to replace the libraries too ... huge work. So the solution is to use some other compiler (try FASM.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: DOS COM files

Postby MichaelW » Apr 20, 2013 13:01

It looks like someone did something similar with FPC:

http://wiki.osdev.org/Pascal_Bare_Bones
marcov
Posts: 2752
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: DOS COM files

Postby marcov » Apr 20, 2013 13:36

MichaelW wrote:It looks like someone did something similar with FPC:

http://wiki.osdev.org/Pascal_Bare_Bones


That's hopefully going to change in the future:

http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/i8086/
rugxulo
Posts: 216
Joined: Jun 30, 2006 5:31
Location: Usono (aka, USA)
Contact:

Re: DOS COM files

Postby rugxulo » Jun 24, 2013 19:50

A .COM isn't the same as a flat binary used for a boot sector because it loads at CS:100h (but a boot sector may not). Though
it's still flat format "raw" because there are no relocations. But a .COM is limited to 64 kb in size on disk (though it can access way more than 64 kb of RAM). A normal DOS MZ .EXE can be up to 640 kb in size. DJGPP-based stuff via .EXE stub usually loads its COFF executable image manually, which is why their .EXEs can be huge (e.g. 10 MB).

FreeBASIC for DOS uses COFF, so that's not OMF compatible. COFF isn't 16-bit friendly (but there are unofficial 8-bit and 16-bit extensions to ELF, right?). Even converters like Agner's ObjConv work only on 32-bit OMF. For 16-bit DOS, if you
want separately linkable modules, e.g. OpenWatcom, you're more or less stuck to OMF (or maybe a.out variant used by BCC/Dev86).

If you can find a DOS BASIC that outputs small model, you may be able to tweak that. IIRC, OpenWatcom's tiny and small models only differ in the tiny runtime (cstart_t.obj), but the libraries are the exact same (clibs.lib).

Return to “DOS”

Who is online

Users browsing this forum: No registered users and 2 guests