6. The PSP

Back Home Up Next

The concept of the PSP in DOS derives from the way programs in CP/M were run.

In CP/M, a program was loaded into what was called the "transient program area." This was, usually, at an address starting at 0100h. Just prior to this starting address for the program, the CCP (console command processor, the logical equivalent to DOS's COMMAND.COM or CMD.EXE program) would set up some convenient information. This included a way to call quit the program and perform a "warm start;" a way to call CP/M's FDOS to access it's features and functions; a couple of pre-formated file control blocks (FCBs) where the command line was automatically analyzed for up to two file names and the names placed into these special places, for convenience; and the command line text, specified with a byte count and then the ASCII text of the command line (after the command, itself.) The exact locations of these in DOS's PSP are exactly the way they were in CP/M. In fact, even the name "FCB" is also used in DOS and the size of the simple FCB in DOS is the same, too. The count of characters in the command line and the text of the command line also are placed in the same positions.

Even though it's clear that DOS borrowed (some may say, plagerized) it from CP/M, what exactly is the PSP?

The DOS Program Segment Prefix

DOS builds a Program Segment Prefix (PSP) for every program it runs -- both .COM and .EXE programs. In .COM files, the PSP is located in the same segment with the code, starting at offset address 0000h and continuing for 256 bytes, up to and including offset address 00FFh. The .COM program code then starts at 0100h in the same memory segment. For .EXE programs, the PSP is in its own memory segment and DOS passes this segment address value in the DS and ES segment registers when it runs the program. Either way, though, every program is provided with a prepared PSP area.

For most programming, the only thing of real value in the PSP is the command line text used when the program was invoked. The rest of it quite often ignored. In newer DOS versions, the FCBs are deprecated (deplored), so the two pre-built FCBs aren't used much, these days. Similarly, it's not necessary to use the PSP to call DOS or to exit back to DOS. This pretty much leaves the command line as the more useful remaining part of the PSP.

The format of the PSP is shown below:

    pspInt20        dw        1 DUP(?)  ; INT 20h instruction.
    pspNextPara     dw        1 DUP(?)  ; segment addr of next paragraph.
                    db        1 DUP(?)  ; reserved.
    pspDispatcher   db        5 DUP(?)  ; long call to DOS.
    pspTermVector   dd        1 DUP(?)  ; Termination address (INT 22h).
    pspCtrlCVector  dd        1 DUP(?)  ; Control-C handler (INT 23h).
    pspCritVector   dd        1 DUP(?)  ; Critical error handler (INT 24h).
                    dw       11 DUP(?)  ; reserved.
    pspEnvironment  dw        1 DUP(?)  ; segment addr of environment.
                    dw       23 DUP(?)  ; reserved.
    pspFCB_1        db       16 DUP(?)  ; default FCB #1.
    pspFCB_2        db       16 DUP(?)  ; default FCB #2.
                    dd        1 DUP(?)  ; reserved.
    pspCmdTailCount db        1 DUP(?)  ; count of chars in command tail.
    pspCmdTailText  db      127 DUP(?)  ; text, starts 20h, ends 0Dh.

In DOS 1.0, the instruction, "INT 20h," was used to terminate a program. When DOS starts a .COM program, it pushes a word of 0 (16-bit value) onto the stack befure running it. In this way, if the .COM program were to execute a return (RET) instruction, this 0 would be popped and used as the code offset. This would take it immediately to the first entry in the PSP, where an INT 20h instruction lives and would then call DOS to terminate the program. One could just as well jump to that location, as well, and it would do the same thing. This is also how CP/M supported it's "warm start" method of exiting a program.

Just as CP/M had, at offset address 0005h, a way of calling its FDOS functions, so DOS also provides the same facility at the same offset address of the PSP. This is the pspDispatcher variable mentioned above. You can also see two FCBs and, as in CP/M, they are at the same offset address and have the same length as they did in CP/M. Finally, you can see the command tail count and the command tail text at the end of the PSP, in the last 128 bytes of it, in fact. This is also the same place that CP/M put it. No surprise. Note that this limits the length of a valid command line, both in DOS and in CP/M.

You do not need to declare any of these items in order to get a PSP from DOS. It builds one for every program, whether it wants one or not. But most .COM programs do not use any of the above parts of the PSP, except for the last two. The command line text remains useful for many DOS programs.

 

Last updated: Thursday, July 08, 2004 17:57