我有一个DumpArray程序,无法正确打印出数组。
我收到此错误:
Unhandled exception at 0x0040107d in Project.exe: 0xC0000005: Access violation reading location 0x00000006.
在DumpArray的程序中,更具体地说是在mov eax,[esi]
INCLUDE Irvine32.inc
.data
arrayDW SDWORD 5,6,7,3,5,3 ;array
.code
main PROC
mov esi, OFFSET arrayDW ;ESI points arrayDW
mov ecx, LENGTHOF arrayDW ;ECX = array count
call Bubblesort ;sorts arrayDW
call DumpArray ;print array
exit
main ENDP
DumpArray PROC
push esi
push ecx
push eax
L1:
mov eax,[esi]
call WriteInt
add esi, 4
loop L1
call Crlf
pop esi
pop ecx
pop eax
ret
DumpArray ENDP
结束主要
答案 0 :(得分:0)
这是在Bubblesort程序中:ESI已经指向arrayDW中的正确内存位置。命令mov eax, arrayDW[esi]
从ESI 加上 arrayDW的内存位置获取值 - 远离正确的内存位置。
我更正了你的代码; - ):
INCLUDE Irvine32.inc
.data
arrayDW SDWORD 5,6,7,3,5,3 ;array
.code
main PROC
mov esi, OFFSET arrayDW ;ESI points arrayDW
mov ecx, LENGTHOF arrayDW ;ECX = array count
call Bubblesort ;sorts arrayDW
call DumpArray ;print array
exit
main ENDP
DumpArray PROC USES esi ecx eax
L1:
mov eax,[esi]
call WriteInt
add esi, 4
loop L1
call Crlf
ret
DumpArray ENDP
Bubblesort PROC USES esi ecx
dec ecx
L1:
push ecx ;save outer loop count
mov esi, OFFSET arrayDW ;point to first value in the array was {mov esi, arrayDW}
L2:
mov eax, [esi] ; get array value, eax is the comparer
cmp [esi+4], eax ; compare current value with the next value
jg L3 ; if [ESI=eax] <= [ESI+4], do not exchange
xchg eax, [esi+4] ; exchange the pair since [ESI=eax] > [ESI+4]
mov [esi], eax ; place the higher value
L3:
add esi, 4 ;move both pointers foward
loop L2 ; looping inner loop
pop ecx ;retrieve outer loop count
loop L1 ; else repeat outer loop
ret
Bubblesort ENDP
END main