Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does DOS load a program into memory?

Tags:

executable

dos

What steps does MS-DOS take to load a COM or EXE file into memory? Are there still references online as to how this happens? The best I can think of is possibly referring to dosbox source.

like image 612
Nick Sonneveld Avatar asked Sep 15 '10 07:09

Nick Sonneveld


1 Answers

When command.com is asked to execute a .com or .exe file, it will call the interrupt service 21h/AH=4B, the EXEC service. It is up to the calling program to:

  • build a DOS EXEC parameter block (see http://www.delorie.com/djgpp/doc/rbinter/it/90/15.html ) (includes information on environment variables, command lines arguments, FCBs and register values on return)
  • free up all memory the calling program isn't using
  • setup calling argument registers
    • ah = 4Bh ('EXEC' service type)
    • al = 00h ('load and execute' function)
    • ds:dx -> program name
    • es:bx -> ptr to exec parameter block
  • call interrupt 21h
  • on return reset stack pointer and test for errors.

When interrupt 21h is called (here's where it gets hazy for me):

  • a page aligned block of memory is allocated
  • the file extension is ignored, instead DOS will check the first two bytes of the file for signature "MZ" or "ZM" if an EXE, and no signature for COM.

for exe:

  • exe header is read for initial register values
  • copy code section from exe into memory
  • relocation table (see http://en.wikipedia.org/wiki/Relocation_table) is read and far pointers are adjusted in memory
  • setup register values
    • AL,AH drive letter status
    • DS,ES -> PSP segment (see http://en.wikipedia.org/wiki/Program_Segment_Prefix )
    • SS:SP -> stack pointer (defined in exe header)
  • jump to CS:IP -> entry point (defined in exe header, relative to start of program)

for com:

  • copy entire .com file into memory
  • setup register values
    • AL,AH drive letter status
    • CS,DS,ES,SS -> PSP segment
    • SP = offset of last word available in first 64k segment
  • jump to IP=100h

Program should now be executing.


Notes:

In Microsoft's KB document "Order of Precedence in Locating Executable Files", it mentions the use of "MS-DOS EXEC function (interrupt 21h service 4Bh)" for executing .com and .exe files http://support.microsoft.com/kb/35284

So we can look at Ralph Brown's Interrupt List on Int 21/AH=4Bh

  • http://www.cs.cmu.edu/~ralf/files.html
  • Int 21/AH=4Bh http://www.delorie.com/djgpp/doc/rbinter/id/51/29.html
  • Int 21/AH=4Bh http://www.ctyme.com/intr/rb-2939.htm

and an example of use:

  • Art of Assembly / 19.1.1.1 Load and Execute http://webster.cs.ucr.edu/AoA/DOS/ch19/CH19-1.html#HEADING1-10

and the dos exe header format:

  • http://www.delorie.com/djgpp/doc/rbinter/it/94/15.html
  • http://www.delorie.com/djgpp/doc/exe/

(this is based off some googling, so please feel free to add suggestions)

like image 114
Nick Sonneveld Avatar answered Oct 01 '22 22:10

Nick Sonneveld