Search code examples
assemblydosx86-16dosbox

Dosbox shut down while create text file


Why after i create the file my dosbox will shut down automatically?? any problem of my code?

.model small
.stack 100h
.data
msg1    db      10, 13, 10, 13, "Please select an item:",0Dh,0Ah,0Dh,0Ah,09h
        db      "1- Create File",0Dh,0Ah,09h
        db      "2- show data",0Dh,0Ah,09h      
        db      "3- Exit",0Dh,0Ah,09h
        db      "Enter item number: " 
        db      '$'   
msg2    db      10,13,"Success Create$"
file    db      "test.txt"
handle  dw      ?
buffer  dw      10 dup (?)

.code
main proc 
    mov   ax,@data
    mov   ds,ax

ShowMenu:       
    lea     dx, msg1  
    mov     ah, 09h 
    int     21h     

    mov     ah, 01h ;get choice
    int     21h        

    cmp     al, "1"
    je      CreateFile
    cmp     al, "2"
    je      ShowData
    cmp     al, "3"
    jmp     Quit
    jl      ShowMenu

Quit: 
   mov   ah,4ch
   int   21h   

CreateFile:
mov ah,3ch ;create file
mov cx,0
lea dx,file;set file name
int 21h

lea dx,msg2
int 21h
jmp ShowMenu

ShowData:
mov ah,3dh ;open file
mov al,0   ;open as read only
lea dx,file
int 21h
mov handle,ax

;read one char
mov ah,3fh   ;service to read file
mov bx,handle
mov cx,1        ;how many byte to read
mov dx,buffer   ;where to store
int 21h

;close file
mov ah,3eh
mov bx,handle
int 21h

jmp ShowMenu


main endp
 end main

create a text file


Solution

  • The problem of not setting the function code 09h before displaying the success message (comment by David Wohlferd) would normally just close your program but not the DOSBox emulator.
    The handle that you get from creating a file would ordinarily leave AH=0 and that would subsequently invoke a DOS termination function.Terminate program


    file    db      "test.txt"
    handle  dw      ?
    

    A further problem in your program is that you forgot to zero-terminate the filespec:

     file    db      "test.txt", 0
    

    A garbage filespec could potentially make DOSBox shutdown!

    Because the handle variable is defined with ? (uninitialized memory) you could be unlucky that the following byte does not happen to be a zero.