Microsoft KB Archive/72848

= How to Use Int 20h to Terminate an .EXE Program =

Article ID: 72848

Article Last Modified on 11/15/2003

-

APPLIES TO


 * Microsoft Macro Assembler 5.0
 * Microsoft Macro Assembler 5.1 Standard Edition
 * Microsoft Macro Assembler 5.1a
 * Microsoft Macro Assembler 6.0 Standard Edition
 * Microsoft Macro Assembler 6.0a
 * Microsoft Macro Assembler 6.0b

-



This article was previously published under Q72848



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 &quot;retf&quot; 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 version 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 'data'

msg    DB &quot;Hello, World&quot;, 0Dh, 0Ah, &quot;$&quot;

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

Additional query words: kbinf 5.10 5.10a 6.00 6.00a 6.00b

Keywords: KB72848

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.