在汇编中写入文件时出现问题 [英] Issues writing to file in assembly

查看:71
本文介绍了在汇编中写入文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用汇编语言编写一个简单的程序,在其中打开一个现有文件,并在其中编写一条消息,该消息是在数据段中定义的.当我要写入文件时会发生问题.我尝试写入AX寄存器后,其中将包含5,并且Norton Expert Guide说这是拒绝访问"错误代码.关于我在做什么错的任何想法吗?抱歉,如果这个问题很简单,我只是想学习一些组件以准备即将进行的测试.我正在使用TASM编译我的代码.我要写入的文件存在(因此打开时没有错误),并且为空.

I am trying to write a simple program in assembly in which I open an existing file and I write a message in it, a message which I define in my data segment. The problem occurs when I want to write to my file. The AX register will contain 5 after I try writing to it, and the Norton Expert Guide says that is an 'Access denied' error code. Any ideas on what I'm doing wrong? Sorry if this question is pretty easy, I'm just trying to learn some assembly for an upcoming test. I am using TASM to compile my code. The file in which I want to write exists(hence the no error when I open it) and it's empty.

这是我的代码:

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

推荐答案

好的,很抱歉,这个问题,但是我终于明白了.当我打开文件时,我做了

Alright, sorry for this question actually, but I finally figured it out. When I opened the file, I did

mov al, 0

这意味着我以只读访问权限打开了该文件.我需要做的是

which means I opened the file with read-only access. What I needed to do was

mov al, 1 (write-only access) or 
mov al, 2 (read+write access). 

对不起,我很高兴我终于知道了.

Sorry for the bother guys, I'm just glad I finally figured it out.

这篇关于在汇编中写入文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆