The Program Segment Prefix (PSP) is a data structure used in DOS systems to store the state of a program. It resembles the Zero Page in the CP/M operating system. The PSP has the following structure:

OffsetSizeContents
00h–01h2 bytes (code)CP/M-80-like exit (always contains INT 20h)
02h–03hword (2 bytes)Segment of the first byte beyond the memory allocated to the program
04hbyteReserved
05h–09h5 bytes (code)CP/M-80-like far call entry into DOS, and program segment size
0Ah–0Dhdword (4 bytes)Terminate address of previous program (old INT 22h)
0Eh–11hdwordBreak address of previous program (old INT 23h)
12h–15hdwordCritical error address of previous program (old INT 24h)
16h–17hwordParent's PSP segment (usually COMMAND.COM - internal)
18h–2Bh20 bytesJob File Table (JFT) (internal)
2Ch–2DhwordEnvironment segment
2Eh–31hdwordSS:SP on entry to last INT 21h call (internal)
32h–33hwordJFT size (internal)
34h–37hdwordPointer to JFT (internal)
38h–3BhdwordPointer to previous PSP (only used by SHARE in DOS 3.3 and later)
3Ch–3Fh4 bytesReserved
40h–41hwordDOS version to return (DOS 5 and later, alterable via SETVER in DOS 5 and later)
42h–4Fh14 bytesReserved
50h–52h3 bytes (code)Unix-like far call entry into DOS (always contains INT 21h + RETF)
53h–54h2 bytesReserved
55h–5Bh7 bytesReserved (can be used to make first FCB into an extended FCB)
5Ch–6Bh16 bytesUnopened Standard FCB 1
6Ch–7Fh20 bytesUnopened Standard FCB 2 (overwritten if FCB 1 is opened)
80h1 byteNumber of bytes on command-line
81h–FFh127 bytesCommand-line tail (terminated by a 0Dh)

The PSP is most often used to get the command line arguments of a DOS program; for example, the command "FOO.EXE /A /F" executes FOO.EXE with the arguments '/A' and '/F'.

If the PSP entry for the command line length is non-zero and the pointer to the environment segment is neither 0000h nor FFFFh, programs should first try to retrieve the command line from the environment variable %CMDLINE% before extracting it from the PSP. This way, it is possible to pass command lines longer than 126 characters to applications.

The segment address of the PSP is passed in the DS register when the program is executed. It can also be determined later by using Int 21h function 51h or Int 21h function 62h. Either function will return the PSP address in register BX.

Alternatively, in .COM programs loaded at offset 100h, one can address the PSP directly just by using the offsets listed above. Offset 000h points to the beginning of the PSP, 0FFh points to the end, etc.

For example, the following code displays the command line arguments:

In DOS 1.x, it was necessary for the CS (Code Segment) register to contain the same segment as the PSP at program termination, thus standard programming practice involved saving the DS register (since the DS register is loaded with the PSP segment) along with a zero word to the stack at program start and terminating the program with a RETF instruction, which would pop the saved segment value off the stack and jump to address 0 of the PSP, which contained an INT 20h instruction.

If the executable was a .COM file, this procedure was unnecessary and the program could be terminated merely with a direct INT 20h instruction or else calling INT 21h function 0. However, the programmer still had to ensure that the CS register contained the segment address of the PSP at program termination. Thus,

In DOS 2.x and higher, program termination was accomplished instead with INT 21h function 4Ch which did not require the CS register to contain the segment value of the PSP.

See also

Further reading

  • (PDF). Version 0.3 (Preliminary ed.). Seattle, Washington, USA: Seattle Computer Products, Inc. 1980. Archived from (PDF) on 2019-06-23. (41 pages)
  • . INTER61. 2000. from the original on 2020-02-17.
  • Schäpers, Arne (1991). "Kapitel 5: EXEC im Detail - Program Segment Prefix (PSP)". DOS 5 für Programmierer: Die endgültige Referenz (in German) (1 ed.). Addison Wesley (Deutschland) GmbH. pp. 148–151, 971–972. ISBN 3-89319-350-2. (1123+v pages, foldout, 5.25"-floppy)

External links

  • (Microsoft.com)