Assembly bubble sort swap

前端 未结 2 1181
北海茫月
北海茫月 2020-12-04 04:14

I\'m trying to do a bubble sort in x86 assembly (yes it has to be bubble, as I\'m not concerned about speed optimization regarding different types of sorts) and for some rea

相关标签:
2条回答
  • 2020-12-04 04:39

    This part of your code:

    mov edx,[eax+edi*4]
    mov [eax+edi*4], edx
    

    effectively does not change anything in the memory, it reads a value from the memory and writes it back where it's just got it from.

    Btw, you may be interested in the xchg instruction.

    0 讨论(0)
  • 2020-12-04 04:41

    I think I'd use pointers into the current position into the list, instead of an index that needs to be scaled every time you use it:

        mov esi, offset list
    top:
        mov edi, esi
    inner:
        mov eax, [edi]
        mov edx, [edi+4]
        cmp eax, edx
        jle no_swap
        mov [edi+4], eax
        mov [edi], edx
    no_swap:
        add edi, 4
        cmp edi, list_end - 4
        jb inner
        add esi, 4
        cmp esi, list_end - 4
        jb top
    
    0 讨论(0)
提交回复
热议问题