Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 汇编程序(asm)无法读取文件_Assembly_Tasm_16 Bit - Fatal编程技术网

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

Assembly 汇编程序(asm)无法读取文件,assembly,tasm,16-bit,Assembly,Tasm,16 Bit,当我运行该代码时: ;-------------------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 i

当我运行该代码时:

;-------------------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:\Files\lab5\text.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
我有这个输出:

程序已启动
读取源文件时出错
节目结束了

text.txt内容:

埃尼安·乌特·斯克利斯·拉库斯,在同一个地方。埃尼安和蒂尼西德猫。布兰迪特的帕特·阿利夸姆·奥迪奥。Integer vitae ligula consequat、interdum metus nec、venenatis arcu。整型斜纹夜蛾。Praesent ac调味品精英。纳勒姆是一个小动物

我不想看这个文件。
编译器:带DOSBox的TASM 16位(带TASM的FreeDOS)。
操作系统:Windows10x64(VirtualBox上的FreeDOSV1.2)

为什么它不起作用

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

如前所述,然后删除答案:

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

0 = read only.
1 = write only.
2 = read/write.
但在代码中,您正在打开值为21h的文件

);如果一切正常,则结果为ax:0,否则不是openFiles进程 推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


这真的很有效。谢谢

检查错误代码。此外,请使用调试器。请参阅更新。调试器告诉我,我有一个错误05h只有Jose在找对地方。您以“仅写”方式打开文件,但随后尝试从中读取。你需要
20h
而不是
21h
。是的,他的观点是
21h
无效,但它是有效的。这对于您的目的来说是错误的,因为这意味着独占写入权限。@Jester,够了:)是的,先生,对不起,先生:D