簡體   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