<--- Turn the page     (contents page)     Turn the page --->


The Program Segment Prefix




The Program Segment Prefix or PSP is what DOS uses to save information about a program just before it loads and runs it.

When DOS loads a program in to memory, it needs a place to store a copy of the environment, the command line, a few interrupt vectors, as well as a few other items. To do this, DOS creates a PSP of 256 bytes at the first position in the memory allocated for the program and then places the program just after these 256 bytes.

The layout of this PSP is as follows:

Offset Size (bytes) Description
00h 002 INT 20h instruction
02h 002 Address of paragraph following this program
04h 001   reserved
05h 005 Call to MS-DOS Function Dispatcher
0Ah 004 Interrupt 22h (termination) address
0Eh 004 Interrupt 23h (Ctrl-C) address
12h 004 Interrupt 24h (Critical Error) address
16h 002 Segment of Parent Program †
18h 020 File Handle Array †
2Ch 002 Segment address of environment block
2Eh 004 SS:SP on entry to last INT 21h Function †
32h 002 Handle array Size ‡
34h 004 Handle array pointer ‡
38h 004 Pointer to previous PSP
3Ch 004   reserved
40h 002 DOS version to return £
42h 014   reserved
50h 003 INT 21h, RETF instructions
53h 009   reserved
5Ch 016 FCB #1
6Ch 020 FCB #1
80h 128 Command line and DTA

† = Undocumented 2.x+
‡ = Undocumented 3.x+
£ = Undocumented 5.x+


The word at offset 00h is so that if you have a .com file, you can use the RET instruction to exit the program.

The word at offset 02h is the address of the next free paragraph following the program. This usually points to 0A000h because DOS allocates all available memory for the program.

The 3 words at offset 0Ah are used to store the current 22h, 23h, and 24h interrupts so that when your program is done, DOS can set these interrupts back to their original locations (vectors).

At offset 18h there is 20 bytes for 20 file handles. If a handle is 0FFh, then it is free (closed).

The word at offset 2Ch is the address of the copy of the environment that DOS allocated for this program.

The word at offset 32h is the size of the file handle array.

The dword at offset 34h is the address to this file handle array.

The 36 bytes at offset 5Ch are the two FCB's (File Control Blocks). DOS 1.0 used FCB's while later versions used Handles.

The 128 bytes at offset 80h is filled with the command line of the program loaded and then if needed, used at the DTA (Disk Transfer Area). If you use a DOS function that uses this DTA and you need the command line, you will need to either change the DTA address or save the command line somewhere else.

Offset 80h holds the byte count of the command line length not including the CR (13d), while the byte at offset 81h is usually a space if a command line is used. ¥




<--- Turn the page     (contents page)     Turn the page --->

Page 4