我正在尝试用汇编语言编写一个简单的程序,在其中打开一个现有文件并在其中写入一条消息,这是我在数据段中定义的消息。当我想写入文件时会出现问题。在我尝试写入后,AX 寄存器将包含 5,诺顿专家指南称这是“拒绝访问”错误代码。关于我做错了什么有什么想法吗?抱歉,如果这个问题很简单,我只是想为即将到来的测试学习一些汇编。我正在使用 TASM 来编译我的代码。我要写入的文件存在(因此打开它时没有错误)并且它是空的。
这是我的代码:
assume cs:code, ds:data
data segment
errorMsg db 'Error at opening $'
errorMsg2 db 'Error at writing $'
msg db 'File name: $'
maxFile db 12
fileLength db ?
fileName db 12 dup(?)
buffer db 100, '$'
text db "Here $"
handle dw ?
data ends
code segment
start:
mov ax, data
mov ds, ax
;print 'File name: ' message on screen
mov ah, 09h
mov dx, offset msg
int 21h
;enter name of file
mov ah, 0ah
mov dx, offset maxFile
int 21h
; transform file name in an asciiz string which ends in 0
mov al, fileLength
xor ah, ah
mov si, ax
mov fileName[si], 0
;open file
mov ah,3dh
mov al, 0
mov dx, offset fileName
int 21h
mov handle, ax; saving the file handle
jc openError;jump if carry, will print 'error at opening'
;write in file
mov ah, 40h
mov bx, handle
mov cx, 4 ;number of bytes to write
mov dx, offset text
int 21h
jc openError2 ;jump if carry, will print 'error at writing'
;!!! here is where I get the error, my program jumps to openError2 label!!!;
;close file
mov ah, 3eh
mov bx, handle
int 21h
jmp endPrg;jump over errors if it reached this point
openError:
mov ah, 09h
mov dx, offset errorMsg
int 21h
openError2:
mov ah, 09h
mov dx, offset errorMsg2
int 21h
endPrg:
mov ax,4c00h
int 21h
code ends
end start