访问冲突 MASM x86 程序集

Access Violation MASM x86 Assembly

我正在做一个项目,目前我的其中一行遇到了访问冲突。我想知道我是否可以就哪里出了问题征求第二意见。这是我的代码(注意,我在运行时遇到错误,但它确实构建了):

.data
BlueTextOnGray = blue + (lightGray * 16)
DefaultColor = lightGray + (black * 16)
arrayD SDWORD 12345678h,1A4B2000h,3434h,7AB9h

fib BYTE 1,2
  BYTE NUMBER_FIBS_TO_COMPUTE dup(0)

prompt  BYTE    "Enter an ending integer: ",0
error   BYTE    "Invalid stopping point! 



.code

main PROC

    mov eax,BlueTextOnGray
    call    SetTextColor
    call    Clrscr          ; Clear the screen
    call    Crlf            ; New line

    mov edx,OFFSET prompt
    call    WriteString
    call    ReadInt         ; Input integer into EAX
    call    Crlf            ; New line

  lea esi, [fib+2]
  mov cl, NUMBER_FIBS_TO_COMPUTE
@@:
  mov al, [esi-2]
  add al, [esi-1]
  mov [esi], al   ;<------------This is where the error occurs
  inc esi
  loop @B

; here print out the results or examine them with debugger

E1: call    Crlf            ; New line
    call    WaitMsg         ; "Press any key..."
    mov eax,DefaultColor
    call    SetTextColor
    call    Clrscr



exit
main ENDP
END main

有没有我遗漏的规则。我已经做了研究,但似乎找不到适合我情况的答案。

任何帮助都会很棒! (另请注意,我还没有完成它,所以可能还有其他错误。)

谢谢!

你的问题是,无论 fib 指向哪里,它被加载到 esi,那个内存页面被标记为只读。

通常,访问冲突是由试图写入在 GDT 中标记为只读的内存位置引起的。当您尝试从您的进程根本无法访问的内存位置读取时,会发生分段错误。

正如@Jester 指出的那样,您没有注意 ECX 中的高位。当您在 CL 中设置循环控制值时,由于 ECX 未知,您的循环可能 运行 远高于您的预期。这会很快使您进入记忆中的只读区域。