The Program Segment Prefix (PSP) is the 256 bytes DOS builds in front of every program
it runs, COM or EXE. It is not in the file. In a COM program it is the first 256 bytes of
the program’s own segment, so the program can read it at offsets 00h to
FFh, and the file’s first byte is at 100h. An
MZ program has one too, in the paragraphs just before
its load module, with DS and ES pointing at it when it starts.
The fields
| Offset | Size | Contents |
|---|---|---|
00h | 2 | INT 20h (CD 20): the exit call, where a RET with the starting stack lands. Programs that walk memory use these two bytes as the sign of a PSP. |
02h | 2 | The segment of the first byte past the memory given to the program |
04h | 1 | Unused by DOS (OS/2 keeps a count here for its fake DOS version) |
05h | 1 | A far call opcode: the start of the entry into DOS that CP/M programs call at 0005h (below) |
06h | 2 | The offset of that far call, which is also the size of the program’s first segment for CP/M’s sake: this is where a CP/M program reads the top of its memory (its value) |
08h | 2 | The rest of the far call, its segment |
0Ah | 4 | The terminate address (INT 22h vector) to restore when the program ends |
0Eh | 4 | The Ctrl-Break address (INT 23h vector) to restore |
12h | 4 | The critical error address (INT 24h vector) to restore, from DOS 1.1 |
16h | 2 | The parent’s PSP segment, usually COMMAND.COM’s (internal) |
18h | 20 | The job file table: the program’s file handles, one byte each, FFh when unused, from DOS 2 (internal). Values 80h to FEh mark remote files under network redirectors based on MS-Net and under Novell NetWare, and D0h to FEh device drivers in an OS/2 DOS box. |
2Ch | 2 | The segment of the program’s environment, from DOS 2 |
2Eh | 4 | SS:SP on entry to the last INT 21h call, from DOS 2 (internal) |
32h | 2 | The number of entries in the job file table, normally 20, from DOS 3 (internal) |
34h | 4 | A far pointer to the job file table, normally PSP:0018h, which a program can move to have more than 20 handles, from DOS 3 (internal) |
38h | 4 | A pointer to the previous PSP, from DOS 3, FFFFFFFFh by default in 3.x; used by SHARE in DOS 3.3 |
3Ch | 1 | The double-byte (DBCS) interim console flag, from DOS 4 (internal) |
3Dh | 1 | The APPEND program’s TrueName flag (internal) |
3Eh | 1 | Novell NetWare’s flag |
3Fh | 1 | Novell NetWare’s task number |
40h | 2 | The DOS version to report to the program, from DOS 5 (SETVER changes it) |
42h | 2 | Windows 3: the selector of the next PSP in Windows’ list |
44h | 2 | Windows 3: PDB_Partition |
46h | 2 | Windows 3: PDB_NextPDB |
48h | 1 | Windows 3: bit 0 is set for a non-Windows application (WINOLDAP) |
49h | 3 | Unused by DOS up to 6.00 |
4Ch | 2 | Windows 3: PDB_EntryStack |
4Eh | 2 | Unused by DOS up to 6.00 |
50h | 3 | INT 21h then RETF (CD 21 CB): a far call here does a DOS call, from DOS 2 |
53h | 2 | Unused by DOS up to 6.00 |
55h | 7 | Unused by DOS up to 6.00; room to turn the first FCB into an extended FCB |
5Ch | 16 | The first file control block, filled from the first argument; it runs into the second if it is opened |
6Ch | 16 | The second file control block, filled from the second argument; opening it would run into the command tail |
7Ch | 4 | Unused |
80h | 1 | The length of the command tail |
81h | 127 | The command tail, ending with 0Dh |
The fields marked internal belong to DOS. Programs read them, but their meaning is not
guaranteed between versions. Microsoft’s own table leaves 3Ch to
3Fh and 42h to 4Fh as reserved; the fields named
there, from DOS 4, APPEND, NetWare and Windows, are documented in Ralf Brown’s Interrupt
List.
The same list records some ways the fields go wrong. Some DOS extenders put
protected-mode values in fields such as the parent’s PSP, so a program that walks the
PSPs should check for the CD 20 at the start, or that the PSP begins a
memory block that owns itself. MS-DOS 5.00 fills the FCBs wrongly when it loads a
program high: the first is empty and the second holds the first argument.
The CP/M call at 05h
Bytes 05h to 09h are a far call: 9Ah, then an
offset and a segment. A CP/M program calls 0005h to reach the system, and
reads the word at 0006h as the top of the memory it may use. DOS sets the
two words so that the call lands at the same place, absolute address
000C0h, whatever the PSP’s segment, while the offset says how much of the
segment the program has. Ralf Brown’s Interrupt List notes that PSPs made by DEBUG,
from DOS 2, point at 000BEh instead.
| Version | Offset at 06h | Segment at 08h |
|---|---|---|
| MS-DOS 1.25 | When the PSP is made: the paragraphs of memory from the PSP to the end, at most 0FFFh, times 16. COMMAND.COM then sets it to (its memory size − 100h) AND FFF0h for a COM program, so FEF0h with 64 KB or more | 000Ch minus the paragraphs; COMMAND.COM moves it by the same amount as the offset, so the call still reaches 000C0h |
| MS-DOS 2.0 | The paragraphs from the PSP to the end of its block, at most 0FFFh, times 16: at most FFF0h | 000Ch minus those paragraphs, so that the call wraps round to 0000:00C0 |
| MS-DOS 4.0 | The same less 10h paragraphs, which the source’s comment says allow for a 100h-byte stack in COM files: at most FEF0h | 000Ch minus that, again reaching 0000:00C0 |
| MS-DOS 3.x, 5 and 6 | Not verified: the source is not published | |
With 64 KB or more in MS-DOS 2.0, 000Ch − 0FFFh wraps round
to segment F00Dh, and F00D:FFF0 is address
1000C0h, 1 MB past 000C0h (in 4.0, F01D:FEF0
gives the same). The call reaches 000C0h only because the address wraps
round at 1 MB, as it does on an 8086.
The command line
Offset 80h holds the number of characters typed after the program’s
name, and 81h on holds them, starting with the space that separated them
from the name and ending with a carriage return (0Dh) that the count leaves
out. Redirections such as > file have been taken out by the command
shell. 127 bytes is the most there is room for.
Offsets 80h to FFh are also the default disk transfer area,
the buffer that the FCB services and the find-file services (4Eh,
4Fh) write into until the program sets another with service
1Ah. A program that wants its command line must read it before it uses any
of those.
Some later shells pass longer command lines through the environment in a
CMDLINE variable; a program that finds the count non-zero and an
environment can look there first. For a line too long for the tail, 4DOS and
Windows 95 set the length byte to 7Fh, put the first 126 characters in
the tail and a 0Dh at FFh, and pass the full line in
CMDLINE; some versions of 4DOS leave out that 0Dh. Ralf
Brown’s Interrupt List also records that a program started from the Borland Pascal
7.00 IDE with an overlong line gets a length byte of 80h and no
0Dh at all, so a careful program puts a 0Dh at
FFh itself before scanning the tail.
The file control blocks
File control blocks are CP/M’s way of naming a file, and DOS 1 used them for all file
access. DOS parses the first two arguments of the command line as file names and puts
them, unopened, in the two FCBs at 5Ch and 6Ch: a drive
number, then an eight-character name and a three-character extension, padded with
spaces. Programs from DOS 2 on use handles instead, but the parsed names are still
there, and the drive check behind AL and
AH at the start comes from them.
Each default block in the PSP has only the 16 bytes that hold a file name, so the two overlap the moment one is opened, and opening the second would overwrite the command tail. Either way, a program copies a block to somewhere with more room before opening it. An open block has this layout:
| Offset | Size | Contents |
|---|---|---|
00h | 1 | Drive: 0 is the default, 1 is A:, and so on; DOS replaces 0 with the real drive when it opens the file |
01h | 8 | File name, padded with spaces |
09h | 3 | Extension, padded with spaces |
0Ch | 2 | Current block number |
0Eh | 2 | Logical record size, 128 by default |
10h | 4 | File size |
14h | 2 | Date of the last change |
16h | 2 | Time of the last change |
18h | 8 | Reserved for DOS |
20h | 1 | Current record within the block |
21h | 4 | Random record number |
An extended FCB puts seven bytes in front: FFh, five reserved
bytes, and an attribute byte, then the block above. The seven bytes at
55h, just before the default FCB, are there so that it can be turned into
one.
The environment
The word at 2Ch is the segment of a copy of the environment: a run of
NAME=value strings, each ending with a zero byte, and one more zero byte
at the end. From DOS 3, a word count of 1 and the full path of the program follow it,
which is how a program finds the file it was run from.
Finding the PSP
A COM program’s PSP is at offset 0 of its own segment, so it never needs to look for
it. An EXE program finds it in DS and ES at the start, and any program can ask for it
later with INT 21h service 51h or 62h, which return its
segment in BX.
CP/M’s page zero
The first 256 bytes follow CP/M’s layout, so that programs translated from CP/M
found things where they expected: CP/M has a jump to its warm start at
0000h where DOS has INT 20h, a jump into the system at
0005h where DOS has its far call, the default FCB at 005Ch,
and the command line and default buffer at 0080h. See
From CP/M.