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

时间:2014-11-14 06:51:26

标签: c sorting gcc assembly

我目前正在使用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循环,以及交换代码。对于交换,我们应该使用xor算法。这是我的汇编代码:

__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

只是在一个无限循环中继续:/甚至更奇怪,我旁边的那个人从字面上复制了我,除了使用不同的寄存器来分配他的值,他的程序正常工作。任何人都知道该怎么做?

1 个答案:

答案 0 :(得分:0)

The line jg END_FOR should be jge END_FOR since the opposite of < is >= – user3386109