Program Segment Prefix
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:
Offset | Size | Contents |
---|---|---|
00h–01h | 2 bytes (code) | |
02h–03h | word (2 bytes)
|
Segment of the first byte beyond the memory allocated to the program |
04h | byte | Reserved |
05h–09h | 5 bytes (code) | |
0Ah–0Dh | dword (4 bytes)
|
Terminate address of previous program (old INT 22h) |
0Eh–11h | dword | Break address of previous program (old INT 23h) |
12h–15h | dword | Critical error address of previous program (old INT 24h) |
16h–17h | word | Parent's PSP segment (usually COMMAND.COM - internal) |
18h–2Bh | 20 bytes | Job File Table (JFT) (internal) |
2Ch–2Dh | word | Environment segment |
2Eh–31h | dword | SS:SP on entry to last INT 21h call (internal)
|
32h–33h | word | JFT size (internal) |
34h–37h | dword | Pointer to JFT (internal) |
38h–3Bh | dword | Pointer to previous PSP (only used by SHARE in DOS 3.3 and later) |
3Ch–3Fh | 4 bytes | Reserved |
40h–41h | word | DOS version to return (DOS 5 and later, alterable via SETVER in DOS 5 and later) |
42h–4Fh | 14 bytes | Reserved |
50h–52h | 3 bytes (code) | Unix-like far call entry into DOS (always contains INT 21h + RETF) |
53h–54h | 2 bytes | Reserved |
55h–5Bh | 7 bytes | Reserved (can be used to make first FCB into an extended FCB) |
5Ch–6Bh | 16 bytes | Unopened Standard FCB 1
|
6Ch–7Fh | 20 bytes | Unopened Standard FCB 2 (overwritten if FCB 1 is opened) |
80h | 1 byte | Number of bytes on command-line |
81h–FFh | 127 bytes | Command-line tail (terminated by a 0Dh)[4][5] |
The PSP is most often used to get the
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
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.[6]
Alternatively, in
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:
org 100h ; .COM - not using ds
; INT 21h subfunction 9 requires '$' to terminate string
xor bx,bx
mov bl,[80h]
cmp bl,7Eh
ja exit ; preventing overflow
mov byte [bx+81h],'$'
; print the string
mov ah,9
mov dx,81h
int 21h
exit:
mov ax,4C00h ; subfunction 4C
int 21h
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.
; save
push ds
xor ax,ax
push ax
; move to the default data group (@data)
mov ax,@data
mov ds,ax
; print message in mess1 (21h subfunction 9)
mov dx,mess1
mov ah,9
int 21h
retf
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,
jmp start
mess1 db 'Hello world!$'
start:
mov dx,mess1
mov ah,9
int 21h
int 20h
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
- Zero page (CP/M)
- CALL 5 (DOS)
- Stack frame (Unix)[7]
- Process directory (Multics)[7]
- Process identifier (PID)[7]
- this (computer programming)
- Self-reference
References
- ^ (13 pages)
- ^ Paul, Matthias R. (2002-10-07) [2000]. "Re: Run a COM file". Newsgroup: alt.msdos.programmer. Archived from the original on 2017-09-03. Retrieved 2017-09-03. [15] (NB. Has details on DOS COM program calling conventions.)
- DOS=HIGH. In that case, DOS makes sure that linear address 1000C0h contains the appropriate far call. […]
- ^ Paul, Matthias R. (1997-07-01) [1994-01-01]. MSDOSTIPs — Tips für den Umgang mit MS-DOS 5.0-7 (in German). Archived from the original on 2017-08-22. Retrieved 2013-10-25.
{{cite book}}
:|work=
ignored (help) (NB. MSDOSTIP.TXT is part of MPDOSTIP.ZIP, maintained up to 2001 and distributed on many sites at the time. The provided link points to a HTML-converted older version of the MSDOSTIP.TXT file.) [16] - ^ Paul, Matthias R. (1997-05-01) [1995-03-01]. "Hinweise zu JPSofts 4DOS 5.5b/c, 5.51, 5.52a und NDOS". MPDOSTIP (in German). Archived from the original on 2016-11-04. Retrieved 2015-05-08. (NB. The provided link points to a HTML-converted version of the
4DOS5TIP.TXT
file, which is part of theMPDOSTIP.ZIP
collection.) [17] - ^ "INT 21h,62h - Get PSP address (DOS 3.x)". Archived from the original on 2012-02-07.
- ^
Further reading
- 86-DOS - Disk Operating System for the 8086 - Programmer's Manual (PDF). Version 0.3 (Preliminary ed.). Seattle, Washington, USA: Seattle Computer Products, Inc. 1980. Archived from the original(PDF) on 2019-06-23. Retrieved 2011-09-13. (41 pages)
- "Format of Program Segment Prefix (PSP)". INTER61. 2000. Archivedfrom the original on 2020-02-17. Retrieved 2019-12-19.
- 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.). ISBN 3-89319-350-2. (1123+v pages, foldout, 5.25"-floppy)
External links
- Accessing Command Line Arguments (Microsoft.com)