Program Segment Prefix: Difference between revisions

Content deleted Content added
Fixed link
Added note on CP/M Zero page and long command lines
Line 1:
The '''Program Segment Prefix''' (PSP) is a data structure used in [[DOS]] systems to store the state of a [[computer program|program]]. It resembles the [[Zero page (CP/M)|Zero Page]] in the [[CP/M]] operating system. The PSP has the following structure:
It has the following structure:
 
{| border="1" width="100%" class="wikitable"
Line 7 ⟶ 6:
! Contents
|-
| 0000h-0101h
| 2 bytes (code)
| [[CP/M]] exit (always contain INT 2020h)
|-
| 0202h-0303h
| [[Word (data type)|word]] (2 bytes)
| Memory size in paragraphs
|-
| 0404h
| byte
| Reserved
|-
| 0505h-0909h
| 5 bytes (code)
| Far call to CP/M compatibility code within DOS
|-
| 0A0Ah-0D0Dh
| [[dword]] (4 bytes)
| Terminate address of previous program (old INT 2222h)
|-
| 0E0Eh-1111h
| dword
| Break address of previous program (old INT 2323h)
|-
| 1212h-1515h
| dword
| Critical error address of previous program (old INT 2424h)
|-
| 1616h-1717h
| word
| Caller's PSP segment (usually [[command.com]] - internal)
|-
| 1818h-2B2Bh
| 20 bytes
| [[Job File Table]] (internal)
|-
| 2C2Ch-2D2Dh
| word
| [[Environment variable|Environment]] segment
|-
| 2E2Eh-3131h
| dword
| SS:SP on entry to last INT 2121h call (Internal)
|-
| 3232h-3333h
| word
| Max open files (Internal - see below)
|-
| 3434h-3737h
| dword
| Handle-entries address (Internal - see below)
|-
| 3838h-4F4Fh
| 24 bytes
| Reserved
|-
| 5050h-5252h
| 3 bytes (code)
| Far call to DOS (always contain INT 2121h + RETF)
|-
| 5353h-5B5Bh
| 9 bytes
| Reserved
|-
| 5C5Ch-6B6Bh
| 16 bytes
| Unopened Standard [[File control block|FCB]] 1
|-
| 6C6Ch-7F7Fh
| 20 bytes
| Unopened Standard FCB 2 (overwritten if FCB 1 is opened)
|-
| 8080h
| 1 byte
| Number of bytes on command-line
|-
| 8181h-FFFFh
| 127 bytes
| Command-line (terminated by a [[Carriage return|0Dh]])
Line 90 ⟶ 89:
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'.
 
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 [[interrupt]] 2121h subfunction 6262h. This interrupt will return the PSP address in register BX.<ref>{{cite web|url=http://www.htl-steyr.ac.at/~morg/pcinfo/hardware/interrupts/inte8fjk.htm|title=INT 2121h,6262h - Get PSP address (DOS 3.x)}}</ref>
 
Alternatively, in [[.COM]] programs, one can address the PSP directly just by using the offsets listed above. 00h points to the beginning of the PSP, FFh points to the end, etc. For example, the following code displays the command line arguments:
 
<!-- This may serve as an example for illustration purposes, but it would be a bad idea to actually code it this way. It would be very easy to crash the system by just giving a command line longer than 126 bytes. -->
<pre><nowiki>
org 100h
Line 111:
int 21h
</nowiki></pre>
 
If the command line length is non-zero, programs should first try to retrieve the command line from the appendage of the environment, if the segment pointer to the environment is neither 0000h or FFFFh. This way, it is possible to pass longer command lines than 127 bytes to applications than via the fixed length command line buffer in the PSP. If this appendage is not available (for example under DOS prior to 3.2) or if it turns out to be of length zero, the command line can be retrieved from the PSP itself, but possibly truncated after 127 bytes. Indicated lengths beyond this should not be trusted on.
 
== See also ==
*[[Zero page (CP/M)|Zero Page]]
 
== References ==