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