rjk
rjk

Reputation: 61

File not opening

Hello I'm new to masm and the goal is to open a file but my code is not working

I have this snippet of code:

.data
filename db "highscore.txt", 0 
handle dw ?
buffer db 100 dup(?)

.code
...
mov dx, offset filename
mov al, 2
mov ah, 3dh
int 21h

mov handle, ax
jc erroropening

mov dx, offset buffer
mov bx, handle
mov cx, 100
mov ah, 3fh
int 21h

mov counter_files, ax

mov bx, handle
mov ah, 3Eh
int 21h
...
erroropening:
    mov dx, offset newline
    mov ah, 09h
    int 21h
    mov dx, offset errormsg
    mov ah, 09h
    int 21h
    mov bx, handle
    mov ah, 3Eh
    int 21h
...

but somehow the file is in not opening, even though it's in the same folder of my asm file. Please help, I'm a newbie here.

Upvotes: 2

Views: 55

Answers (1)

Fifoernik
Fifoernik

Reputation: 9899

3 observations about your program:

  • As Dirk Wolfgang Glomp said DOS uses 8.3 filenames and thus the name is limited to 8 characters. Your string "highscore.txt" uses 1 character too many for the name part.

  • When you jump to erroropening you already moved the AX register in the handle variable. At this point AX has an errorcode and not a handle. Nonetheless you use it to close a file after having displayed a message.

  • You would better also check the CF delivered by the DOS ReadFile function. Add another routine errorreading and in it closing the file would be a sensible thing to do.

Upvotes: 3

Related Questions