0

I am trying to read a text file and print the contents in assembly. The file is read and the contents do print out. However, "read file" is printed on the next line after the file's contents. I wanted to know why the errfor is being printed if the code works just fine.

    .model tiny

    .data


  filename db "file.txt", 0
  bufferSize = 512
  filehandle dw ?
  buffer db ' $'
  message1 db 'Cannot open file $'
  message2 db 'Cannot read file $'
  message3 db 'Cannot close file $'


    .code
    org 100h

    start:

 call open
 call read
 call close
 call Exit

open:
  mov ah,3DH
  mov al,0
  mov dx, offset filename
  int 21h
  jc openErr
  mov filehandle, ax
  ret

 read:
mov ah, 3Fh
mov bx, filehandle
mov cx,bufferSize
mov dx, offset buffer
int 21h
cmp ax,0
    jc readErr

    ;changes are here

;displays content of file
    call clear
mov ah, 9
mov dx, offset buffer
    int 21h
    ret

;changes stop here
  close:
 mov ah, 3Eh
 mov bx, filehandle
 int 21h
 jc closeErr
 ret

  Exit: 
 mov ax, 4C00h
 int 21h

openErr:
      call newline
      lea  DX,message1     ;set up pointer to error message
      mov  AH,9          ;display string function
      int  21H           ;DOS call
      stc               ;set error flag
      ret

readErr:
      call newline
      lea  DX,message2     ;set up pointer to error message
      mov  AH,9          ;display string function
      int  21H           ;DOS call
      stc               ;set error flag
      ret

closeErr:
      call newline
      lea  DX,message3     ;set up pointer to error message
      mov  AH,9          ;display string function
      int  21H           ;DOS call
      stc               ;set error flag
      ret


newline:  ;prints a newline                           
            mov ah, 2                    
            mov dl, 0DH
            int 21h            
            mov dl, 0AH                 
            int 21h         
            ret

           clear: ;clears the screen

        mov ax,003h
        int 10h
        ret 



      end start
ajstc
  • 39
  • 1
  • 7
  • 1
    That's 16 bit DOS code, I am not sure it will like `Documents` which is not 8.3 format. Try using the short name. PS: you forgot to go to `Exit`. – Jester May 16 '17 at 14:00
  • Do you mean "file.txt"? I already tried that and I get the same error. Also, what do you mean I forgot to go to Exit? – ajstc May 16 '17 at 15:20
  • Move the file to the same directory where TASM is, then try to open it, this way you don't need the path, just the filename. – Jose Manuel Abarca Rodríguez May 16 '17 at 15:54
  • @JoseManuelAbarcaRodríguez Thank you! That worked perfctly! Now that I think about it, the solution was rather obvious. Now I have another question, is there any link you can give me to print out the contents of the file? – ajstc May 16 '17 at 17:52
  • http://stackoverflow.com/a/29213830/3298930 – Jose Manuel Abarca Rodríguez May 16 '17 at 17:56
  • @ajstc "short names" in windows world are the old DOS 8.3 file name aliases. You can see them in command line (`cmd.exe`) by entering `dir /x`, for example in one of my dirs the file `"build.gradle"` has DOS name `"BUILD~2.GRA"` ... the DOS name must be used with `int 21h` DOS services. – Ped7g May 17 '17 at 12:22

0 Answers0