ID Number: Q72848
5.00 5.10 6.00
MS-DOS
Summary:
When terminating a program executed as an .EXE file, it is recommended
that function 4Ch of interrupt 21h be used, rather than interrupt 20h.
Unfortunately, function 4Ch is not available on versions of MS-DOS
earlier than 2.0. If the use of Int 20h is required, then it is
important to be sure that the CS register points to the beginning of
the PSP (program segment prefix).
More Information:
Int 20h is often used to terminate .COM programs. The problem with
using the same interrupt to end .EXE programs is that Int 20h requires
that CS point to the PSP, and while this is true for .COM programs, it
does not hold for .EXE programs.
Simply setting CS equal to the PSP will change the flow of execution,
making it unlikely that the Int 20h call will be reached; therefore, a
more indirect method must be used. First, push the segment of the PSP,
then push an offset of 0000. Finally, issue a "retf" instruction. This
causes program execution to switch to offset 0000 of the PSP, which
contains an Int 20h instruction.
This method is demonstrated in the sample code below, which is written
for MASM versions 5.x but will work with MASM version 6.0 as well.
Sample Code
-----------
; Assemble options needed: none
stack SEGMENT PARA STACK 'STACK'
db 2048 dup(?)
stack ENDS
data SEGMENT WORD PUBLIC 'WORD'
msg db "Hello, World", 0Dh, 0Ah, "$"
data ENDS
text SEGMENT WORD PUBLIC 'CODE'
begin: push es ;ES = PSP at entry, so we'll save it
mov ax, SEG data ;Initialize DS to data segment
mov ds, ax
ASSUME DS:data, CS:text, SS:stack
mov ax, SEG msg
mov ds, ax ;Set DS:DX to the address of msg
mov dx, OFFSET msg
mov ah, 09h ;Function 09h (Display String)
int 21h
mov ax, 00h ;Extra step for 8088/8086 chips
push ax ;PSP segment is already on the stack
retf
text ENDS
END begin