2015-07-02 59 views
0

項目將在裝配中創建一個氣泡排序算法,該算法將整理給定的整數列表。我已經上升,我的輸出在一定程度上是正確的。這似乎結合時的編號順序被混淆了,這裏就是我的意思:整數在裝配中對整數進行排序

10 -20 10 12 30 -22 -5 55 52 0

數= 10 Ascend_or_Descend = 1

0 5 10 12 30 52 55 -22 -20 -5

下面是包括將其提供給測試的主要方法的代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <io.h> 

int sorter (int* list, int count, int opcode) 
{ 
__asm 
{ 
mov eax, 0   ; zero out the result 
mov ebx, opcode  ; move opcode to ebx for comparison 

mov ecx, count; //OuterLoop counter 
cmp ecx, 1; //check length 
jle Return; 

OuterLoop: 
    mov edi, list; //move in list  
    push ecx; 

    mov ecx, count; 
    dec ecx; //Decrement inner loop 
InnerLoop: 
    mov eax, [edi]; //First Value in list 
    cmp ebx, 1; 
    je Ascending; 
    cmp eax, [edi+4]; //next int is 4 bits away 
    jb Swap; 
    jmp Continue; 
Ascending: 
    cmp eax, [edi+4]; //Again compare with next int 
    ja Swap; //if no swap 
    jmp Continue; 
Swap: 
    mov edx, [edi+4]; //Move to temp register 
    mov [edi+4], eax; //Move stored value there 
    mov [edi], edx; //Return temp value to list 
Continue: 
    add edi, 4; //Increment to move to next int 
    loop InnerLoop; 
    pop ecx; //reset counter 
    loop OuterLoop; 
Return:; 
} 

} 


int main(int argc, char** argv) 
{ 
int numlist[1000], asc; 
FILE *fptr; 

int i = 0; 

if (argc != 3) 
{ 
    printf("Usage: %s filename asc_des\n", argv[0]); 
    return 1; 
} 

asc = atoi (argv[2]); 
asc = (asc == 1) ? 1 : 2; 

printf("\n"); 

fptr = fopen((argv[1]), "rtc"); 
if (fptr == NULL) 
    printf("File %s was not opened\n",argv[1]); 
else 
{ 
    /* Set pointer to beginning of file: */ 
    fseek(fptr, 0L, SEEK_SET); 

    /* Read data from file: */ 
    while (fscanf(fptr, "%d", &numlist[i]) != EOF) 
    { 
     printf("%d ", numlist[i]); 
     i++; 
    } 

    printf("\n\nNumber of integer = %d\n", i); 
    printf("Ascend_or_Descend = %d\n\n", asc); 
    fclose(fptr); 
    } 

    sorter(numlist, i, asc); 

    for (int j = 0; j < i; j++) 
     printf("%d ", numlist[j]); 

    printf("\n"); 

    return 0; 
    } 

回答

3

從英特爾公司的手冊:

術語「上方」和「下方」與CF標誌相關聯,並且是指關係兩個無符號整數 值之間。術語「較大」和「少」與SFOF標誌相關聯,並指關係 兩個有符號整數之間的值

通過使用jbja你治療比較的結果無符號比較的結果,因此爲什麼有符號數字最終會在有序數組的末尾(例如-22解釋爲無符號32位值爲4294967274)。

您應該使用jl而不是jbjg而不是ja

+0

對不起,遲到的迴應,但你是正確的。這個新東西,謝謝! – FrenchFruits