Access violation writing location 0x00465004

49 Views Asked by At

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
1

There are 1 best solutions below

0
rcgldr On

The problem is ebx was going negative with the repeated subtracts. There's no need to use ebx or edx. In case arr has length zero, after sub ecx,1, you can use jb ... to jump to exit.

main PROC
        mov ecx, LENGTHOF arr
        sub ecx, 1
        jb  exit0
    l1:
            push ecx
            mov esi, 0
        l2:
                mov eax, arr[esi]
                mov edi, arr[esi + TYPE arr]
                cmp edi, eax
                jg  noswap
                mov arr[esi + TYPE arr], eax
                mov arr[esi], edi
        noswap:
                add esi, TYPE arr
                loop l2
            pop ecx
            loop l1
    exit0:
        exit
main endp
        end main