【问题标题】:Issues writing to file in assembly在程序集中写入文件的问题
【发布时间】:2016-12-26 22:37:13
【问题描述】:

我正在尝试在汇编中编写一个简单的程序,在该程序中我打开一个现有文件并在其中写入一条消息,一条我在我的数据段中定义的消息。当我想写入我的文件时会出现问题。在我尝试写入 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

【问题讨论】:

    标签: assembly x86 dos x86-16


    【解决方案1】:

    好吧,其实很抱歉这个问题,但我终于想通了。当我打开文件时,我做到了

    mov al, 0
    

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

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

    对不起,打扰了,我很高兴我终于弄明白了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多