32bit assembly - insertion sort doesn't work properly

孤人 提交于 2019-12-25 06:48:05

问题


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.



来源:https://stackoverflow.com/questions/27334022/32bit-assembly-insertion-sort-doesnt-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!