home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / docum / hadware.doc / chapter.006 < prev    next >
Encoding:
Text File  |  1992-03-15  |  34.0 KB  |  685 lines

  1.                                     -177-
  2.  
  3.    **  Programmer's  Technical    Reference  for    MSDOS  and  the  IBM  PC **
  4. ─────────────────────────┤ Registered User Version ├───────────────────────────
  5.              not for public distribution
  6.          Copyright (c) 1987, 1988, 1989 Dave Williams
  7.  
  8.  
  9.                  C H A P T E R   S I X
  10.  
  11.                DOS CONTROL BLOCKS AND WORK AREAS
  12.  
  13.  
  14.                 C O N T E N T S
  15.  
  16. DOS Address Space ..................................................... 6**1
  17. Storage Blocks ........................................................ 6**2
  18. Disk Transfer Area (DTA) .............................................. 6**3
  19. Program Segment Prefix ................................................ 6**4
  20. Memory Control Blocks ................................................. 6**5
  21. DOS Program Segment ................................................... 6**6
  22.  
  23.  
  24.  
  25. DOS Address Space ..................................................... 6**1
  26.  
  27.  Contrary to popular belief, DOS is not limited to 640k of work space. This
  28. constraint is enforced by the mapping of ROM and video RAM into the default 1
  29. megabyte CPU address space. Some MSDOS compatible machines, such as the Sanyo
  30. 55x series, can have as much as 768k of contiguous DOS workspace with the
  31. appropriate option boards. Since DOS has no real memory management, it cannot
  32. deal with a fragmented workspace. Fragmented RAM (such as RAM mapped into the
  33. option ROM address space) can be dealt with as a RAMdisk or other storage area
  34. by using a device driver or other software.
  35.  
  36.  The 80386 CPU and appropriate control software can create a DOS workspace of
  37. more than one megabyte. Certain add-on boards can also add more than a
  38. megabyte of workspace, but only for specially written software. Since these
  39. are all proprietary schemes, little information is availible at present.
  40.  
  41.  
  42.  
  43. Storage Blocks ........................................................ 6**2
  44.  
  45.  A storage block is used by DOS to record the amount and location of allocated
  46. memory within the machine's address space.
  47.  
  48.  A storage block, a Program Segment Prefix, and an environment area are built
  49. by DOS for each program currently resident in the address space. The storage
  50. block is used by DOS to record the address range of memory allocated to a
  51. program. It is used by DOS to find the next availible area to load a program
  52. and to determine if there is enough memory to run that porogram. When a
  53. memory area is in use, it is said to be allocated. Then the program ends, or
  54. releases memory, it is said to be deallocated.
  55.  
  56.  A storage block contains a pointer to the Program Segment Prefix associated
  57. with each program. This control block is constructed by IBMDOS for the purpose
  58. of providing standardized areas for DOS/program communication. Within the
  59. PSP are areas which  are used to save interrupt vectors, pass parameters to
  60. the program, record disk directory information, and to buffer disk reads and
  61. writes. This control block is 100h bytes in length and is followed by the
  62. program module loaded by DOS.
  63.  
  64.  The PSP contains a pointer to the environment area for that program. This
  65. area contains a copy of the current DOS SET, PROMPT, COMSPEC, and PATH values
  66. as well as any user-set variables. The program may examine and modify this
  67. information as desired.
  68.  
  69.                                     -178-
  70.  
  71.  Each storage block is 10h bytes long, although only 5 bytes are currently
  72. used by DOS. The first byte contains 4Dh (a capital M) to indicate that it
  73. contains a pointer to the next storage block. A 5Ah (a capital Z) in the
  74. first byte of a storage block indicatres there are no more storage blocks
  75. following this one (it is the end of the chain). The identifier byte is
  76. followed by a 2 byte segment number for the associated PSP for that program.
  77. The next 2 bytes contain the number of segments what are allocated to the
  78. program. If this is not the last storage block, then another storage block
  79. follows the allocated memory area.
  80.  When the storage block contains zero for the number of allocated segments,
  81. then no storage is allocated to this block and the next storage block
  82. immediately follows this one. This can happen when memory is allocated and
  83. then deallocated repeatedly.
  84.  
  85.  IBMDOS constructs a storage block and PSP before loading the command
  86. interpreter (default is COMMAND.COM).
  87.  
  88.  If the copy of COMMAND.COM is a secondary copy, it will lack an environment
  89. address at PSP+2Ch.
  90.  
  91.  
  92.  
  93. Disk Transfer Area (DTA) .............................................. 6**3
  94.  
  95.  DOS uses an area in memory to contain the data for all file reads and writes
  96. that are performed with FCB function calls. This is known as the disk transfer
  97. area. This disk transfer area (DTA) is sometimes called a buffer. It can be
  98. located anywhere in the data area of your application program (the DTA should
  99. not, however, cross a 64k segment boundary) and should be set by your program.
  100.  
  101.  Only one DTA can be in effect at a time, so your program must tell DOS what
  102. memory location to use before using any disk read or write functions. Use
  103. function call 1Ah (Set Disk Transfer Address) to set the disk transfer address.
  104. Use function call 2Fh (Get Disk Transfer Address) to get the disk transfer
  105. address. Once set, DOS continues to use that area for all disk operations until
  106. another function call 1Ah is issued to define a new DTA. When a program is given
  107. control by COMMAND.COM, a default DTA large enough to hold 128 bytes is
  108. established at 80h into the program's Program Segment Prefix.
  109.  
  110.  For file reads and writes that are performed with the extended function calls,
  111. there is no need to set a DTA address. Instead, specify a buffer address when
  112. you issue the read or write call.
  113.  
  114.  DOS functions 11h (search first), 12h (search next), 14h (sequential read),
  115. 15h (sequential write), 21h (random read), 22h (random write), 27h (random
  116. block read), and 28h (random block write) use the DTA for file access.
  117.  
  118.  
  119. --------------------------
  120. TITLE: DTA use
  121.  
  122. Can anyone tell me which DOS functions actually use the DTA?  I have
  123. a vague idea that it's just the FCB I/O functions and FindFirst/
  124. FindNext, but don't remember where I got that.
  125.  
  126. --------------------------
  127. Actually, the handle calls set and reset the DTA to their own private area,
  128. too, so all the calls use the "DTA" mechanism on some level.  As far as
  129. having to define it first...I'd think those ones that say to define it first,
  130. no?
  131.  
  132. --------------------------
  133. Seems to me that you're right - just the FCB calls use it.  Of course this is
  134. ancient history so I don't know how it is that I know that ...
  135.  
  136.                                        -179-
  137.  
  138. --------------------------
  139. you are right. the read/write-commands using the FCB-approach
  140. and the ind first/find next calls, even the new ones use DTA.
  141. Look t the technical reference for DOS (any version) to find
  142. more deails. It is very good documented.
  143.  
  144. --------------------------
  145. That's what it's _supposed_ to be.  Untrue, though, as I've had my DTA
  146. clobbered in a program where I don't do any of those calls.  I think the
  147. "get date" and "set date" (or time) calls clobber the DTA too.
  148.  
  149.  
  150.  
  151. Program Segment Prefix ................................................ 6**4
  152.  
  153.  When DOS loads a program, it first sets aside a section of memory for the
  154. program called the program segment, or code segment. Then it constructs a
  155. control block called the program segment prefix, or PSP, in the first 256
  156. (100h) bytes. Usually, the program is loaded directly after the PSP at 100h.
  157.  
  158.  The PSP contains various information used by DOS to help run the program.
  159. The PSP is always located at offset 0 within the code segment. When a program
  160. recieves control certain registers are set to point to the PSP. For a COM
  161. file, all registers are set to point to the beginning of the PSP and the
  162. program begins at 100h. For the more complex EXE file structures, only DS and
  163. ES registers are set to point to the PSP. The linker determines the settings
  164. for the CS, IP, SS, and SP registers and may set the starting location in CS:IP
  165. to a location other than 100h.
  166.  
  167.  IBMBIO provides an IRET instruction at absolute address 847h for use as a
  168. dummy routine for interrupts that are not used by DOS. This lets the interrupts
  169. do nothing until their vectors are rerouted to their appropriate handlers.
  170.  
  171.  
  172.  The PSP (with offsets in hexadecimal) is formatted as follows:
  173.  (* = undocumented)
  174.  
  175. ┌──────────────────────────────────────────────────────────────────────────────┐
  176. │     P  R  O  G  R  A    M    S  E  G  M  E  N  T      P  R    E  F  I  X     │
  177. ├───────┬──────────┬───────────────────────────────────────────────────────────┤
  178. │ offset│   size   │             C O N T E N T S               │
  179. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  180. │  00h    │ 2 bytes  │ int 20h                               │
  181. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  182. │  02h    │ 2 bytes  │ segment address, end of allocation block               │
  183. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  184. │  04h    │ 1 byte   │ reserved, normally 0                       │
  185. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  186. │  05h    │ 5 bytes  │ FAR call to MSDOS function dispatcher        (int 21h)  │
  187. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  188. │  0Ah    │ 4 bytes  │ previous termination handler interrupt vector  (int 22h)  │
  189. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  190. │  0Eh    │ 4 bytes  │ previous contents of ctrl-C interrupt vector   (int 23h)  │
  191. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  192. │  12h    │ 4 bytes  │ prev. critical error handler interrupt vector  (int 24h)  │
  193. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  194. │  16h    │ 22 bytes │ reserved for DOS                           │
  195. └───────┼──────────┼───────────────────────────────────────────────────────────┤
  196.       * │ 2 bytes  │ (16) parent process' PSP                       │
  197.       * │ 20 bytes │ (18) "handle table" used for redirection of files           │
  198. ┌───────┼──────────┼───────────────────────────────────────────────────────────┤
  199. │  2Ch    │ 2 bytes  │ segment address of the program's environment block        │
  200. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  201. │  2Eh    │ 34 bytes │ reserved, DOS work area                       │
  202. └───────┴──────────┴───────────────────────────────────────────────────────────┘
  203.  
  204.                                     -180-
  205.  
  206.     ┌──────────┬───────────────────────────────────────────────────────────┐
  207.       * │  4 bytes │ (2Eh) stores the calling process's stack pointer when     │
  208.     │       │       switching to DOS's internal stack.               │
  209.       * │       │ (32h) DOS 3.x max open files                   │
  210.       * │  2 bytes │ (3Ah) size of handle table   |these functions are in here │
  211.       * │  4 bytes │ (3Ch) handle table address   |but reported addresses vary │
  212. ┌───────┼──────────┼───────────────────────────────────────────────────────────┤
  213. │  50h    │  3 bytes │ int 21h, RETF instruction                       │
  214. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  215. │  53h    │  2 bytes │ reserved - unused?                        │
  216. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  217. │  55h    │  7 bytes │ reserved, or FCB#1 extension                   │
  218. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  219. │  5Ch    │ 16 bytes │ default unopened File Control Block #1               │
  220. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  221. │  6Ch    │ 16 bytes │ default unopened FCB #2 (overlaid if FCB #1 opened)       │
  222. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  223. │  80h    │  1 byte  │ parameter length (number of chars entered after filename) │
  224. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  225. │  81h    │   ...    │ parameters                            │
  226. ├───────┼──────────┼───────────────────────────────────────────────────────────┤
  227. │  0FFh │ 128 bytes│ command tail and default Disk Transfer Area (DTA)           │
  228. └───────┴──────────┴───────────────────────────────────────────────────────────┘
  229.  
  230.  
  231. 1. The first segment of availible memory is in segment (paragraph) form. For
  232.    example, 1000h would respresent 64k.
  233.  
  234. 2. Offset 2Ch contains the segment address of the environment.
  235.  
  236. 3. Programs must not alter any part of the PSP below offset 5Ch.
  237.  
  238.  
  239. PSP (comments):
  240.  
  241. offset 00h  contains hex bytes "CD 20", the int 20h opcode. A program can end
  242.         by making a jump to this location when the CS points to the PSP.
  243.         For normal cases, int 21h/fn4Ch should be used.
  244.  
  245. offset 02h  contains the segment-paragraph address of the end of memory as
  246.         reported by DOS. (which may not be the same as the real end of RAM).
  247.         Multiply this number by 10h or 16 to get the amount of memory
  248.         availible. ex. 1000h would be 64k.
  249.  
  250. offset 04h  "reserved or used by DOS" according to Microsoft
  251.  
  252. offset 05h  contains a long call to the DOS function dispatcher. Programs may
  253.         jump to this address instead of calling int 21h if they wish.
  254.         Used by BASIC and other CPM object-code translated programs. It is
  255.         slower than standard int 21h.
  256.  
  257. offset 0Ah, 0Eh, 12h
  258.         vectors (IP, CS)
  259.  
  260. offset 16h  PSP:16h is the segment address of the invoking program's PSP, which
  261.     *   will most often be COMMAND.COM but perhaps may be a secondary
  262.         non-permanent COMMAND or a multitasking shell, etc. At any rate,
  263.         the resident shell version of COMMAND.COM has PSP:16h = PSP, which
  264.         indicates "don't look any lower in memory" for the command
  265.         interpreter. To find the beginning of the allocation chain, look
  266.         backwards through the PSP link addresses until the link address is
  267.         equal to the PSP segment address that it resides in. This should
  268.         be COMMAND.COM. To find COMMAND.COM's environment, look at the word
  269.         stored at offset 0BD3h (PC-DOS 3.1 only). This is a segment
  270.         address, so look there at offset 0.
  271.  
  272.                                     -181-
  273.  
  274.        18h  handle alias table (networking). Also you can make PRN go to CON,
  275.     *   CON go to PRN, ERR go to PRN, etc. 0FFh = availible.
  276.  
  277. offset 2Ch  is the segment:offset address of the environment for the program
  278.         using this particular PSP. This pointer does not point to
  279.         COMMAND.COM's environment unless it is a second copy of COMMAND.
  280.  
  281. offset 2Eh  the DWORD at PSP+2Eh is used by DOS to store the calling process's
  282.     *   stack pointer when switching to DOS's own private stack - at the end
  283.         of a DOS function call, SS:SP is restored from this address.
  284.  
  285. offset 32h, 34h
  286.     *   table of number of file handles (up to 64k of handles!)
  287.  
  288. offset 40h  2 byte field points to the segment address of COMMAND.COM's PSP in
  289.     *   "weird" EXE files produced by Digital Research RASMPC/LINKPC.
  290.         EXE files created with these tools can cause all sorts of problems
  291.         with standard MSDOS debugging tools.
  292.  
  293. offset 50h  contains a long call to the DOS int 21 function dispatcher.
  294.  
  295. offset 5Ch, 65h, 6Ch
  296.         contain FCB information for use with FCB function calls. The first
  297.         FCB may overlay the second if it is an extended call; your program
  298.         should revector these areas to a safe place if you intend to use
  299.         them.
  300.  
  301. offset 5Ch  16 bytes first command-line argument (formatted as uppercase 11
  302.         character filename)
  303.  
  304. offset 6Ch  16 bytes second command-line argument (formatted as uppercase 11
  305.         character filename)
  306.  
  307. offset 7Ch-7Fh
  308.        "reserved or used by DOS"
  309. offset 80h  1 byte number of bytes in command line argument
  310.  
  311. offset 80h, 81h
  312.         contain the length and value of parameters passed on the command
  313.         line.
  314.  
  315. offset 81h  97 bytes unformatted command line and/or default DTA
  316.  
  317. offset 0FFh contains the DTA
  318.  
  319.  
  320.  The PSP is created by DOS for all programs and contains most of the information
  321. you need to know about a program running. You can change the environment for
  322. the current process, however, but for the parent process, DOS in this case, you
  323. need to literally backtrack to DOS or COMMAND.COM's PSP. In order to get there
  324. you must look at the current PSP. At offset 16h of the current PSP segment
  325. there is a 2 byte segment address to the parent or previous process PSP. From
  326. there you can manipulate the enviroment by looking at offset 2Ch.
  327.  
  328. Try this under debug and explore the addresses located at these offsets;
  329.  
  330.       offset  length            description
  331.      ------------------------------------------------------------
  332.     16h    2    segment address of parent process PSP
  333.     2Ch    2    segment address of environment block.
  334.  
  335.                                     -182-
  336.  
  337.  
  338. Remember under debug you will have to backtrack two times.
  339.  
  340.     Programs    Parent
  341.       --------------------------
  342.     command.com    none
  343.     debug.com    command.com
  344.     program     debug.com
  345.  
  346.  
  347.  
  348. Memory Control Blocks ................................................. 6**5
  349.  
  350.  DOS keeps track of allocated and availible memory blocks, and provides four
  351. function calls for application programs to communicate their memory needs to
  352. DOS. These calls are:
  353.  
  354.           48h --- allocate memory              (MALLOC)
  355.           49h --- free allocated memory
  356.           4Ah --- modify allocated memory blocks  (SETBLOCK)
  357.           4Bh --- load or execute program          (EXEC)
  358.  
  359. DOS manages memory as follows:
  360.  
  361.  DOS build a control block for each block of memory, whether free or allocated.
  362. For example, if a program issues an "allocate" (48h), DOS locates a block of
  363. free memory that satisfies the request, and then "carves" the requested memory
  364. out of that block. The requesting program is passed the location of the first
  365. byte of the block that was allocated for it - a memory management control block,
  366. describing the allocated block, has been built for the allocated block and a
  367. second memory management control block describes the amount of space left in the
  368. original free block of memory. When you do a SETBLOCK to shrink an allocated
  369. block, DOS builds a memory management control block for the area being freed and
  370. adds it to the chain of control blocks. Thus, any program that changed memory
  371. that is not allocated to it stands a chance of destroying a DOS memory
  372. management control block. This causes unpredictable results that don't show up
  373. until an activity is performed where DOS uses its chain of control blocks. The
  374. normal result is a memory allocation error, which means a system reset will be
  375. required.
  376.  
  377.  When a program (command or application program) is to be loaded, DOS uses the
  378. EXEC function call 4Bh to perform the loading.
  379.  
  380.  This is the same function call that is availible to applications programs for
  381. loading other programs. This function call has two options:
  382.  
  383.       Function 00h, to load and execute a program (this is what the command
  384.             processor uses to load and execute external commands)
  385.  
  386.       Function 03h, to load an overlay (program) without executing it.
  387.  
  388.  Although both functions perform their loading in the same way (relocation is
  389. performed for EXE files) their handling of memory management is different.
  390.  
  391. FUNCTION 0: For function 0 to load and execute a program, EXEC first allocates
  392. the largest availible block of memory (the new program's PSP will be at offset
  393. 0 in that block). Then EXEC loads the program. Thus, in most cases, the new
  394. program owns all the memory from its PSP to the end of memory, including memory
  395. occupied by the transient parent of COMMAND.COM. If the program were to issue
  396. its own EXEC function call to load and execute another program, the request
  397. would fail because no availible memory exists to load the new program into.
  398.  
  399.                                     -183-
  400.  
  401. NOTE: For EXE programs, the amount of memory allocated is the size of the
  402.       program's memory image plus the value in the MAX_ALLOC field of the file's
  403.       header (offset 0Ch, if that much memory is availible. If not, EXEC
  404.       allocates the size of the program's memory image plus the value in the
  405.       MIN_ALLOC field in the header (offset 0Ah). These fields are set by the
  406.       Linker).
  407.  
  408.  A well-behaved program uses the SETBLOCK function call when it receives
  409. control, to shrink its allocated memory block down to the size it really needs.
  410. A COM program should remember to set up its own stack before doing the SETBLOCK,
  411. since it is likely that the default stack supplied by DOS lies in the area of
  412. memory being used. This frees unneeded memory, which can be used for loading
  413. other programs.
  414.  
  415.  If the program requires additional memory during processing, it can obtain
  416. the memory using the allocate function call and later free it using the free
  417. memory function call.
  418.  
  419.  When a program is loaded using EXEC function call 00h exits, its initial
  420. allocation block (the block beginning with its PSP) is automatically freed
  421. before the calling program regains control. It is the responsibility of all
  422. programs to free any memory they allocate before exiting to the calling
  423. program.
  424.  
  425.  FUNCTION 3: For function 3, to load an overlay, no PSP is built and EXEC
  426. assumes the calling program has already allocated memory to load the new program
  427. into - it will NOT allocate memory for it. Thus the calling program should
  428. either allow for the loading of overlays when it determines the amount of memory
  429. to keep when issuing the SETBLOCK call, or should initially free as much memory
  430. as possible. The calling program should then allocate a block (based on the size
  431. of the program to be loaded) to hold the program that will be loaded using the
  432. "load overlay" call. Note that "load overlay" does not check to see if the
  433. calling program actually owns the memory block it has been instructed to load
  434. into - it assumes the calling program has followed the rules. If the calling
  435. program does not own the memory into which the overlay is being loaded, there is
  436. a chance the program being loaded will overlay one of the control blocks that
  437. DOS uses to keep track of memory blocks.
  438.  
  439.  Programs loaded using function 3 should not issue any SETBLOCK calls since
  440. they don't own the memory they are operating in. (This memory is owned by the
  441. calling program)
  442.  
  443.  Because programs loaded using function 3 are given control directly by (and
  444. return contrrol directly to) the calling program, no memory is automatically
  445. freed when the called program exits. It is up to the calling program to
  446. determine the disposition of the memory that had been occupied by the exiting
  447. program. Note that if the exiting program had itself allocated any memory, it
  448. is responsible for freeing that memory before exiting.
  449.  
  450.  Memory control blocks, sometimes called "arena headers" after their UNIX
  451. counterpart, are 16 bytes long. Only the first 5 bytes are used. 16 bytes are
  452. used for the memory control block, which always starts at a paragraph boundary.
  453. When DOS call 48h is made to allocate "x" many paragraphs of memory, the amount
  454. used up is actually one more than the figure in the BX register to provide
  455. space for the associated memory control block. The location of the memory
  456. control block is at the paragraph immediately before the segment value returned
  457. in AX by the DOS int 21h/fn 48h call i.e. ((AX-1):0).
  458.  
  459.                                     -184-
  460.  
  461. ┌──────────────────────────────────────────────────────────────────────────────┐
  462. │        M E M O R Y     C O N T R O L         B L O C K               │
  463. ├───────┬─────────┬────────────────────────────────────────────────────────────┤
  464. │ Offset│   Size  │                Function                   │
  465. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  466. │   0    │ 1 byte  │ ASCII M or Z                           │
  467. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  468. │  1-2    │ 2 bytes │ PSP segment address of program owning this block of memory │
  469. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  470. │  3-4    │ 2 bytes │ Size of next MCB in 16-byte paragraphs               │
  471. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  472. │  5-F    │ 11 bytes│ unused                               │
  473. └───────┴─────────┴────────────────────────────────────────────────────────────┘
  474.  
  475. byte 1      will always have the value of 4Dh or 5Ah. The value 5Ah (Z) indicates
  476.       the block is the last in a chain, all memory above it is unused. 4Dh
  477.       (M) means that the block is intermediate in a chain, the memory above
  478.       it belongs to the next program or to DOS.
  479.  
  480. byte 2,3  hold the PSP segment address of the program that owns the
  481.       corresponding block of memory. A value of 0 means the block is free
  482.       to be claimed, any other value represents a segment address.
  483.  
  484. byte 3, 4 indicate the size in paragraphs of the memory block. If you know the
  485.       address of the first block, you can find the next block by adding the
  486.       length of the memory block plus 1 to the segment address of the
  487.       control block. Finding the first block can be difficult, as this
  488.       varies according to the DOS version and the configuration.
  489.  
  490.  The remaining 11 bytes are not currently used by DOS, and may contain "trash"
  491. characters left in memory from previous applications.
  492.  
  493.  If DOS determines that the allocation chain of memory control blocks has been
  494. corrupted, it will halt the system and display the message "Memory Allocation
  495. Error", and the system will halt, requiring a reboot.
  496.  
  497.  Each memory block consists of a signature byte (4Dh or 5Ah) then a word which
  498. is the PSP value of the owner of the block (which allocated it), followed by a
  499. word which is the size in paragraphs of the block. The last block has a
  500. signature of 5Ah. All others have 4Dh. If the owner is 0000 then the block is
  501. free.
  502.  
  503.  Once a memory control block has been created it should only be manipulated
  504. with the appropriate DOS function calls. Accidentally writing over any of the
  505. first 5 bytes of a memory control block can cause a memory allocation error
  506. and cause the system to lock up. If the first byte is overwritten with
  507. something other than an 'M' or a 'Z' then DOS will complain with an error
  508. return code of 7 signifying "Memory Control Blocks destroyed". However, should
  509. you change the ownership or block size bytes, you've had it.
  510.  
  511.  When a .COM program is first loaded by DOS and given control, the memory
  512. control block immediately preceding the Program Segment Prefix contains the
  513. following data:
  514.  
  515.       ID    = 'Z'
  516.       Owner = segment address of PSP (= CS register of .COM program)
  517.       Size    = number of available paragraphs in DOS memory pool
  518.  
  519.                                     -185-
  520.  
  521.   An .EXE file will have the following data in the memory control block for
  522. the program (just prior to the PSP):
  523.  
  524.       ID    = 'M'
  525.       Owner = segment address of PSP (= DS register of program)
  526.       Size    = the number of paragraphs allocated to the program according
  527.           to the information in the .EXE program header
  528.  
  529.  In the case of an .EXE program file the amount of memory allocated depends
  530. on the contents of the program header which informs the DOS loader how much to
  531. allocate for each of the segments in the program. With an .EXE program file
  532. there will always be a 'Z' memory control block created in memory immediately
  533. after the end of the space allocated to the program itself.
  534.  
  535.  One important fact to remember about DOS memory allocation is that blocks of
  536. RAM allocated by different calls to DOS function 48H will NOT be contiguous. At
  537. the very best, they will be separated by the 16 bytes of the memory control
  538. block, and at worst they could be anywhere in RAM that DOS manages to find a
  539. existing memory control block of sufficient size to accomodate the memory
  540. request.
  541.  
  542.  DOS treats the memory control blocks as a kind of linked list (term used
  543. loosely). It uses the earlier MCBs to find the later ones by calculating the
  544. location of the next one from the size of the prior one. As such, erasing any
  545. of the MCB data in the chain of MCBs will upset DOS severely, as each call for
  546. a new memory allocation causes DOS to scan the whole chain of MCBs looking for
  547. a free one that is large enough to fulfill the request.
  548.  
  549.  A separate MCB is created for the DOS environment strings at each program
  550. load, so there will be many copies of the environment strewn through memory
  551. when you have a lot of memory resident programs loaded. The memory control
  552. blocks for the DOS environment strings are not returned to the DOS memory pool
  553. if the program goes resident, as DOS will need to copy this enviroment for the
  554. next program loaded.
  555.  
  556.  
  557.  
  558.  
  559. DOS Program Segment ................................................... 6**6
  560.  
  561.  When you enter an external command or call a program through the EXEC function
  562. call, DOS determines the lowest availible address space to use as the start of
  563. available memory for the program being started. This area is called the Program
  564. Segment.
  565.  
  566.  At offset 0 within the program segment, DOS builds the Program Segment Prefix
  567. control block. EXEC loads the program after the Program Segment Prefix (at
  568. offset 100h) and gives it control.
  569.  
  570.  The program returns from EXEC by a jump to offset 0 in the Program Segment
  571.  
  572. Prefix, by issuing an int 20h, or by issuing an int 21h with register AH=00h or
  573. 4Ch, or by calling location 50h in the PSP with AH=00h or 4Ch.
  574.  
  575.  It is the responsibility of all programs to ensure that the CS register
  576. contains the segment address of the Program Segment Prefix when terminating by
  577. any of these methods except call 4Ch.
  578.  
  579.  All of these methods result in returning to the program that issued the EXEC.
  580. During this returning process, interrupt vectors 22h, 23h, and 24h (Terminate,
  581. Ctrl-Break, and Critical Error Exit addresses) are restored from the values
  582. saved in the PSP of the terminating program. Control is then given to the
  583. terminate address.
  584.  
  585.                                     -186-
  586.  
  587. When a program receives control, the following conditions are in effect:
  588.  
  589. For all programs:
  590.  
  591. 1) The segment address of the passed environment is contained at offset 2Ch in
  592.    the Program Segment Prefix.
  593.  
  594. 2) The environment is a series of ASCII strings totalling less than 32k bytes
  595.    in the form: "NAME=value"      The default environment is 160 bytes.
  596.     Each string is a maximum of 127 bytes terminated by a byte of zeroes for a
  597.    total of 128 bytes, and the entire set of strings is terminated by another
  598.    byte of zeroes. Following the byte of zeroes that terminates the set of
  599.    environment string is a set of initial arguments passed to a program that
  600.    contains a word count followed by an ASCIIZ string. The ASCIIZ string
  601.    contains the drive, path, and filename.ext of the executable program.
  602.    Programs may use this area to determine where the program was loaded from.
  603.    The environment built by the command processor (and passed to all programs
  604.    it invokes) contains a COMSPEC=string at a minimum (the parameter on COMSPEC
  605.    is the path used by DOS to locate COMMAND.COM on disk). The last PATH and
  606.    PROMPT commands issued will also be in the environment, along with any
  607.    environment strings entered through the SET command.
  608.     The environment that you are passed is actually a copy of the invoking
  609.    process's environment. If your application terminates and stays resident
  610.    through int 27h, you should be aware that the copy of the environment passed
  611.    to you is static. That is, it will not change even if subsequent PATH,
  612.    PROMPT, or SET commands are issued.
  613.     The size of the environment may be changed from its default of 160 bytes
  614.    by using the SHELL= command in the CONFIG.SYS from in DOS version 3.1 up,
  615.    or COMMAND.COM may be patched in earlier versions.
  616.  
  617.    The environment can be used to transfer information between processes or to
  618.    store strings for later use by application programs. The environment is
  619.    always located on a paragraph boundary. This is its format:
  620.     byte    ASCIIZ string 1
  621.     byte    ASCIIZ string 2
  622.         ....
  623.     byte    ASCIIZ string n
  624.     byte    of zeros (0)
  625.    Typically the environment strings have the form:
  626.     NAME = VALUE
  627.    The length of NAME or VALUE can be anything desired as long as it still fits
  628.    into the 123 byte space (4 bytes are used by "SET ").
  629.    Following the byte of zeros in the environment, a WORD indicates the number
  630.    of other strings following.
  631.  
  632.    If the environment is part of an EXECed command interpreter, it is followed
  633.    by a copy of the DS:DX filename passed to the child process. A zero value
  634.    causes the newly created process to inherit the parent's environment.
  635.  
  636. 3) Offset 05h in the PSP contains code to invoke the DOS function dispatcher.
  637.    Thus, by placing the desired function number in AH, a program can issue a
  638.    long call to PSP+05h to invoke a DOS function rather than issuing an int 21h.
  639.  
  640. 4) The disk transfer address (DTA) is set to 80h (default DTA in PSP).
  641.  
  642. 5) File Control Blocks 5Ch and 6Ch are formatted from the first two parameters
  643.    entered when the command was invoked. Note that if either parameter contained
  644.    a path name, then the corresponding FCB will contain only a valid drive
  645.    number. The filename field will not be valid.
  646.  
  647. 6) An unformatted parameter area at 81h contains all the characters entered
  648.    after the command name (including leading and imbedded delimiters), with 80h
  649.    set to the number of characters. If the <, >, or | parameters were entered
  650.    on the command line, they (and the filenames associated with them) will not
  651.    appear in this area, because redirection of standard input and output is
  652.    transparent to applications.
  653.  
  654.                                     -187-
  655.  
  656. (For EXE files only)
  657. 7) DS and ES registers are set to point to the PSP.
  658.  
  659. 8) CS, IP, SS, and SP registers are set to the values passed by the linker.
  660.  
  661. (For COM files only)
  662. 9) For COM files, offset 6 (one word) contains the number of bytes availible in
  663.    the segment.
  664.  
  665. 10) Register AX reflects the validity of drive specifiers entered with the
  666.     first two parameters as follows:
  667.     AH=0FFh if the second parameter contained an invalid drive specifier,
  668.         otherwise AH=00h.
  669.     AL=0FFh is the first parameter contained an invalid drive specifier,
  670.         otherwise AL=00h.
  671.  
  672. 11) All four segment registers contain the segment address of the inital
  673.     allocation block, that starts within the PSP control block. All of user
  674.     memory is allocated to the program. If the program needs to invoke another
  675.     program through the EXEC function call (4Bh), it must first free some memory
  676.     through the SETBLOCK function call to provide space for the program being
  677.     invoked.
  678.  
  679. 12) The Instruction Pointer (IP) is set to 100h.
  680.  
  681. 13) The SP register is set to the end of the program's segment. The segment
  682.     size at offset 6 is rounded down to the paragraph size.
  683.  
  684. 14) A word of zeroes is placed on top of the stack.
  685.