Why .com file also executes .exe file on emu 8086?
Posted by userlivedhere@reddit | learnprogramming | View on Reddit | 5 comments
An emulator can execute a .com file because it includes logic that handles the legacy MS-DOS executable format, often by examining the file's structure rather than relying solely on the file extension. = says google ai I am not able to comprehend it what does this mean
white_nerdy@reddit
In DOS [1], if you have a file whose name ends with .COM, you can type the name of the file at the command line. DOS will put the contents of that file in memory and run it.
If you have a file whose name ends with .EXE, DOS will try to run it, but differently: It will look inside the file for a header. The header explains some things about the file, for example how much memory the program needs, or whether it needs to be relocated [3] [4]. The header famously starts with the letters MZ, the initials of Microsoft employee Mark Zbikowski.
Anyway, if you have a file whose name ends with .COM, but the file's contents begin with the characters MZ, some (all?) versions of DOS won't put the contents in memory and run it like they would for any other .COM file; instead they'll parse the header and load it the same way it would load a file whose name ends with .EXE.
[1] Microsoft's MS-DOS was the original PC operating system, but there are a bunch of other PC operating systems also called DOS, that are mostly compatible with MS-DOS. There's also PC DOS, DR DOS and FreeDOS.
[2] The .COM stands for COMmand; it's unrelated to the .com domain name.
[3] The header is documented here and here.
[4] Suppose you write a game, and you decide to put the player's hitpoints at address 376 and their magic points at address 378. If the OS puts your program somewhere else -- for example, address 5000 -- the hitpoints are now at address 5376 and the magic points are now at address 5378. Relocation is technology that solves that problem by telling the OS all the places it needs to add 5000 to, to get the program to run.
[5] This is not a typo; they really are in reverse order. The x86 CPU is little endian. This means the least-significant byte is stored first, so the number 378 is stored as the bytes 78 03.
[6] Actually in DOS, relocation happens at the segment level. "Segments" are a processor "feature" that is huge can of worms for DOS programming. Segments are confusing and a PITA to work with even if you understand them; modern OS's don't use them, instead adopting a "flat" (non-segmented) memory model and managing memory with pages rather than segments.
flatfinger@reddit
Some utilities which were straight-loaded .COM files in DOS 1.0 became relocation-patched executables in DOS 2.0. Renaming the files to EXE would have broken programs that passed names containing ".COM" to the MS-DOS "execute program" function as a means of running those utilities, so DOS 2.0 allowed the .COM extension to be used even for reloation-patched executables.
kitsnet@reddit
As far as I recall from the actual use of MS-DOS, .exe was "the legacy MS-DOS executable format". .com was just raw executable code.
Chrykal@reddit
It's saying that the extension isn't important, your emulator is looking at the file structure to determine what to do with the file.
Disclaimer: I've never used emu 8086, I'm just explaining what the AI said, don't trust AI
HashDefTrueFalse@reddit
I think it's just trying to say that file extensions do not determine the contents. If the contents of the file is as expected, the emulator can run it. Extensions are just part of the file name. They only have the significance a program gives them. Some programs may refuse to look at file contents if the extension is unexpected. Some will look for magic bytes at the start. Some will try their luck with a parse regardless etc...