Question

I cannot understand why I cannot perform comparison anymore at Continue label after putting the reading file module (from Open_File until Close_Error). If I remove the reading file module, it can work.

No error while assembling and linking. No error message while running the program. The program does not crash when running. Infinite loop if I put the reading file module. Display 3 testing if reading file module has been removed

What register actually affects this? Or which flag? How can I solve it?

File: https://www.dropbox.com/s/uwulv7du5k7xnb3/1.dat

.model small
.stack 20240
.data
    fname db '1.dat', 0
    handle dw ?
    fileLength dw 0
    openErrorMsg db "File open failed$"
    closeErrorMsg db "File close failed$"
    readErrorMsg db "File read failed$"
    buffer db ?

    tryCount db 4
    try db 0
    msg db "testing$"
.code
main proc
mov ax, @data
mov ds, ax          ; initialize DS

;----------------------------Open file----------------------------
Open_File:
mov ah, 09h
lea dx, openErrorMsg
int 21h

mov ah, 3dh         ; open file function
mov al, 0           ; open for reading  
lea dx, fname       ; copy address to DX
int 21h             ; open the file
mov handle, ax      ; handle or err code

jc Open_Error       ; jump if error
jnc Read_File

Open_Error:
    mov ah, 09h
    lea dx, openErrorMsg
    int 21h

    ;--------------------------newline
    mov ah, 02h
    mov dl, 0ah
    int 21h

    jmp Exit

;----------------------------Read file----------------------------
Read_File:
mov ah, 3fh         ; read file 
mov bx, handle      ; copy handle into BX
mov cx, ax          ; read until EOF
lea dx, buffer
int 21h             ; AX = bytes read

lea bx, buffer              ; get the length of buffer
mov si, ax                  
mov byte ptr [bx+si], '$'   ; put terminator at the end of buffer

mov fileLength, bx
add fileLength, si
sub fileLength, 46h


jc Read_Error       ; jump if error
jnc Display

Read_Error:
    mov ah, 09h
    lea dx, readErrorMsg
    int 21h

    ;--------------------------newline
    mov ah, 02h
    mov dl, 0ah
    int 21h

;----------------------------Display----------------------------
Display:
mov si, 0
Decrypt:
    sub buffer[si], 20h
    xor  buffer[si], 11000011b
    inc si
    cmp si, fileLength
jne Decrypt
je Continue_Display

Continue_Display:
mov ah, 09h
lea dx, buffer
int 21h

;--------------------------newline
mov ah, 02h
mov dl, 0ah
int 21h

;----------------------------Close file----------------------------
mov ah, 3eh         ; close file function
mov bx, handle      ; copy handle to bx
int 21h             ; execute closing
jc Close_Error      ; jump if error
jnc Continue

Close_Error:
mov ah, 09h
lea dx, closeErrorMsg
int 21h

;--------------------------newline
mov ah, 02h
mov dl, 0ah
int 21h

;-------------------------------------------------------------

Continue:
inc try
mov al, try
cmp al, tryCount
jb Go
je Exit

Go:
mov ah, 09h
lea dx, msg
int 21h
jmp Continue


Exit:
;--------------------------newline
mov ah, 02h
mov dl, 0ah
int 21h

mov ah, 4ch
int 21h
main endp
end main
Was it helpful?

Solution

Here you declare buffer to be a single byte

buffer db ?

And here you read into that single byte.

lea dx, buffer
int 21h             ; AX = bytes read

lea bx, buffer              ; get the length of buffer
mov si, ax                  
mov byte ptr [bx+si], '$'   ; put terminator at the end of buffer

so even if you would read only a single byte from the file, you are already writing beyond the limts of the buffer variable by putting the $ at the end.

I don't know how long your input file is, but it is rather likely, that you unintentationally overwrite a bigger part of memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top