组装冒泡排序交换 [英] Assembly bubble sort swap

查看:23
本文介绍了组装冒泡排序交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 x86 程序集中进行冒泡排序(是的,它必须是冒泡排序,因为我不关心不同类型排序的速度优化)并且出于某种原因,我的代码不会交换必要的值.这是我的代码

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 reason, my code will not swap the necessary values. Here is my code

mov eax, list                   ;store list in eax
mov edx,[eax+4*edi-4]           ;temp = var1
cmp edx,[eax+edi*4]             ;compare
JLE SECOND_LOOP                 ;jump if var1 < var2
mov [eax+4*edi-4],[eax+edi*4]   ;var1 = var2
mov [eax+edi*4], edx            ;var2 = temp
jmp SECOND_LOOP

在应该将临时值加载回地址的最后一条 mov 指令中,它..没有.EAX 寄存器具有包含我的值列表的数组的起始地址

At the last mov instruction where it's supposed to load the temp back into the address, it..doesn't. The EAX register has the starting address of the array which contains my list of values

0x*starting address* 0a 00 00 00 ec ff ff ff 05 00 00 00 0c 00 00 00 1e 00 00 00 fb ff ff ff ea
0x*address after   * ff ff ff 37 00 00 00 34 00 00 00 00 00 00 00

并且下一个地址包含更多数字.十进制数为10 -20 5 12 30 -5 -22 55 52 0.基本上现在我正在尝试将 FFFFFFEC 移动到 0000000A,然后将 0000000A 移动到 FFFFFFEC.我可以将它存储到我的临时寄存器 EDX 中,但不能将 EDX 的值存储到特定地址中.有什么帮助吗?

and the next address contains a few more numbers. In decimal, the numbers are 10 -20 5 12 30 -5 -22 55 52 0. Essentially right now I'm trying to move FFFFFFEC to 0000000A and then move 0000000A to FFFFFFEC. I can store it into my temp register EDX, but cannot store the value of EDX into the specific address. Any help?

推荐答案

我想我会使用指向列表当前位置的指针,而不是每次使用时都需要缩放的索引:

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

这篇关于组装冒泡排序交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆