Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> It can be run as a COM file or put into a boot sector of a floppy disk to be run.

Isn't "COM file" something specific to DOS? How does it differ from a machine binary file, i.e. what GCC (or a linker) would spit out of the ASM code? (The makefile uses nasm, thought)



From https://en.m.wikipedia.org/wiki/COM_file

> Since it lacks relocation information, it is loaded by the operating system at a pre-set address, at offset 0100h immediately following the PSP, where it is executed (hence the limitation of the executable's size): the entry point is fixed at 0100h.


COM is a machine code file to be loaded at 0100 then executed. There's no instruction difference, but you have to remove the headers. Even a .a produced by GCC has to be de-ELFed with -nostdlib -ffreestanding.


-ffreestanding will still produce an elf binary. You have to either use "objcopy -O binary" or set the output format in the linker script (or on the linker command lind) to produce a flat binary without headers.


We used to refer to .com files as memory images, because they look the same on disk as they do in memory.

EXE, OVL and the like had relocatable segments, whereas the memory images assumed all segments were within the code segment. cs=ds=es and the stack you just crammed somewhere in that space, usually at the end of the literal code, but still very much inside the space of the one segment you could access.


It's a plain 16-bit executable without headers or linking information. Specific to DOS.


Which essentially is equal to a raw machine binary, except that the MBR's boot loader is mapped to 0x7c00, while DOS's COM loader maps the start at 0x1000. Of course the pure MBR version has no access to DOS interrupt handlers for IO access or similar. (Interrupt 0x21)


Interesting that, when you compare the MBR and COM versions side-by-side in a hex editor, the only changes (apart from the required few bytes at the end of the MBR version) are in what I assume are memory addresses: a 7D byte in the MBR version ends up being 02 in the COM version. One higher than 7C and 01 in the respective starting addresses.


You can also easily see this in the (really nice) code, where the only difference is the starting address set at https://github.com/nanochess/Pillman/blob/master/pillman.asm... and the MBR magic bytes in the very bottom.

If you now wonder what the difference between a .com and a .exe file is:

A .com file has a single page which is loaded in that specific location, with all addresses in the code assuming absolute addresses. The .exe (MZ file) allows DOS to put the program anywhere into memory and the rewrite address references (or rather segments) according the relocation table from the program header. With MZ files DOS also sets up space for the stack and sets registers accordingly.


It's also the reason why we have to careful with files named (for example) "google.com".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: