Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  As you will soon discover, the PSP contains a rather confusing mixture of
  items. (See Figure 15-9.) The background and history of DOS pull it in
  different directions--backward to the earlier CP/M system and forward to
  UNIX-type operating environments. As a result, the PSP contains elements
  that serve different purposes and are oriented to different programming
  methods. We'll discuss the elements in the order in which they appear.

  The field at offset 00H (2 bytes) contains bytes CDH and 20H, the
  interrupt 20H instruction. As we saw in the discussion of interrupt 20H in
  this chapter, this interrupt is only one of several standard ways for a
  program to terminate. This instruction is placed at the beginning of the
  PSP (at offset 00H) so that a program can end itself simply by jumping to
  this location when the CS points to the PSP. As you might guess, this is
  not the most sensible thing for a program to do; it's always best to go
  through the appropriate interrupt or function call. This odd method of
  terminating a program is a relic of the days when CP/M compatibility was
  important.

       Offset              Length
  Hex         Dec          (bytes)     Description
  --------------------------------------------------------------------------
  00H           0            2         INT 20H instruction
  02H           2            2         Size of memory (in paragraphs)
  04H           4            1         (Reserved; normally 0)
  05H           5            5         Call to DOS function dispatcher
  0AH          10            4         Interrupt 22H (Terminate) address
  0EH          14            4         Interrupt 23H (Ctrl-C) address
  12H          18            4         Interrupt 24H (Critical Error)
                                       address
  16H          22           22         (Reserved)
  2CH          44            2         Environment segment address
  2EH          46           34         Reserved
  50H          80            3         INT 21H, RETF instructions
  53H          83            9         (Reserved)
  5CH          92           16         FCB #1
  6CH         108           20         FCB #2
  80H         128          128         Command-line parameters and default
                                       Disk Transfer Area (DTA)
  --------------------------------------------------------------------------

  Figure 15-9.  The parts of the program segment prefix (PSP).

  The field at offset 02H (2 bytes) contains the segment address of the last
  paragraph of memory allocated to the program. DOS normally loads a program
  in the first free area of memory large enough to contain the program. A
  program can use this field to determine the actual size of the memory area
  allocated to it.

  In practice, there's a better way to determine the amount of memory
  allocated to a program. Interrupt 21H, function 4AH can return the size
  of any block of memory, not just the block into which a program is loaded.
  (See Chapter 17 for more on this DOS service.)

  The field at offset 05H (5 bytes) contains a long call to the DOS function
  dispatcher, the internal DOS routine that examines the function number you
  pass to DOS and executes the corresponding service routine. This field,
  too, is a remnant of the days when CP/M compatibility was important to DOS
  programmers. A program can make a near CALL to offset 05H in the PSP with
  a function number in register CL and get the same result as if it had
  loaded AH with the function number and executed interrupt 21H.

  Needless to say, this technique is not very useful in real-world DOS
  programs.

  The fields at offsets 0AH, 0EH, and 12H (4 bytes each) contain the
  segmented addresses of the default handlers for interrupt 22H (Terminate),
  23H (Ctrl-C), and 24H (Critical Error). These addresses are stored in the
  PSP for your convenience. If you substitute a customized interrupt handler
  for one of the DOS handlers, you can restore the default handler by
  copying its address from the PSP into the corresponding interrupt vector.

  In DOS versions 2.0 and later, the field at offset 2CH (2 bytes) contains
  the paragraph address of the program's environment block. The environment
  block contains a list of ASCIIZ strings (strings of ASCII characters, each
  terminated with a zero byte) that define various kinds of information. The
  end of the environment block is marked by a zero-length string (a single
  zero byte) where you would expect to find the first byte of the next
  string. Environment blocks that begin with a zero-length string contain no
  strings.

  Each environment string is of the form NAME = value, where NAME is
  capitalized and of any reasonable length and value can be almost anything.
  The environment thus consists of a list of global variables, each of which
  contains information that your program may be able to use. For example, if
  the environment block contains the PATH environment variable (that is, a
  string that starts with PATH=), any program--including DOS itself--can
  examine its environment block to determine which directories to search for
  executable files (and in what order). In this way the environment block
  provides a simple means of passing information to any program that
  examines it. (You can change the contents of the environment block with
  the DOS SET command.)

  DOS makes a copy of the environment block whenever it loads a program to
  be executed, and places the copy's paragraph address (segment) in the
  program's PSP. To obtain information from the environment block, a program
  must first obtain its segment from the PSP and then examine each of the
  zero-terminated strings. Some high-level languages contain functions that
  do this for you. For example, in C, the getenv() library function does all
  the work.

  Many sophisticated DOS programs rely on information in the environment
  block. Also, the concept of the environment is found in other powerful
  operating systems, including UNIX and OS/2. Whenever you need to pass
  user-configurable information to a program, we highly recommend the use of
  the environment block.

  The field at offset 50H contains two executable 8086 instructions: INT 21H
  and RETF (far return).

  This is another kludge that lets you invoke DOS functions somewhat
  indirectly. To use this feature, set up everything necessary to invoke a
  DOS interrupt 21H function (selecting the function in AH, and so forth).
  Then, instead of bravely performing an interrupt 21H (a 2-byte
  instruction), do a far call to offset 50H in the PSP (a 5-byte
  instruction).

  You might expect that this feature is another flash from the past, a bit
  of CP/M compatibility, but actually it was introduced with DOS version 2.0
  and will not work with previous versions of DOS. You might find that
  making a far call to offset 50H in the PSP is handy if you intend to patch
  the address of a different function dispatcher into your code, but in most
  cases, a simple INT 21H will suffice.

  The fields at offsets 5CH and 6CH support old-fashioned file processing,
  using file control blocks, or FCBs. FCBs can be used for file I/O with any
  version of DOS, but their use is discouraged with DOS versions 2.0 and
  later, where more modern file I/O is available through the use of file
  handles. See page 341 for more on file control blocks, and see page 350
  for more on file handles.

  This area of the PSP was designed to make life easier for programs that
  receive one or two filenames as parameters. The basic idea, and a good one
  we think, is to let DOS construct the necessary FCBs out of the first two
  command-line parameters (the parameters given on the command line,
  following the program name). If a program needs either or both FCBs, it
  can open and use them without having to decode the command-line parameters
  and construct the FCBs itself.

  If you use this feature of the PSP, you should be aware of three potential
  complications: First, the two FCBs overlap where they are placed. If your
  program needs only the first, fine; but if it needs the second FCB as
  well, one or both of them should be moved elsewhere before they are used.
  Second, these FCBs can involve FCB extensions, a fact overlooked in most
  DOS documentation for the PSP. Finally, if you use a DOS function that
  requires an extended FCB, you should be careful to copy the default FCBs
  to another area of memory where the FCB extensions won't overlap other
  data in the PSP.

  Keep in mind that the use of FCBs is considered obsolete, but if you want
  to use them, this information should help.

  The field at offset 80H serves two purposes: When DOS first builds the
  PSP, it fills this field with the command-line parameters typed by the
  user when the program was invoked. The length of the command line is in
  the byte at offset 80H. A string containing the command-line parameters
  follows at offset 81H.

  This string has some peculiarities: It does not contain the name of the
  program that was invoked. Instead, it begins with the character that
  immediately follows the program name, which is usually a blank.
  Separators, such as blanks or commas, are not stripped out or compressed.
  If you use the command line, you have to be prepared to scan through it,
  recognizing standard separators. Fortunately, high-level languages often
  provide functions that parse the command parameter string for you. In C,
  for example, the values argc and argv are passed to the main startup
  routine in every C program. These two values contain the number of
  command-line parameters and the address of a list of individual
  parameters. It's usually easier to rely on your high-level language to
  extract command-line parameters from the PSP than it is to do it yourself
  in assembly language.

  Starting with DOS version 2.0, the command line is modified in a
  particular way: DOS strips any redirection parameters (such as < or >) and
  reconstructs the parameter line as if these items were not there. As a
  result of these two operations on the command string, a program can
  neither find out if its standard I/O is being redirected nor find out its
  own name.

  The other purpose served by the field at offset 80H in the PSP is that of
  the default Disk Transfer Area. This default buffer area is established by
  DOS just in case you use a DOS service that calls for a DTA and haven't
  yet set up your own DTA buffer. See Chapters 16 and 17 for descriptions
  of the services that use or manipulate the DTA.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson