title: How to open a txt file [EMU8086/Assembly]
body: <p>I've written a simple program, in which one I press </p>
  
  <ol>
  <li>the program makes a text file</li>
  <li>it shows a sentence about the program</li>
  <li>the program gets finished.</li>
  </ol>
  
  <p>These three buttons work perfectly fine, now I what I want is this: when I press "4" the program reads that text file (that I made before by pressing "1"). Now I've also written the code that opens the text file and it works normal as an stand alone. Here it is:</p>
  
  <pre><code>org 100h ; .com memory layout
  mov dx, offset file ; address of file to dx
  mov al,0 ; open file (read-only)
  mov ah,3dh
  int 21h ; call the interupt
  jc terminate ; if error occurs, terminate program
  mov bx,ax ; put handler to file in bx
  mov cx,1 ; read one character at a time
  print:
  lea dx, BUF
  mov ah,3fh ; read from the opened file (its handler in bx)
  int 21h
  CMP AX, 0 ; how many bytes transfered?
  JZ terminate 
  mov al, BUF 
  mov ah,0eh ; print character (teletype).
  int 10h
  jmp print ; repeat if not end of file.
  terminate:
  mov ah, 0 ; wait for any key...
  int 16h
  ret
  file db "c:\finaltest.txt", 0
  BUF db ?
  END
  </code></pre>
  
  <p>But when I import these codes into my main program, it can't open the file. Here's the full program, I want the program to read that text file when I press "4", but instead the program just gets reloaded:</p>
  
  <pre><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- About",0Dh,0Ah,09h      
          db      "3- Exit",0Dh,0Ah,09h     
          db      "4- Open File",0dh,0ah,09h
          db      "Enter item number: " 
          db      '$'   
  
  About   db      10, 13, 10, 13, "Blank Text About the Program$"
  
  handle  dw  ?
  file1   db  "c:\finaltest.txt", 0
  text    db  "Contains Message",0
  text_size equ $ - text
  
  .code
  main proc 
      mov   ax,@data
      mov   ds,ax
  
  ShowMenu:       
      lea     dx, msg1  
      mov     ah, 09h 
      int     21h     
  
  getnum:        
      mov     ah, 1 
      int     21h        
  
      cmp     al, '1' 
      jl      ShowMenu   
      cmp     al, '3'
      jg      ShowMenu 
  
      cmp     al, "1"
      je      CreateFile
      cmp     al, "2"
      je      ShowAbout
      cmp     al, "3"
      jmp     Quit          
      cmp     al, "4"
      jmp     OpenFile
  
  
  Quit: 
     mov   ah,4ch
     int   21h   
  
  Showabout:       
      lea     dx, About  
      mov     ah, 09h 
      int     21h    
      jmp     ShowMenu
  
  CreateFile:
  jmp new
  text_size = $ - offset text
  new:
  mov ah, 3ch
  mov dx, offset file1
  int 21h
  mov handle, ax
  mov ah, 40h
  mov bx, handle
  mov dx, offset text
  mov cx, text_size
  int 21h
  int 21h           
  ret
  
  OpenFile:    
  jmp print
  mov dx, offset file
  mov al,0 
  mov ah,3dh
  int 21h
  jc terminate 
  mov bx,ax 
  mov cx,1 
  print:
  lea dx, BUF
  mov ah,3fh
  int 21h
  CMP AX, 0
  JZ terminate 
  mov al, BUF 
  mov ah,0eh 
  int 10h
  jmp print 
  terminate:
  mov ah, 0 
  int 16h
  ret
  file db "c:\finaltest.txt", 0
  BUF db ?
  END            
  
      jmp     ShowMenu                
  main endp
  end main
  </code></pre>