我正在编写汇编中的冒泡排序代码,我的汇编器在交换标签内给出“访问冲突写入位置”错误。请帮忙
INCLUDE irvine32.inc
.data
arr DWORD 8, 5,1,2,6
.code
main PROC
mov ecx, LENGTHOF arr
sub ecx, 1
mov ebx, LENGTHOF arr
sub ebx, 1
mov edx, 0
l1:
push ecx
sub ebx, edx
mov ecx, ebx
mov esi, 0
l2:
mov eax, arr[esi]
mov edi, arr[esi + TYPE arr]
cmp edi, eax
jle swap
jmp done
swap:
mov arr[esi + TYPE arr], eax
mov arr[esi], edi
done:
add esi, TYPE arr
loop l2
pop ecx
add edx, 1
loop l1
exit
main endp
end main
问题是 ebx 随着重复的减法而变为负值。无需使用 ebx 或 edx:
main PROC
mov ecx, LENGTHOF arr
sub ecx, 1
l1:
push ecx
mov esi, 0
l2:
mov eax, arr[esi]
mov edi, arr[esi + TYPE arr]
cmp edi, eax
jg done
mov arr[esi + TYPE arr], eax
mov arr[esi], edi
done:
add esi, TYPE arr
loop l2
pop ecx
loop l1
exit