繁体   English   中英

使用ASM内联汇编(64位)进行气泡排序的奇怪排序问题

[英]Weird sorting issue with bubble sort using ASM inline assembly (64 bit)

我目前正在64位计算机上使用C进行内联ASM组装(我认为)。 至少对于gcc,我应该使用gcc -Wall -masm = intel name.c -o name进行编译。 无论如何,我们必须使用某些规范来实现冒泡排序。

主要是模板应该看起来像这样:

#include <stdio.h>

#define ARRAY_LENGTH 20

int main()
{
    int array[ARRAY_LENGTH];
    int i;
    int swapped;

    printf("Enter the elements one by one \n");
    for (i = 0; i < ARRAY_LENGTH; i++)
    {
        scanf("%d", &array[i]);
    }

    printf("Input array elements \n");
    for (i = 0; i < ARRAY_LENGTH ; i++)
    {
        printf("%d\n", array[i]);
    }

    /*  Bubble sorting begins */
    do
    {
        swapped = 0;
        /*
        for (i = 1; i < ARRAY_LENGTH; i++)
        {
            if (array[i] < array [i-1])
            {
                swapped = 1;
                int temp = array [i-1];
                array [i-1] = array [i];
                array[i] = temp;
            }
        }
        */
        __asm__ __volatile__(";"                              //use up to 20 inst.
                             :"+r" (swapped)                  //don't touch this line 
                             :"b" (array), "a" (ARRAY_LENGTH) //don't touch this line
                             :"memory"                        //include other clobbered regs
                             );

    } while (swapped > 0);

    printf("Sorted array is...\n");
    for (i = 0; i < ARRAY_LENGTH; i++)
    {
        printf("%d\n", array[i]);
    }

    return 0;
}

基本上,我们应该在汇编中实现for循环以及交换代码。 对于交换,我们应该使用异或算法。 这是我的汇编代码:

__asm__ __volatile__(   "mov rsi, 0;"
                                "FOR:" // start for loop
                                "add rsi, 1;"   
                                "cmp rsi, rax;" // is rsi > array length (i < length?)
                                "jg END_FOR;" // if so jump to end of for loop
                                // else carry on with for loop, set values for a[i]/a[i-1]
                                "mov ecx, dword ptr [rbx + 4 * rsi];"
                                "mov edx, dword ptr [rbx + 4 * (rsi - 1)];" // set the values to ecx,edx
                                "cmp ecx, edx;" // a[i] > a[i-1]? jmp to done, else
                                "jg DONE;"
                                // swap
                                "xor ecx, edx;"
                                "xor edx, ecx;"
                                "xor ecx, edx;" // swap complete
                                // store values back into memory
                                "mov dword ptr [rbx + 4 * rsi], ecx;" // new ecx
                                "mov dword ptr [rbx + 4 * rsi - 4], edx;" // new edx
                                "mov %0, 1;"
                                "DONE:" // end else // increment counter
                                "jmp FOR;" // jump back to start of for loop
                                "END_FOR:"
                                : "+r" (swapped)        // don't touch this line [output]
                                : "b" (array), "a" (ARRAY_LENGTH) // dont touch this line [input]
                                : "memory", "cc", "rsi", "ecx", "edx"           // include other clobbered reg
                             );

问题是我的代码正在做一些非常奇怪的排序工作。 由于某种原因,一组特定的值将给我返回一个几乎排序的数组。 其他时候,程序将在无限循环中继续:| 我的老师看着它,不知道它出了什么问题,老实说我也不知道。

例如,使用以下输入:

11
-11
12
-12
13
-13
14
-14
15
-15
16
-16
17
-17
18
-18
19
-19
20
-20

产生以下结果:

-20
-19
-18
-17
-16
-15
-14
-13
-12
-11
0
11
12
13
14
15
16
17
18
19

使用此输入时:

100
90
80
70
60
50
40
30
20
10
0
-10
-20
-30
-40
-50
-60
-70
-80
-90

只是在一个无限循环中继续:/甚至很奇怪,我旁边的那个人实际上是从我那里复制过来的,除了使用不同的寄存器分配他的值,并且他的程序正常工作。 有人知道该怎么办吗?

jg END_FOR行应为jge END_FOR因为<的反义是>= – user3386109

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM