Reputation: 1466
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?
Upvotes: 2
Views: 18442
Reputation: 14319
If you want to exit without any particular error code this is the way to go:
mov ax,4C00h
int 21h
But if you want to return a particular error code, this is a better way:
; ...
ErrorCode db 0 ; set a default error code
; ...
mov ErrorCode,1h ; change the error code if needed
; ...
mov ah,4Ch ; prepare to exit
mov al,ErrorCode ; and return the error code
int 21h
Upvotes: 0
Reputation: 37262
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
Upvotes: 5
Reputation: 302
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
Upvotes: 7