在汇编中写入文件的问题
Issues writing to file in assembly
我正在尝试用汇编语言编写一个简单的程序,我在其中打开一个现有文件并在其中写入一条消息,一条消息是我在数据段中定义的。当我想写入我的文件时出现问题。在我尝试写入后,AX 寄存器将包含 5,诺顿专家指南说这是一个 'Access denied' 错误代码。关于我做错了什么的任何想法?抱歉,如果这个问题很简单,我只是想为即将到来的测试学习一些汇编。我正在使用 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
好吧,这个问题其实很抱歉,但我终于想通了。当我打开文件时,我做了
mov al, 0
这意味着我以只读权限打开了文件。我需要做的是
mov al, 1 (write-only access) or
mov al, 2 (read+write access).
抱歉打扰了大家,很高兴我终于弄明白了。
我正在尝试用汇编语言编写一个简单的程序,我在其中打开一个现有文件并在其中写入一条消息,一条消息是我在数据段中定义的。当我想写入我的文件时出现问题。在我尝试写入后,AX 寄存器将包含 5,诺顿专家指南说这是一个 'Access denied' 错误代码。关于我做错了什么的任何想法?抱歉,如果这个问题很简单,我只是想为即将到来的测试学习一些汇编。我正在使用 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
好吧,这个问题其实很抱歉,但我终于想通了。当我打开文件时,我做了
mov al, 0
这意味着我以只读权限打开了文件。我需要做的是
mov al, 1 (write-only access) or
mov al, 2 (read+write access).
抱歉打扰了大家,很高兴我终于弄明白了。