汇编语言 80x86

问题描述 投票:0回答:1

我正在尝试这个程序,但我收到错误消息说

Number1.exe 中 0x00411B4A 处的未处理异常:0xC0000005:访问冲突读取位置 0x000AFF01.

.586
.MODEL FLAT

INCLUDE io.h            ; header file for input/output

.STACK 4096

.DATA
array1 DWORD 0aff01h, 0bff02h, 0cff03h, 0dff04h, 0eff05h
array2 DWORD 002ff0affh, 003ff0bffh, 004ff0cffh, 005ff0dffh, 006ff0effh
array3 DWORD 5 DUP(0)             ; reserve space for 5 DWORDs
count  DWORD ?

.CODE
_MainProc PROC

    mov ecx, 5                          ; set the loop counter
    lea esi, array1                 ; load address of array1
    lea edi, array2                 ; load address of array2
    lea ebx, array3                 ; load address of array3
    call arrMix                     ; call the arrMix procedure
    mov eax, 0                      ; set a breakpoint here
    ret                             ; return from the program
_MainProc ENDP

 arrMix PROC 
    mov ecx, count                  ; initialize loop counter
    mov esi, array1                 ; load address of array1
    mov edi, array2                 ; load address of array2
    mov ebx, array3                 ; load address of array3

arrMixLoop:
    mov eax, {esi]                  ; load element from array1
    and eax, 00FF00FFh              ; keep only bits 0-7 and 16-23
    shl eax, 8                      ; shift left by 8 bits
    mov edx, [edi]                  ; load element from array2
    and edx, 0FF00FF00h             ; keep only bits 8-16 and 24-31
    shr edx, 8                      ; shift right by 8 bits
    or eax, edx                     ; combine the two bit sets
    mov [ebx], eax                  ; store the result in array3
    add esi, 4                      ; move to the next element in array1
    add edi, 4                      ; move to the next element in array2
    add ebx, 4                      ; move to the next element in array3
    loop arrMixLoop                 ; repeat until loop counter is zero

    ret                             ; return from the procedure

arrMix ENDP
END                               ; end of source code
assembly x86 masm access-violation
1个回答
1
投票
arrMix PROC 
   mov ecx, count                  ; initialize loop counter
   mov esi, array1                 ; load address of array1
   mov edi, array2                 ; load address of array2
   mov ebx, array3

您正在使用 EBX、ECX、ESI 和 EDI 中已有的输入调用 arrMix 过程。然而,arrMix 程序从销毁这些开始。

ECX=5 设置从 uninitialized count 变量中加载 ECX 被破坏,并且 3 个指针被它们各自的第一个数组元素覆盖。
因为 ECX 很可能被 0 覆盖,

loop arrMixLoop
指令开始执行它的 40 亿次迭代!并且由于指针现在无效,因此必然会发生读/写访问冲突。

在 MASM 中,

mov esi, array1
从内存中加载一个值,而
mov esi, OFFSET array1
加载数组的地址(就像您期望的那样)。

mov eax, {esi]
行在左方括号上有错字。这应该是
mov eax, [esi]
.

© www.soinside.com 2019 - 2024. All rights reserved.