Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

arrays - 32bit assembly - insertion sort doesn't work properly

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...