Difference between .com, .exe, and .bat?
Asked Answered
P

10

27

What is the difference between the a.bat, a.com and a.exe extensions?

Paryavi answered 22/1, 2010 at 7:13 Comment(0)
L
61

Originally, a .COM file was a literal blob of 8086 code (that is, 16-bit x86). It is meant to be loaded at a fixed address, and the loader would jump straight to the first byte of its address. It's also limited in size.

An .EXE file has more header information. So it has required structures for things like dynamic linking, where code from a DLL can be patched into the .EXE's memory space at load time.. It originally comes from DOS, but it's today used in Windows.

However DOS and Windows eventually went to a model where the file extension in a .COM and .EXE didn't mean anything. The program loader first checks the first two bytes of the file. If it happens to be the string MZ (legend has it this stands for the initials of an early Microsoft employee), it will treat it as an EXE, otherwise it will load it as if it were a COM file. Since MZ doesn't map to a sensible x86 instruction to start a program, they can get away with this. Net effect: In some versions of DOS/Windows, an .EXE can be named with .COM and vice versa. For example, in many versions of DOS/Windows, the famous COMMAND.COM was actually an EXE.

I am not sure how much the previous paragraph applies to NT based versions of Windows. I'd imagine by now they've abandoned the .COM stuff altogether.

Lastly, a .BAT file is a list of commands to be executed as if you typed them at your command prompt. However these days most people name them as .CMD.

Lorna answered 22/1, 2010 at 7:26 Comment(12)
MZ stands for MORIN Zobowski - He designed and implemented the first versions of EXE headers. :)Bywoods
You can rename any .exe file to .com and it will run fine - it makes no difference.Zincate
Just because it runs it doesn't mean it works! EXE and COM are like 2 completely different model of cars from the same manufacture.Bywoods
@George Edison - What I meant was, if you assemble "mov ax, 4c00h ; int 21h", then name that with .COM, will it do anything if you run it? Does this still work on Win7?Lorna
I use Vista :) What I meant was that Windows will load the executable based on headers/content, not extension. It is ignored - at least when it comes to exe/com. As for real .com apps, NT based OS's will not run 16 bit executable files.Zincate
@George Edison I'm pretty sure 32-bit NT will still run Win16 EXEs.Lorna
I believe that a "call 0" would terminate the program, as it's a holdover from CP/M days. The zero refers to the first 16 bit word of the PSP, which is an int 20h (no exit code used, but causes a program to terminate)Associative
16 bit COM files can run in NTVDM on all 32 bit Microsoft OS. 16 bit was deprecated in 64 bit OS.Collazo
The question is pretty vague, but a more complete answer could include which of the 3 is executed if they are in the same directory (presumably .com, depending on environment variable PATHEXT).Midlands
In Visual Studio, "devenv.exe" is the GUI version of Visual Studio and "devenv.com" is the command line build tool version. It's really an EXE, just renamed as .com. But because ".com" has precedence in the PATH search, typing "devenv" in a console window launches the command line build tool. Neat trick should ever want to have a Windows GUI EXE also have a command line version of itself.Embowel
@KJ MZ stands for "Mark Zbikowski". The guy who designed the format for the EXE executable file and its header for DOS. en.wikipedia.org/wiki/Mark_ZbikowskiBywoods
Note that a .COM file was a specific hold over from the 8080 processor and CP/M operating system ancestors of MS/DOS. It is a simple blob of code that is loaded at 0x0200 (the start of the CP/M program area), and also does not use any of the segment registers. That means that the entirety of the program and data fits in 64 KB of memory (the limit of 16 bit addressing in the 8080) Part of idea is that existing 8080 source could be re-compiled to .COM files and run on an 8086 system. (All the 8080 opcodes exist in the 8086, just at different numbers)Moulding
E
2

.bat is a batch file. It is interpreted.

.exe is a regular executable program file.

A .com file, at least for MS-DOS, has many meta-data missing and is loaded into a specific offset in the main memory. It is smaller than .exe

Estriol answered 22/1, 2010 at 7:18 Comment(0)
T
1

I assume you mean for Windows?

"a.bat" is supposed to be a batch file, the Windows/DOS equivalent of a script file.

"a.com" and "a.exe" are supposed to be equivalent these days. However, back in the Windows 3.x days, a "com" file was a DOS executable, where an "exe" file was a portable executable, or a Windows-based executable. This is a gotcha these days, as files in the format "www.example.com" can exist on your hard drive, and many people mistake such a file for a web link. Even worse, Windows typically tries executing "com" files before "exe" files.

Tega answered 22/1, 2010 at 7:18 Comment(3)
Order of Precedence in Locating Executable Files (support.microsoft.com/kb/35284) is also useful sometimes when you're in a bind (rarely though).Weeden
.exe files existed before Windows came around.Shoreward
Actually, Portable Executables existed before Windows. See Wikipedia.Zincate
C
1

Actually, .com and .exe are both binary executable files, whereas .bat is basically a batch file. Now suppose you have got many files with the same name, but different extensions.

For instance, a.com, a.exe and if you are running through the command prompt file a. It will first execute a.com (only if it exists), else it will run a.exe. Or say a.exe is also not there then it will look for a.bat execution.

Curassow answered 26/1, 2014 at 8:21 Comment(1)
A
0

A bat(ch) file is a script that is executed by the command interpretor.

A exe file is compiled binary code to be executed directly on the cpu.

A com file is a relic from the past to create a small exe.

Ailey answered 22/1, 2010 at 7:16 Comment(0)
A
0

.BAT - Batch File: list of commands (basically a text file with command-line commands)

.COM - DOS Executable loaded into a fixed block of memory (stems back from before multi-tasking)

.EXE - Executable file - standard application on the Windows platform

Aquamarine answered 22/1, 2010 at 7:21 Comment(0)
D
0

While EXE and BAT files often serve a similar purpose, they use completely different file formats. Both file types can be used for creating executable content in Windows, but BAT files are limited in the commands they can perform. Since BAT files contain human-readable text, they can be easily edited and therefore are often used for custom scripting tasks. EXE files, on the other hand, contain complex binary data that is built using a compiler. Since EXE files support more complex commands than BAT files, most Windows applications are saved in the EXE format.

I was also looking for the same query and found something that have pasted here.

Please refer the below link, you will find it useful, it perfectly answers your question:
Difference between .BAT and .EXE

Designedly answered 2/7, 2013 at 8:34 Comment(0)
M
0
  • A .BAT (short for "batch") file is a plain text file that contains a series of Windows commands.
  • An .EXE (short for "executable") file is a binary file that contains much more complex executable binary code.
  • A .COM file was a DOS executable and nowadays its same as .EXE.
Melise answered 16/11, 2014 at 10:47 Comment(0)
P
0

I compiled a simple .com example file using nasm -f bin -o helloworld.com helloworld.asm:

org 100h

section .text

mov ah, 0
mov dx, msg
int 21h

mov ah, 4Ch
int 21h

section .data
msg db "Hello, World!$"

Something nice and simple. I got the .com file, but when I try to run it in Windows 11 I get:

PS C:\Workspace\asm> .\helloworld.com ResourceUnavailable: Program 'helloworld.com' failed to run: An error occurred trying to start process 'C:\Workspace\asm\helloworld.com' with working directory 'C:\Workspace\asm'. The specified executable is not a valid application for this OS platform.At line:1 char:1

  • .\helloworld.com

It's a small 26 byte file - which is one reason it was so popular:

b400 ba0c 01cd 21b4 4ccd 2100 4865 6c6c
6f2c 2057 6f72 6c64 2124 

It just appears Windows wants nothing to do with it anymore.

Pulcheria answered 10/11, 2023 at 19:45 Comment(1)
It appears that you are still having a problem, and are reporting it, yet you posted it as an answer, instead of as a comment or, perhaps better in this case, a new question. Please reserve the answers feature for new solutions, though.Hebbe
C
-2

.bat file effects directly on the performance of CPU. While, the .exe file will be compiled by interpreter and then executed on CPU.

Culmination answered 27/3, 2013 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.