What exactly is PSP (program segment prefix) ?
I searched in Google but couldn't find any clean and straight answers to help me completely understand it.
What does it do and what is the use of it?
CodePudding user response:
When you want to run a program in DOS, its command processor command.com
reserves a block from remaining free memory and fills a 256 bytes long structure called PSP in the beginning of this block. Image of the executable program is loaded from disk to memory right behind the PSP. Segment address of PSP is loaded into segment registers DS
and ES
and the program is launched.
If it is a COM program, CS=DS=ES=SS
and instruction pointer IP
is set right after the PSP, i.e. IP=256
.
When the program is MZ executable, CS:IP
and SS:SP
are set from their corresponding fields in MZ header.
Program Segment Prefix is useful for the program in many ways:
- The launched program can inspect command-line arguments located at
offset
80h
in PSP. - Segment address of block of strings with environment
variables can be found at
PSP:2Ch
. - Program may hook and change
the default system reaction to Ctrl-Break at
PSP:0Eh
or reaction to critical error atPSP:12h
. - At
PSP:16h
it can get PSP address of its parent process to find out whether it was launched fromcommand.com
or spawned from other executable program. - And last
but not least the return instruction
INT 20h
atPSP:0
can be used in COM program to terminate itself by a single instructionRET
.
More detailed description of PSP can be found in Wikipedia.