Assembly bubble sort swap

若如初见. 提交于 2019-11-27 16:31:19

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
Alexey Frunze

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.

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