汇编程序 (asm) 无法读取文件



当我运行该代码时:

;-------------------MACRO-----------------
println MACRO info
    push ax
    push dx
    mov ah, 09h
    mov dx, offset info
    int 21h
    ;print new line
    mov dl, 10
    mov ah, 02h
    int 21h
    mov dl, 13
    mov ah, 02h
    int 21h
    pop dx
    pop ax
ENDM
;-----------------end macro----------------
.model small
.stack 100h
.data
sourcePath db "C:Fileslab5text.txt"
sourceID dw 0
maxWordSize equ 50
buffer db maxWordSize + 2 dup(0)
startText db "Program is started", '$'
badSourceText db "Cannot open source file", '$'
fileNotFoundText db "File not found", '$'
errorClosingSource db "Cannot close source file", '$'
errorClosingDest db "Cannot close destination file", '$'
endText db "Program is ended", '$'
errorReadSourceText db "Error reading from source file", '$'
errorWritingDestText db "Error writing to destination file", '$'
.code
main:
    mov ax, @data
    mov es, ax
    mov ds, ax
    println startText
    call openFiles
    cmp ax, 0
    jne endMain             ;we have some error
    call processingFile
    cmp ax, 0
    jne endMain             ;we have some error
    call closeFiles
    cmp ax, 0
    jne endMain             ;we have some error
endMain:
    ;exit
    println endText
    mov ah, 4Ch
    int 21h
;Result in ax: 0 if all is good, else not
openFiles PROC
    push bx dx
    ;open source
    mov ah, 3Dh         ;open source file
    mov al, 21h         ;readonly, block write, other cannot write
    mov dx, offset sourcePath
    mov cx, 01h
    int 21h
    jb badOpenSource    ;works when cf = 1
    mov sourceID, ax    ;save file ID
    mov ax, 0           ;return value
    jmp endOpenProc     ;all is good
badOpenSource:
    println badSourceText
    cmp ax, 02h
    jne errorFound
    println fileNotFoundText
errorFound:
    mov ax, 1
endOpenProc:
    pop dx bx
    ret
ENDP
;macro help processing
;bx - file ID
resetPosInFileToStart MACRO
    push ax bx cx dx
    mov ah, 42h
    xor al ,al          ;mov al, 0 - from file start
    xor cx, cx
    xor dx, dx
    int 21h
    pop dx cx bx ax
ENDM
;end macro help
processingFile PROC
    push ax bx cx dx si di
    mov bx, sourceID
    resetPosInFileToStart
    call readFromFile
    cmp ax, 0
    je finishProcessing
    mov si, offset buffer
    mov di, offset buffer
    mov cx, ax                  ;save num of symbols in buffer
    xor dx, dx
finishProcessing:
    pop di si dx cx bx ax
    ret
ENDP
;Result in ax: 0 if all is good, else not
closeFiles PROC
    push bx cx
    xor cx, cx
    mov ah, 3Eh
    mov bx, sourceID
    int 21h
    jnb goodCloseOfSource       ;cf = 0
    println errorClosingSource
    inc cx          ;now it is a counter of errors
goodCloseOfSource:
    mov ax, cx      ;save number of errors
    pop cx bx
    ret
ENDP
;reads to buffer maxWordSize symbols
;RES: ax - how much symbols we read
readFromFile PROC
    push bx cx dx
    mov ah, 3Fh
    mov bx, sourceID
    mov cx, maxWordSize
    mov dx, offset buffer
    int 21h
    jnb goodRead                    ;cf = 0 - we read file
    println errorReadSourceText
    mov ax, 0
goodRead:
    pop dx cx bx
    ret
ENDP
end main

我有那个输出:

程序已启动
从源文件
读取时出错 程序已结束

文本.txt内容:

Aenean ut scelerisque lacus, at aliquam ipsum.Aenean et tincidunt felis.Suspendisse volutpat aliquam odio at blandit.Integer vitae ligula consequat, interdum metus nec, venenatis arcu.Integer rhoncus quis felis et maximus.Praesent ac condimentum elit.Nullam a molestie ligula.

我想读这个文件。
编译器:TASM 16 位与 DOSBox (和 FreeDOS 与 TASM(。
操作系统:Windows 10 x64(VirtualBox上的FreeDOS v1.2(。

为什么它不起作用?

更新
错误代码:我在 DOS 3Fh (int 21h
( 中出现错误CF = 1,AL = 05h(访问被拒绝(。但是文件是免费的。

正如何塞·曼努埃尔·阿巴卡·罗德里格斯所说,然后删除答案:

打开文件时有三个可能的值:

0 = read only.
1 = write only.
2 = read/write.

但是在您的代码中,您打开的值为 21h

的文件

;结果为 ax:如果一切正常,则为 0,否则无法打开文件 PROC 按 BX 推送 DX

;open source
mov ah, 3Dh         ;open source file
mov al, 21h         ;readonly, block write, other cannot write ◄■■██ As !!!
mov dx, offset sourcePath
mov cx, 01h
int 21h
jb badOpenSource    ;works when cf = 1

将 21h 替换为 0、1 或 2 :

MOV AL, 0

它真的很有效。谢谢

最新更新