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

  1.                                     -188-
  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 E V E N
  10.  
  11.  
  12.                   DOS FILE STRUCTURE
  13.  
  14.  
  15.  
  16. File Management Functions ............................................. 7**1
  17. FCB Function Calls .................................................... 7**2
  18. Handle Function Calls ................................................. 7**3
  19. Special File Handles .................................................. 7**4
  20. Raw and Cooked File I/O ............................................... 7**5
  21. Number of Open Files Allowed  ......................................... 7**6
  22. Restrictions on FCB Usage ............................................. 7**7
  23. Restrictions on Handle usage .......................................... 7**8
  24. Allocating Space to a File ............................................ 7**9
  25. MSDOS / PCDOS Differences ............................................. 7**10
  26. .COM File Structure ................................................... 7**11
  27. .EXE File Structure ................................................... 7**12
  28. The Relocation Table .................................................. 7**13
  29. "NEW" .EXE Format (Microsoft Windows and OS/2) ........................ 7**14
  30. Standard File Control Block ........................................... 7**15
  31. Extended File Control Block ........................................... 7**16
  32. Disk Transfer Area .................................................... 7**17
  33.  
  34.  
  35.  
  36. File Management Functions ............................................. 7**1
  37.  
  38.  Use DOS function calls to create, open, close, read, write, rename, find, and
  39. erase files. There are two sets of function calls that DOS provides for support
  40. of file management. They are:
  41.  
  42.    * File Control Block function calls     (0Fh-24h)
  43.    * Handle function calls         (39h-69h)
  44.  
  45.  Handle function calls are easier to use and are more powerful than FCB calls.
  46. Microsoft recommends that the handle function calls be used when writing new
  47. programs. DOS 3.0 up have been curtailing use of FCB function calls; it is
  48. possible that future versions of DOS may not support FCB function calls.
  49.  
  50.  The following table compares the use of FCB calls to Handle function calls:
  51.  
  52.    ┌──────────────────────────────┬─────────────────────────────────────────┐
  53.    │           FCB Calls      │          Handle Calls            │
  54.    ├──────────────────────────────┼─────────────────────────────────────────┤
  55.    │  Access files in current      │  Access files in ANY directory        │
  56.    │  directory only.          │                        │
  57.    │                  │                        │
  58.    │  Requires the application      │  Does not require use of an FCB.        │
  59.    │  program to maintain a file  │  Requires a string with the drive,        │
  60.    │  control block to open,      │  path, and filename to open, create,    │
  61.    │  create, rename or delete      │  rename, or delete a file. For file     │
  62.    │  a file. For I/O requests,   │  I/O requests, the application program  │
  63.    │  the application program      │  must maintain a 16 bit file handle     │
  64.    │  also needs an FCB       │  that is supplied by DOS.            │
  65.    └──────────────────────────────┴─────────────────────────────────────────┘
  66.  
  67.                                     -189-
  68.  
  69.  The only reason an application should use FCB function calls is to maintain
  70. the ability to run under DOS 1.x. To to this, the program may use only function
  71. calls 00h-2Eh. Though the FCB function calls are frowned upon, many of the
  72. introductory assembly language programming texts use the FCB calls as examples.
  73.  
  74.  
  75.  
  76. FCB Function Calls .................................................... 7**2
  77.  
  78.  FCB function calls require the use of one File Control Block per open file,
  79. which is maintained by the application program and DOS. The application program
  80. supplies a pointer to the FCB and fills in the appropriate fields required by
  81. the specific function call. An FCB function call can perform file management on
  82. any valid drive, but only in the current logged directory. By using the current
  83. block, current record, and record length fields of the FCB, you can perform
  84. sequential I/O by using the sequential read or write function calls. Random I/O
  85. can be performed by filling in the random record and record length fields.
  86.  
  87.  Several possible uses of FCB type calls are considered programming errors and
  88. should not be done under any circumstances to avoid problems with file sharing
  89. and compatibility with later versions of DOS.
  90.  
  91.  Some errors are:
  92.  
  93. 1) If program uses the same FCB structure to access more than one open file. By
  94.    opening a file using an FCB, doing I/O, and then replacing the filename
  95.    field in the file control block with a new filename, a program can open a
  96.    second file using the same FCB. This is invalid because DOS writes control
  97.    information about the file into the reserved fields of the FCB. If the
  98.    program replaces the filename field with the original filename and then
  99.    tries to perform I/O on this file, DOS may become confused because the
  100.    control information has been changed. An FCB should never be used to open a
  101.    second file without closing the one that is currently open. If more than
  102.    one File Control Block is to be open concurrently, separate FCBs should be
  103.    used.
  104.  
  105. 2) A program should never try to use the reserved fields in the FCB, as the
  106.    function of the fields may change with different versions of DOS.
  107.  
  108. 3) A delete or a rename on a file that is currently open is considered an error
  109.    and should not be attempted by an application program.
  110.  
  111.  It is also good programming practice to close all files when I/O is done. This
  112. avoids potential file sharing problems that require a limit on the number of
  113. files concurrently open using FCB function calls.
  114.  
  115.  
  116.  
  117. Handle Function Calls ................................................. 7**3
  118.  
  119.  The recommended method of file management is by using the extended "handle"
  120. set of function calls. These calls are not restricted to the current directory.
  121. Also, the handle calls allow the application program to define the type of
  122. access that other processes can have concurrently with the same file if the
  123. file is being shared.
  124.  
  125.  To create or open a file, the application supplies a pointer to an ASCIIZ
  126. string giving the name and location of the file. The ASCIIZ string contains an
  127. optional drive letter, optional path, mandatory file specification, and a
  128. terminal byte of 00h. The following is an example of an ASCIIZ string:
  129.  
  130.      format:          [drive][path] FILENAME.EXT,0
  131.  
  132.      in MASM:          db "A:\PATH\FILENAME.EXT",0
  133.  
  134.                                     -190-
  135.  
  136.  
  137.  If the file is being created, the application program also supplies the
  138. attribute of the file. This is a set of values that defines the file read-only,
  139. hidden, system, directory, or volume label.
  140.  
  141.  If the file is being opened, the program can define the sharing and access
  142. modes that the file is opened in. The access mode informs DOS what operations
  143. your program will perform on this file (read-only, write-only, or read/write)
  144. The sharing mode controls the type of operations other processes may perform
  145. concurrently on the file. A program can also control if a child process
  146. inherits the open files of the parent. The sharing mode has meaning only if
  147. file sharing is loaded when the file is opened.
  148.  
  149.  To rename or delete a file, the appplication program simply needs to provide
  150. a pointer to the ASCIIZ string containing the name and location of the file
  151. and another string with the new name if the file is being renamed.
  152.  
  153.  The open or create function calls return a 16-bit value referred to as the
  154. file handle. To do any I/O to a file, the program uses the handle to reference
  155. the file. Once a file is opened, a program no longer needs to maintain the
  156. ASCIIZ string pointing to the file, nor is there any need to stay in the same
  157. directory. DOS keeps track of the location of the file regardless of what
  158. directory is current.
  159.  
  160.  Sequential I/O can be performed using the handle read (3Fh) or write (40h)
  161. function calls. The offset in the file that I/O is performed to is
  162. automatically moved to the end of what was just read or written. If random I/O
  163. is desired, the LSEEK (42h) function call can be used to set the offset into
  164. the file where I/O is to be performed.
  165.  
  166.  
  167.  
  168. Special File Handles .................................................. 7**4
  169.  
  170.  DOS reserves five special file handles for use by itself and applications
  171. programs. They are:
  172. ┌───────┬────────┬─────────────────────────────────────────────────────────────┐
  173. │ 0000h │ STDIN  │ standard input device    (input can be redirected)      │
  174. │ 0001h │ STDOUT │ standard output device    (output can be redirected)     │
  175. │ 0002h │ STDERR │ standard error output device (output cannot be redirected)  │
  176. │    │     │ NOTE: DOS opens STDERR for both writing and reading. Since  │
  177. │    │     │ STDIN can be redirected, using STDERR to read the keyboard  │
  178. │    │     │ is a reliable way to ensure that your program is actually   │
  179. │    │     │ reading the keyboard, if that's what you want to do.        │
  180. │ 0004h │ STDAUX │ standard auxiliary device                       │
  181. │ 0005h │ STDPRN │ standard printer device (PRN, normally LPT1)            │
  182. └───────┴────────┴─────────────────────────────────────────────────────────────┘
  183.  
  184.  These handles are predefined by DOS and can be used by an application program.
  185. They do not need to be opened by a program, although a program can close these
  186. handles. STDIN should be treated as a read-only file, and STDOUT and STDERR
  187. should be treated as write-only files. STDIN and STDOUT can be redirected. All
  188. handles inherited by a process can be redirected, but not at the command line.
  189.  These handles are very useful for doing I/O to and from the console device.
  190. For example, you could read input from the keyboard using the read (3Fh)
  191. function call and file handle 0000h (STDIN), and write output to the console
  192. screen with the write function call (40h) and file handle 0001h (STDOUT). If
  193. you wanted an output that could not be redirected, you could output it using
  194. file handle 0002h (STDERR). This is very useful for error messages that must
  195. be seen by a user.
  196.  
  197.  File handles 0003h (STDAUX) and 0004h (STDPRN) can be both read from and
  198. written to. STDAUX is typically a serial device and STDPRN is usually a
  199. parallel device.
  200.  
  201.                                     -191-
  202.  
  203. Raw and Cooked File I/O ............................................... 7**5
  204.  
  205.  Raw and cooked modes originated in the Unix world and were provided with DOS
  206. 2.x+. They apply only to character I/O (including the keyboard, screen,
  207. printer and serial ports - but not block devices like disk drives), and only
  208. to the "new" 2.x file handle I/O functions (not the old FCB file I/O
  209. functions). Raw mode is called "binary" mode in DOS 3.x+, and cooked mode is
  210. called "ASCII." The common raw-cooked convention is from DOS 2.x and other
  211. operating systems.
  212.  
  213.  The five predefined DOS file handles are all devices, so the mode can be
  214. changed from raw to cooked via IOCTL. These handles are in cooked mode when
  215. initialized by DOS. Regular file handles that are not devices are always in
  216. raw mode and cannot be changed to cooked mode.
  217.  
  218.  The predefined file handles STDIN (0000h) and STDOUT (0001h) and STDERR
  219. (0002h) are all duplicate handles. If the IOCTL function call is used to change
  220. the mode of any of these three handles, the mode of all three handles is
  221. changed. For example, if IOCTL was used to change STDOUT to raw, then STDIN
  222. and STDERR would also be changed to raw mode.
  223.  
  224.  In the default cooked mode, DOS examines the character I/O data stream for
  225. certain special control characters, and takes specific actions if they are
  226. found. For example, Ctrl-C is treated as a Break interrupt, Ctrl-S pauses the
  227. screen display, and Ctrl-Z is treated as end-of-file. (If you try to send
  228. Ctrl-Z to a printer through a DOS file handle in cooked mode, DOS closes the
  229. printer file!)    Also, input is buffered within DOS until a CR is detected - so
  230. you can't process each key as it is pressed.
  231.  
  232.  In raw mode, DOS ignores special characters, passing them through without any
  233. special processing, and does not buffer input lines. So to use file handle I/O
  234. and send bit-mapped graphics to a printer through DOS, or process individual
  235. keystrokes immediately, or bypass Ctrl-C checking, you need to switch the file
  236. handle to raw mode. Raw mode is not automatically reset to cooked mode by DOS
  237. when a program terminates, so it is a good idea to reset the file into cooked
  238. mode before your program exits if the system was in cooked mode to begin with.
  239. I/O to files is done in raw mode.
  240.  
  241.  To set a file handle into raw mode or back into cooked mode, use DOS IOCTL
  242. (int 21h Fn 44h, Chapter 4):
  243.  
  244.    1.  Get the current mode bits (Subfunction 0).
  245.    2.  Check that the file is a character file.  (If not, exit.)
  246.    3.  Switch the cooked mode bit to raw or vice versa.
  247.    4.  Set the mode bits (Subfunction 1).
  248.  
  249.  Microsoft C v4 and later do NOT set raw mode for binary files. When running
  250. with the CON driver set to raw mode (to enhance display speed) programs
  251. compiled in MSC will crash the computer.A letter to Microsoft reporting this
  252. odd behavior got the somewhat bizarre reply that "Microsoft does not support
  253. the use of any TSRs" from their techs. Raw mode is clearly documented by both
  254. IBM and Microsoft, and their own tools should take it into account.
  255.  
  256.                                     -192-
  257.  
  258. FILE I/O IN BINARY (RAW) MODE├─────────────────────────────────────────────────
  259.  
  260. The following is true when a file is read in binary mode:
  261.  
  262. 1)  The characters ^S (scroll lock), ^P (print screen), ^C (control break) are
  263.     not checked for during the read. Therefore, no printer echo occurs if ^S or
  264.     ^P are read.
  265. 2)  There is no echo to STDOUT (0001h).
  266. 3)  Read the number of specified bytes and returns immediately when the last
  267.     byte is received or the end of file reached.
  268. 4)  Allows no editing of the input using the function keys if the input is from
  269.     STDIN (0000h).
  270.  
  271.  
  272. The following is true when a file is written to in binary mode:
  273.  
  274. 1)  The characters ^S (scroll lock), ^P (print screen), ^C (control break) are
  275.     not checked for during the write. Therefore, no printer echo occurs.
  276. 2)  There is no echo to STDOUT (0001h).
  277. 3)  The exact number of bytes specified are written.
  278. 4)  Does not caret (^) control characters. For example, Ctrl-D is sent out as
  279.     byte 04h instead of the two bytes ^ and D.
  280. 5)  Does not expand tabs into spaces.
  281.  
  282.  
  283. FILE I/O IN ASCII (COOKED) MODE├───────────────────────────────────────────────
  284.  
  285. The following is true when a file is read in ASCII mode:
  286.  
  287. 1)  Checks for the characters ^C,^S, and ^P.
  288. 2)  Returns as many characters as there are in the device input buffer, or the
  289.     number of characters requested, whichever is less. If the number of
  290.     characters requested was less than the number of characters in the device
  291.     buffer, then the next read will address the remaining characters in the
  292.     buffer.
  293. 3)  If there are no more bytes remaining in the device input buffer, read a
  294.     line (terminated by ^M) into the buffer. This line may be edited with the
  295.     function keys. The characters returned terminated with a sequence of 0Dh,
  296.     0Ah (^M,^J) if the number of characters requested is sufficient to include
  297.     them. For example, if 5 characters were requested, and only 3 were entered
  298.     before the carriage return (0Dh or ^M) was presented to DOS from the
  299.     console device, then the 3 characters entered and 0Dh and 0Ah would be
  300.     returned. However, if 5 characters were requested and 7 were entered before
  301.     the carriage return, only the first 5 characters would be returned. No 0Dh,
  302.     0Ah sequence would be returned in this case. If less than the number of
  303.     characters requested are entered when the carriage return is received, the
  304.     characters received and 0Dh, 0Ah would be returned. The reason the 0Ah
  305.     (linefeed or ^J) is added to the returned characters is to make the devices
  306.     look like text files.
  307. 4)  If a 1Ah (^Z) is found, the input is terminated at that point. No 0Dh, 0Ah
  308.     (CR,LF) sequence is added to the string.
  309. 5)  Echoing is performed.
  310. 6)  Tabs are expanded.
  311.  
  312.  
  313. The following is true when a file is written to in ASCII mode:
  314.  
  315. 1)  The characters ^S,^P,and ^C are checked for during the write operation.
  316. 2)  Expands tabs to 8-character boundaries and fills with spaces (20h).
  317. 3)  Carets control chars, for example, ^D is written as two bytes, ^ and D.
  318. 4)  Bytes are output until the number specified is output or a ^Z is
  319.     encountered. The number actually output is returned to the user.
  320.  
  321.                                     -193-
  322.  
  323. Number of Open Files Allowed  ......................................... 7**6
  324.  
  325.  The number of files that can be open concurrently is restricted by DOS. This
  326. number is determined by how the file is opened or created (FCB or handle
  327. function call) and the number specified by the FCBS and FILES commands in the
  328. CONFIG.SYS file. The number of files allowed open by FCB function calls and the
  329. number of files that can be opened by handle type calls are independent of one
  330. another.
  331.  
  332.  
  333.  
  334. Restrictions on FCB Usage ............................................. 7**7
  335.  
  336.  If file sharing is not loaded using the SHARE command, there is no restriction
  337. on the number of files concurrently open using FCB function calls.
  338.  
  339.  However, when file sharing is loaded, the maximum number of FCBs open is set
  340. by the the FCBS command in the CONFIG.SYS file.
  341.  
  342.  The FCBS command has two values you can specify, 'm' and 'n'. The value for
  343. 'm' specifies the number of files that can be opened by FCBs, and the value 'n'
  344. specifies the number of FCBs that are protected from being closed.
  345.  
  346.  When the maximum number of FCB opens is exceeded, DOS automatically closes the
  347. least recently used file. Any attempt to access this file results in an int 24h
  348. critical error message "FCB not availible". If this occurs while an application
  349. program is running, the value specified for 'm' in the FCBS command should be
  350. increased.
  351.  
  352.  When DOS determines the least recently used file to close, it does not include
  353. the first 'n' files opened, therefore the first 'n' files are protected from
  354. being closed.
  355.  
  356.  
  357.  
  358. Restrictions on Handle usage .......................................... 7**8
  359.  
  360.  The number of files that can be open simultaneously by all processes is
  361. determined by the FILES command in the CONFIG.SYS file. The number of files a
  362. single process can open depends on the value specified for the FILES command.
  363. If FILES is greater than or equal to 20, a single process can open 20 files. If
  364. FILES is less than 20, the process can open less than 20 files. This value
  365. includes the three predefined handles STDIN, STDOUT, and STDERR. This means
  366. only 17 additional handles can be added. DOS 3.3+ includes a function to use
  367. more than 20 files per application.
  368.  
  369.  
  370.  
  371. Allocating Space to a File ............................................ 7**9
  372.  
  373.  Files are not necessarily written sequentially on a disk. Space is allocated
  374. as needed and the next location availible on the disk is allocated as space for
  375. the next file being written. Therefore, if considerable file generation has
  376. taken place, newly created files will not be written in sequential sectors.
  377. However, due to the mapping (chaining) of file space via the File Allocation
  378. Table (FAT) and the function calls availible, any file may be used in either a
  379. sequential or random manner.
  380.  
  381.  Space is allocated in increments called clusters. Cluster size varies
  382. according to the media type. An application program should not concern itself
  383. with the way that DOS allocates space to a file. The size of a cluster is only
  384. important in that it determines the smallest amount of space that can be
  385. allocated to a file. A disk is considered full when all clusters have been
  386. allocated to files.
  387.  
  388.                                     -194-
  389.  
  390.  A DOS file can be up to (2^32)-1 (4,294,967,295) bytes long. This is the
  391. maximum value that can be put in the dword in the FAT which holds the file size
  392. information, less one byte for the end of file marker.
  393.  
  394.  
  395.  
  396. MSDOS / PCDOS Differences ............................................. 7**10
  397.  
  398.  There is a problem of compatibility between MS-DOS and IBM PC-DOS having to
  399. do with FCB Open and Create. The IBM 1.0, 1.1, and 2.0 documentation of OPEN
  400. (call 0Fh) contains the following statement:
  401.  
  402.  "The current block field (FCB bytes C-D) is set to zero [when an FCB is
  403. opened]."
  404.  
  405.  This statement is NOT true of MS-DOS 1.25 or MS-DOS 2.00. The difference is
  406. intentional, and the reason is CP/M 1.4 compatibility. Zeroing that field is
  407. not CP/M compatible. Some CP/M programs will not run when machine translated if
  408. that field is zeroed. The reason it is zeroed in the IBM versions is that IBM
  409. specifically requested that it be zeroed. This is the reason for the complaints
  410. from some vendors about the fact that IBM MultiPlan will not run under MS-DOS.
  411. It is probably the reason that some other IBM programs don't run under MS-DOS.
  412.  
  413. NOTE: Do what all MS/PC-DOS systems programs do: Set every single FCB field you
  414. want to use regardless of what the documentation says is initialized.
  415.  
  416.  
  417.  
  418. .COM File Structure ................................................... 7**11
  419.  
  420.  The COM file structure was designed for DOS 1.0 and maximum compatibility
  421. with programs ported from the CP/M operating system. COM files normally
  422. comprise one segment only. A COM file is loaded as a memory image of the disk
  423. file and the Instruction Pointer is set to offset 100h within the program.
  424.  
  425.  
  426.  
  427. .EXE File Structure ................................................... 7**12
  428.  
  429.  The EXE file is the native mode for DOS. EXE files may make use of multiple
  430. segments for code, stack, and data. The design of the EXE file reflects the
  431. segmented design of the Intel 80x86 CPU architecture. EXE files may be as
  432. large as availible memory and may make references to specific segment addresses.
  433.  
  434.  The EXE files produced by the Linker program consist of two parts, control and
  435. relocation information and the load module itself.
  436.  
  437.  The control and relocation information, which is described below, is at the
  438. beginning of the file in an area known as the header. The load module
  439. immediately follows the header. The load module begins in the memory image of
  440. the module contructed by the Linker.
  441.  
  442.  When you are loading a file with the name *.EXE, DOS does NOT assume that it
  443. is an EXE format file. It looks at the first two bytes for a signature (the
  444. letters MZ) telling it that it is an EXE file. If it has the proper signature,
  445. then the load proceeds. Otherwise, it presumes the file to be a .COM format
  446. file.
  447.  
  448.  If the file has the EXE signature, then the internal consistency is checked.
  449. Pre-2.0 versions of MSDOS did not check the signature byte for EXE files.
  450.  
  451.  The .EXE format can support programs larger than 64K. It does this by
  452. allowing separate segments to be defined for code, data, and the stack, each
  453. of which can be up to 64K long. Programs in EXE format may contain explicit
  454. references to segment addresses. A header in the EXE file has information for
  455. DOS to resolve these references.
  456.  
  457.                                     -195-
  458.  
  459.  EXE file size does not reflect the amount of RAM an EXE file might use, since
  460. stack space or extra RAM may be allocated by the EXE loader at init. Some
  461. programs may also store their overlay files in the main EXE file.
  462.  
  463.  
  464. ┌─────────────────────────────────────────────────────────────────────────────┐
  465. │             E X E     F I L E   H E A D E R                  │
  466. ├─────────┬──────┬────────────────────────────────────────────────────────────┤
  467. │ Offset  │ Size │              C O N T E N T S                  │
  468. ├─────────┼──────┼─────┬──────────────────────────────────────────────────────┤
  469. │   00h   │ BYTE │ 4Dh │ The Linker's signature to mark the file as a valid   │
  470. ├─────────┼──────┼─────┤ .EXE file (ASCII letters M and Z, for Mark Zbikowski,│
  471. │   01h   │ BYTE │ 5Ah │ one of the major DOS programmers at Microsoft)       │
  472. ├─────────┼──────┼─────┴──────────────────────────────────────────────────────┤
  473. │ 02h-03h │     │ Length of the image mod 512 (remainder after dividing the  │
  474. │      │ WORD │ load module image size by 512) (including header)          │
  475. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  476. │ 04h-05h │ WORD │ Size of the file in 512 byte pages including the header.   │
  477. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  478. │ 06h-07h │ WORD │ Number of relocation table items following the header.     │
  479. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  480. │ 08h-09h │ WORD │ Size of the header in 16 byte (paragraphs). This is used   │
  481. │      │     │ to locate the beginning of the load module in the file.    │
  482. │      │     │ Although the DOS loader will allow the actual program to   │
  483. │      │     │ begin at any location specified by this value some          │
  484. │      │     │ debuggers, including Microsoft's, need the program to be   │
  485. │      │     │ aligned on a 512-byte boundary in the .EXE file.          │
  486. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  487. │ 0Ah-0Bh │ WORD │ Minimum number of 16 byte paragraphs required above the    │
  488. │      │     │ end of the loaded program.                      │
  489. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  490. │ 0Ch-0Dh │ WORD │ Max number of 16 byte paragraphs required above the end of │
  491. │      │     │ the loaded program. If the minimum and maximum number of   │
  492. │      │     │ paragraphs are both zero, the program will be loaded as    │
  493. │      │     │ high in memory as possible.                      │
  494. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  495. │ 0Eh-0Fh │ WORD │ Displacement in paragraphs of stack segment within load    │
  496. │      │     │ module. This size must be adjusted by relocation.          │
  497. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  498. │ 10h-11h │ WORD │Offset to be in SP register when the module is given control│
  499. │      │     │   (stack offset)                          │
  500. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  501. │ 12h-13h │ WORD │ Checksum - 16-bit negative sum of all the words in the     │
  502. │      │     │ file, ignoring overflow.                      │
  503. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  504. │ 14h-15h │ WORD │ Offset for the IP register when the module is given control│
  505. │      │     │ (initial instruction pointer)                  │
  506. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  507. │ 16h-17h │ WORD │ Offset in paragraphs of code segment (CS) within load      │
  508. │      │     │ module. This size must be adjusted by relocation.          │
  509. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  510. │ 18h-19h │ WORD │ Offset in bytes of first relocation item in the file.      │
  511. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  512. │ 1Ah-1Bh │ WORD │ Overlay number (0 for the resident part of the program)    │
  513. ├─────────┼──────┼────────────────────────────────────────────────────────────┤
  514. │   1Ch   │ BYTE │ (undocumented) Microsoft compilers put 01h here, which may │
  515. │      │     │ indicate the version of .EXE header structure for later    │
  516. │      │     │ changes. Normally the relocation table begins at 1Eh, but  │
  517. │      │     │ that can be changed to a different location if the word at │
  518. │      │     │ 18-19h is adjusted to match.                   │
  519. └─────────┴──────┴────────────────────────────────────────────────────────────┘
  520.  
  521.                                     -196-
  522.  
  523. The Relocation Table .................................................. 7**13
  524.  
  525.  The word at 18h locates the first entry in the relocation table. The
  526. relocation table is made up of a variable number of relocation items. The number
  527. of items is contained at offset 06h. The relocation item contains two fields
  528. - a 2 byte offset value, followed by a 2 byte segment value. These two fields
  529. represent the displacement into the load module before the module is given
  530. control. The process is called relocation and is accomplished as follows:
  531.  
  532. 1. The formatted part of the header is read into memory. Its size is 1Bh.
  533.  
  534. 2. A portion of memory is allocated depending on the size of the load module
  535.    and the allocation numbers in offsets 0Ah and 0Ch. DOS always tries to
  536.    allocate 0FFFFh paragraphs. Since this call will always fail, the function
  537.    returns the amount of free memory. If this block is larger than the minimum
  538.    specified at offset 0Ah and the loaded program size, DOS will allocate the
  539.    size specified at offset 0Ch or the largest free memory space, whichever is
  540.    less.
  541.  
  542. 3. A Program Segment Prefix is built following the resident portion of the
  543.    program that is performing the load operation.
  544.  
  545. 4. The formatted part of the header is read into memory (its size is at
  546.    offset 08h)
  547.  
  548. 5. The load module size is determined by subtracting the header size from the
  549.    file size. Offsets 04h and 08h can be used for this calculation. The actual
  550.    size is downward adjusted based on the contents of offset 02h. Note that all
  551.    files created by the Linker programs prior to version 1.10 always placed a
  552.    value of 4 at this location, regardless of the actual program size.
  553.    Therefore, Microsoft recommends that this field be ignored if it contains a
  554.    value of 4. Based on the setting of the high/low loader switch, an
  555.    appropriate segment is determined for loading the load module. This segment
  556.    is called the start segment.
  557.  
  558. 6. The load module is read into memory beginning at the start segment. The
  559.    relocation table is an ordered list of relocation items. The first relocation
  560.    item is the one that has the lowest offset in the file.
  561.  
  562. 7. The relocation table items are read into a work area one or more at a time.
  563.  
  564. 8. Each relocation table item segment value is added to the start segment value.
  565.    The calculated segment, in conjunction with the relocation item offset value,
  566.    points to a word in the load module to which is added the start segment
  567.    value. The result is placed back into the word in the load module.
  568.  
  569. 9. Once all the relocation items have been processed, the SS and SP registers
  570.    are set from the values in the header and the start segment value is added
  571.    to SS. The ES and DS registers are set to the segment address of the program
  572.    segment prefix. The start segment value is added to the header CS register
  573.    value. The result, along with the header IP value, is used to give the
  574.    module control.
  575.  
  576.  
  577.  
  578. "NEW" .EXE Format (Microsoft Windows and OS/2) ........................ 7**14
  579.  
  580.  The "old" EXE format is documented here. The "new" EXE format puts more
  581. information into the header section and is currently used in applications that
  582. run under Microsoft Windows. The linker that creates these files comes with the
  583. Microsoft Windows Software Development Kit and is called LINK4. If you try to
  584. run a Windows-linked program under DOS, you will get the error message "This
  585. program requires Microsoft Windows". The OS/2 1.x file format is assentially
  586. the same as the Windows format.
  587.  
  588.                                     -197-
  589.  
  590.  Windows executables have dynamic linking and all of the code/data inside an
  591. EXE file is not necessarily loaded at run time.
  592.  
  593.  Offset 3Ch dword: offset of new .EXE header (for Microsoft Windows & OS/2)
  594.  
  595.  
  596.  
  597. Standard File Control Block ........................................... 7**15
  598.  
  599.  The standard file control block is defined as follows, with offsets in hex:
  600.  
  601. ┌──────────────────────────────────────────────────────────────────────────────┐
  602. │          F I L E      C O N T R O L      B L O C K               │
  603. ├───────┬─────────┬────────────────────────────────────────────────────────────┤
  604. │offset │  size   │                Function                   │
  605. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  606. │   0    │ 1 byte  │ Drive number. For example:                       │
  607. │    ├─────────┴────────────────────────────────────────────────────────────┤
  608. │    │ Before open:      00h = default drive                       │
  609. │    │          01h = drive A:                       │
  610. │    │          02h = drive B: etc.                       │
  611. │    │ After open:      00h = drive C:                       │
  612. │    │          01h = drive A:                       │
  613. │    │          02h = drive B: etc.                       │
  614. │    │ A zero is replaced by the actual drive number during opening.        │
  615. ├───────┼─────────┬────────────────────────────────────────────────────────────┤
  616. │  1-8    │ 8 bytes │ Filename, left justified with blanks.               │
  617. │    ├─────────┴────────────────────────────────────────────────────────────┤
  618. │    │ If a reserved device name is placed here (such as PRN) do not        │
  619. │    │ include the optional colon.                           │
  620. ├───────┼─────────┬────────────────────────────────────────────────────────────┤
  621. │  9-B    │ 3 bytes │ Filename extension, left justified with trailing blanks.   │
  622. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  623. │  C-D    │ 2 bytes │ Current block # relative to start of file, starting with 0 │
  624. │    ├─────────┴────────────────────────────────────────────────────────────┤
  625. │    │ (set to 0 by the OPEN function call). A block consists of 128        │
  626. │    │ records, each of the size specified in the logical record size field.│
  627. │    │ The current block number is used with the current record field       │
  628. │    │ (below) for sequential reads and writes.                   │
  629. ├───────┼─────────┬────────────────────────────────────────────────────────────┤
  630. │  E-F    │ 2 bytes │ Logical record size in bytes.                   │
  631. │    ├─────────┴────────────────────────────────────────────────────────────┤
  632. │    │ Set to 80h by OPEN function. If this is not correct, you must set    │
  633. │    │ the value because DOS uses it to determine the proper locations in   │
  634. │    │ the file for all disk reads and writes.                   │
  635. ├───────┼─────────┬────────────────────────────────────────────────────────────┤
  636. │ 10-13 │ 4 bytes │ File size in bytes.                        │
  637. │    ├─────────┴────────────────────────────────────────────────────────────┤
  638. │    │ In this field, the first word is the low-order part of the size.     │
  639. ├───────┼─────────┬────────────────────────────────────────────────────────────┤
  640. │ 14-15 │ 2 bytes │Date file was created or last updated.               │
  641. │    ├─────────┴────────────────────────────────────────────────────────────┤
  642. │    │ MM/DD/YY are mapped as follows:                       │
  643. │    │      15  14  13  12  11  10  9  8    7  6  5  4  3  2  1  0           │
  644. │    │      y   y   y   y   y   y   y  m    m  m  m  d  d  d  d  d           │
  645. │    │ where:        mm is 1-12                           │
  646. │    │            dd is 1-31                           │
  647. │    │            yy is 0-119 (1980-2099)                   │
  648. └───────┴──────────────────────────────────────────────────────────────────────┘
  649.  
  650.                                     -198
  651. ┌───────┬────────┬─────────────────────────────────────────────────────────────┐
  652. │ 16-17 │ 2 bytes│ Time file was created or last updated.               │
  653. ├───────┼────────┴─────────────────────────────────────────────────────────────┤
  654. │    │ These bytes contain the time when the file was created or last       │
  655. │    │  updated. The time is mapped in the bits as follows:               │
  656. │    ├───────────────────────────────┬───────────────────────────────┐      │
  657. │    │      B Y T E   16h     │      B Y T E   17h     │      │
  658. │    ├───────────────────────────────┼───────────────────────────────┤      │
  659. │    │ F   E   D   C   B   A   9   8 │ 7   6   5   4   3   2   1   0 │      │
  660. │    ├───────────────────┬───────────┴───────────┬───────────────────┤      │
  661. │    │ H   H   H   H   H │ M   M   M   M   M   M │ D   D   D   D   D │      │
  662. │    ├───────────────────┼───────────────────────┼───────────────────┤      │
  663. │    │ binary # hrs 0-23 │ binary # minutes 0-59 │ bin. # 2-sec incr │      │
  664. │    ├───────────────────┴───────────────────────┴───────────────────┘      │
  665. │    │ note: The time is stored with the least significant byte first.      │
  666. ├───────┼────────┬─────────────────────────────────────────────────────────────┤
  667. │ 18-19 │ 2 bytes│ Reserved for DOS.                           │
  668. ├───────┼────────┼─────────────────────────────────────────────────────────────┤
  669. │  20    │ 1 byte │Current relative record number.                   │
  670. │    ├────────┴─────────────────────────────────────────────────────────────┤
  671. │    │ (0-127) within the current block. This field and the Current Block   │
  672. │    │ field at offset 0Ch make up the record pointer. This field is not    │
  673. │    │ initialized by the OPEN (0Fh) function call. You must set this field │
  674. │    │ before doing sequential read-write operations to the diskette. To    │
  675. │    │ read the first record of a file, set this value to zero.           │
  676. ├───────┼─────────┬────────────────────────────────────────────────────────────┤
  677. │ 21-25 │ 4 bytes │ Relative Record.                           │
  678. │    ├─────────┴────────────────────────────────────────────────────────────┤
  679. │    │ Points to the currently selected record, counting from the beginning │
  680. │    │ of the file starting with 0. This field is not initialized by the    │
  681. │    │ OPEN system call. You must set this field before doing a random read │
  682. │    │ or write to the file.                            │
  683. │    │  If the record size is less than 64 bytes, both words are used.      │
  684. │    │ Otherwise, only the first 3 bytes are used. Note that if you use the │
  685. │    │ File Control Block at 5Ch in the program segment, the last byte of   │
  686. │    │ the FCB overlaps the first byte of the unformatted parameter area.   │
  687. └───────┴──────────────────────────────────────────────────────────────────────┘
  688.  
  689. note 1) An unopened FCB consists of the FCB prefix (if used), drive number, and
  690.     filename.ext properly filled in. An open FCB is one in which the
  691.     remaining fields have been filled in by the CREAT or OPEN function
  692.     calls.
  693.      2) Bytes 0-5 and 32-36 must be set by the user program. Bytes 16-31 are
  694.     set by DOS and must not be changed by user programs.
  695.      3) All word fields are stored with the least significant byte first. For
  696.     example, a record length of 128 is stored as 80h at offset 14, and 00h
  697.     at offset 15.
  698.  
  699.  
  700.  
  701. Extended File Control Block ........................................... 7**16
  702.  
  703.  The extended file control block is used to create or search for files in the
  704. disk directory that have special attributes.
  705.  
  706.                                     -199-
  707.  
  708. It adds a 7 byte prefix to the FCB, formatted as follows:
  709.  
  710. ┌──────────────────────────────────────────────────────────────────────────────┐
  711. │    E X T E N D E D     F I L E     C O N T R O L        B L O C K           │
  712. ├───────┬─────────┬────────────────────────────────────────────────────────────┤
  713. │Offset │   Size  │               Function                   │
  714. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  715. │  00h    │ 1 byte  │ Flag byte containing 0FFh to indicate an extended FCB      │
  716. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  717. │  01h    │ 4 bytes │ Reserved by Microsoft                       │
  718. ├───────┼─────────┼────────────────────────────────────────────────────────────┤
  719. │  06h    │ 2 bytes │ Attribute byte                           │
  720. │    ├─────┬───┼────────────────────────────────────────────────────────────┤
  721. │    │ hex │bit│               meaning                       │
  722. │    ├─────┼───┼────────────────────────────────────────────────────────────┤
  723. │    │ 00h │   │ (no bits set) normal; can be read or written without       │
  724. │    │     │   │ restriction                            │
  725. │    │ 01h │ 0 │ file is marked read-only. An attempt to open the file for  │
  726. │    │     │   │ output using int 21h/fn 3Dh will fail and an error code    │
  727. │    │     │   │ will be returned. This value can be used with other values │
  728. │    │     │   │ below.                               │
  729. │    │ 02h │ 1 │ indicates a hidden file. The file is excluded from normal  │
  730. │    │     │   │ directory searches.                        │
  731. │    │ 04h │ 2 │ indicates a system file. The file is excluded from normal  │
  732. │    │     │   │ directory searches.                        │
  733. │    │ 08h │ 3 │ indicates that the entry contains the volume label in the  │
  734. │    │     │   │ first 11 bytes. The entry has no other usable information  │
  735. │    │     │   │ and may exist only in the root directory.               │
  736. │    │ 10h │ 4 │ indicates that the file is a subdirectory               │
  737. │    │ 20h │ 5 │ indicates an archive bit. This bit is set to on whenever   │
  738. │    │     │   │ the file is written to and closed. Used by BACKUP and      │
  739. │    │     │   │ RESTORE.                               │
  740. │    │     │ 6 │ reserved, set to 0                           │
  741. │    │     │ 7 │ reserved, set to 0                           │
  742. │    ├─────┴───┴────────────────────────────────────────────────────────────┤
  743. │    │ note 1) Bits 6 and 7 may be used in OS/2.                   │
  744. │    │ note 2) Attributes 08h and 10h cannot be changed using int21/43h.    │
  745. │    │ note 3) The system files IBMBIO.COM and IBMDOS.COM (or customized    │
  746. │    │      equivalent) are marked as read-only, hidden, and system      │
  747. │    │      files. Files can be marked hidden when they are created.     │
  748. │    │ note 4) Read-only, hidden, system and archive attributes may be      │
  749. │    │      changed with int21h/fn43h.                       │
  750. │    │                                       │
  751. │    │ Refer to int 21h/fn11h (Search First) for details on using the       │
  752. │    │ attribute bits during directory searches. This function is present   │
  753. │    │ to allow applications to define their own files as hidden (and       │
  754. │    │ thereby excluded from normal directory searches) and to allow        │
  755. │    │ selective directory searches                           │
  756. └───────┴──────────────────────────────────────────────────────────────────────┘
  757.  
  758.  Any reference in the DOS function calls to an FCB, whether opened or unopened,
  759. may use either a normal or extended FCB. If you are using an extended FCB, the
  760. appropriate register should be set to the first byte of the prefix, rather than
  761. the drive-number field.
  762.  
  763.  Common practice is to refer to the extended FCB as a negative offset from the
  764. first byte of a standard File Control Block.
  765.  
  766.  
  767. Disk Transfer Area .................................................... 7**17
  768.  
  769.  The old-style (DOS 1.x compatible) FCB-oriented function calls use a buffer
  770. called the Disk Transfer Area (DTA) for disk access. Use of the DTA is
  771. documented in Chapter 6, "DOS Memory Control Blocks and Work Areas."
  772.  
  773.