I am writing the code for bubble sorting in assembly and my assembler is giving me "Access violation writing location" error inside the swap label. kindly help
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
The problem is ebx was going negative with the repeated subtracts. There's no need to use ebx or edx. In case
arrhas length zero, aftersub ecx,1, you can usejb ...to jump toexit.