Author Topic: How big can a '.com' file be?  (Read 14305 times)

Offline bluasm

  • Jr. Member
  • *
  • Posts: 6
How big can a '.com' file be?
« on: April 30, 2012, 01:01:49 AM »
This will sound dumb, But I always thought a '.com' executable could be no larger than 64 kb; and that such a file could only work in lower or
real mode dos memory. An 'exe' is completely relocatable. So, if I link a 'com' file(from nasm) into a C wrapper, will the resulting exe work
in the normal fashion? (I *told* you it was a dumb question.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How big can a '.com' file be?
« Reply #1 on: April 30, 2012, 04:23:58 AM »
Not so much "dumb", but from someone who hasn't learned about the different linkable object and executable formats. A .com file, as you observe, is limited to 64k. Nasm will create a file bigger than 64k and name it .com, but dos won't load (and execute) it. A .com file, on disk, consists entirely of the code you wrote - there's no "executable header". It is this lack of an executable header that allows dos to determine the executable type - dos doesn't care if it's named .com or .exe! If it sees the letters "MZ" (the guy's initials - Mark something Polish - who wrote the dos code), it expects the rest of the executable header... which includes the information necessary to relocate it.

It is possible to use Nasm in "-f bin" output mode to create our own "MZ header", but it is more usual to use "-f obj" to create a linkable object file which, with the assistance of a linker, creates the executable header with the relocation information, possible linking muliple modules into a single executable.

If you attempt to link a C wrapper with a .com file, the linker will refuse... be unable, because the necessary information isn't there. I can't predict exactly what the linker will say about it, but it won't work!

I'm sure it's possible to "trick" C into including your plain binary code, and maybe even making it work right if you know what you're doing, but it's a lot easier to assemble into "-f obj" (or some other linkable format) and let the linker do its job!

Best,
Frank


Offline bluasm

  • Jr. Member
  • *
  • Posts: 6
Re: How big can a '.com' file be?
« Reply #2 on: April 30, 2012, 03:32:13 PM »
Frank,
    Tnx for the quick answer; looks like I have some playing around to do. I have used a hex/disk editor to peruse the header on the exe with it's
'MZ'; so I think you're saying that '-f obj' results in the usual 'MZ' header, or if not, dos or freedos will run it as an 'exe' on the whole?.
BTW, I once tried to link a C++ with a nasm,nogo; then tried the intermediate c-wrapper between nasm and C++, still nogo. Found out this is
due to 'name mangling', which different compilers scheme differently, making it impossible to accomodate. Worse, even if the C++ prog does
not make use of function overloading, the mangling is always on. Thats why I abandoned C++; it is more trouble tha it is worth. C is best.
Richard.

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: How big can a '.com' file be?
« Reply #3 on: April 30, 2012, 08:28:47 PM »
Frank,
    Tnx for the quick answer; looks like I have some playing around to do. I have used a hex/disk editor to peruse the header on the exe with it's
'MZ';

I find hex editors to be (mostly) useless. If you want to study what's going on in an executable, I would suggest you get a really good disassembler. I regularly use IDA Pro (there is a free version around somewhere) for things like that, I also kinda liked BORG disassembler for Win32 stuff. Another really good tool is objdump which is specifically designed for studying and disassembling object files.

so I think you're saying that '-f obj' results in the usual 'MZ' header, or if not, dos or freedos will run it as an 'exe' on the whole?.

The '-f obj' option produces an OMF object file which can be linked into an MZ/EXE file. To link it, use ALink or TLink.

BTW, I once tried to link a C++ with a nasm,nogo; then tried the intermediate c-wrapper between nasm and C++, still nogo. Found out this is
due to 'name mangling', which different compilers scheme differently, making it impossible to accomodate. Worse, even if the C++ prog does
not make use of function overloading, the mangling is always on. Thats why I abandoned C++; it is more trouble tha it is worth. C is best.
Richard.

To disable name mangling in a C++ function, use the extern "C" option. See the attached file for an example.

Regards,
Bryant Keller

NOTE: The example's makefile was tested under GNU/Linux, if you are working on windows it might need to be changed.. i have no clue (I'm not much of a windows user). :P

About Bryant Keller
bkeller@about.me

Offline bluasm

  • Jr. Member
  • *
  • Posts: 6
Re: How big can a '.com' file be?
« Reply #4 on: May 01, 2012, 10:40:34 AM »
Tnx all; BTW, the "preferred" object file format using djgpp is, evidently: -f coff; in Dr. Carters book, "pcasm", and in the included folder with djgpp
examples, took a c file: main5.c;it's associated sub5.asm, compiled the c, assembled the asm wth '-f coff'. Then liknked these and an appropriate
io, "asm_io.o", and it works perfectly. The book mentions that with the borland compiler, you would use the '-f obj' switch.Richard.