Starting with the fact they have a dedicated set of "subsystem" values: https://docs.microsoft.com/en-us/windows/win32/debug/pe-form... - the win32 loader won't load anything that isn't the CUI or the GUI Windows one, although the kernel also knows about the native one. You'll just get the "this is not a Windows application" messagebox.
With NASM, you can also override that linker stub. For example save this to a file:
org 100h
start:
mov dx, msg
mov ah, 9
int 21h
mov ah, 4Ch
int 21h
msg db 'Are you still running DOS in 2022? Wow! 640K enough for you!',13,10,'$'
nasm -fbin -o stub.com stub.asmthen if you are using Microsoft's tools you can use LINK.EXE <usual args> /STUB:stub.com and voila, if anyone _happens_ to (try to) your code under DOS, they'll get an amusing message.
If you want it to take up minimum space, use this one:
org 100h
start:
mov ah,4Ch
int 21h
which will just exit that program if run under dos.Apparently the header is hardcoded in mingw, but you could easily binary patch it in the resulting executable.
I think I read somewhere you can also remove the stub completely, but I've never tried it.
Edit: for my last comment, yes, you can definitely drop it: https://stackoverflow.com/a/9659538.