In my procedure of my bubble sort, I get this error:
Unhandled exception at 0x00000006 in Project.exe: 0xC0000005: Access violation.
This occurs at ret
of the procedure. I am not sure what does error means or how to fix this.
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
exit
main ENDP
Bubblesort PROC
push esi ;save esi
push ecx ;save ecx
dec ecx ; size of array
L1:
push ecx ;save outer loop count
mov esi, arrayDW ;point to first value in the array was {mov esi, arrayDW}
L2:
mov eax, arrayDW[esi] ; get array value, eax is the comparer
cmp arrayDW[esi+4], eax ; compare current value with the next value
jg L3 ; if [ESI=eax] <= [ESI+4], do not exchange
xchg eax, arrayDW[esi+4] ; exchange the pair since [ESI=eax] > [ESI+4]
mov arrayDW[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
My assembly is atrociously rusty, but it looks like you push esi
and push ecx
at the start of your bubblesort routine and never pop
them, so your ret
instruction would be trying to pull the caller's address off the stack and end up jumping to what had been ecx
, that is, the length of your array. Your array had 6 elements in it, so it would make sense that you'd end up jumping to 0x00000006
.
You can fix this either by adding two pop
instructions at the end of your subroutine or by removing the push esi
and push ecx
at the start of it.