How to end assembly correctly?

13,004

Solution 1

The most correct way to end a DOS program is to use the "terminate" DOS function; followed by adequate comments so that people understand that this function won't return.

For example:

pleaseKillMeNow:
    mov ah,0x4C          ;DOS "terminate" function
    int 0x21

Solution 2

Brendan's answer shows how to exit but it leaves the error level undefined (it will be whatever is in the AL register...)

If you want to exit with error level 0:

mov ax,0x4c00
int 0x21

If you want to exit with error level 1:

mov ax,0x4c01
int 0x21
Share:
13,004
Roman
Author by

Roman

Updated on June 14, 2022

Comments

  • Roman
    Roman almost 2 years

    I have a problem correctly terminating a 16bit DOS program written in Assembly. Here's part of code:

    .386P
    .model flat
    
    stack_s segment stack 'stack' 
            db 256 dup(0)
    stack_s ends
    
    data segment use16
    data ends
    
    code segment 'code' use16
    assume cs:code, ds:data
    
    main proc
        mov ax, data
        mov ds, ax
    
        iretd
    main endp
    
    code ends
    end main
    

    The problem is, that the program doesn't terminate in a correct way. DOSBox just freezes. I tried to understand what happens using debugger, and it seems the program just ends up in an infinite loop after iretd is performed. Why does this happen? How can do I terminate a 16bit DOS app correctly?