32bit assembly - insertion sort doesn't work p

2019-09-02 05:38发布

问题:

My task here is to add a code that sorts the array with insertion sort. 'printf' function prints a string printArray prints the array

For some reason the array doesn't get sorted, and i cant find the reason why. Help will be appreciated.

main:
        push MSG    ; print welcome message
        call printf
        add esp,4   ; clean the stack 

        call printArray ;print the unsorted array

        ;;;;;;;;;;add code here;;;;;;;;;;

        mov eax,1
loop1:
        mov ebx, array
        add ebx, eax

loop2:
        mov esi, ebx
        dec esi
        mov esi, [esi]      ;esi holds the value before what ebx points to
        cmp [ebx], esi
        ja endLoop2

        mov edx, esi
        mov esi, ebx
        dec esi
        mov ecx, [ebx]
        mov [esi], ecx
        mov [ebx], edx
        dec ebx
        cmp ebx, array
        ja loop2

endLoop2:
        inc eax
        cmp eax, 11
        jbe loop1

        ;;;;;;;end of your code;;;;;;;;;;;;;;

        call printArray

        mov eax, 1  ;exit system call
        int 0x80

回答1:

If your array is full of 1 byte values, use movb instead of mov when loading and storing to memory.