Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
458 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...