2016-04-25 3 views
0

Мне нужно реализовать алгоритм сортировки вставки в x86, и мой код вообще не изменяет вывод массива. Я думаю, что проблема заключается в том, где я пытаюсь поменять местами свой внутренний цикл, но всякий раз, когда я изменяю способ получения элементов массива, ничего не происходит. Я ничего не меняю в том, что выводит программа. Почему это происходит, и как я могу это исправить?Вставка сортировка не подкачки

Мой код:

void asmSort(int *list, int arrayLen, int halfpoint) { 

/* 
* list = address of the list of integer array 
* arraylen = the number of element in the list just like list.length in java 
* halfpoint use as a flag 
* halpfpoint = 1 when the sort routine reach half point just return, otherwise finished the sort and return 
*/ 

/* 
* 
* 
insertion_sort(list,arrayLen,halfpoint); 
return; 
selection_sort(list,arrayLen,halfpoint); 
return; 
* 
* 
*/ 


// any variable can be declare here before _asm 
/* 
int tmp = 0; 
int i = 0; 
int j = 0; 
*/ 


    _asm 
{ 
    mov ecx, arrayLen 
    mov esi, list 
    mov ebx, halfpoint 
    mov eax, 99 
    push eax 
    push ebp 

    mov ebp, 4 //this is i 
    shl ecx, 2 

outerLoop: 
    cmp ebp, ecx 
    jg exitOuter 
    add esi,ebp 
    mov edi,[esi]// temp = a[i] 
    mov eax, ebp //j = i 
    sub eax, 4 // j = j-1 
innerLoop : 
    cmp eax, 0 //j>0 
    jle exitInner 
    add esi, eax // offset array to a[j] 
    mov edx, [esi] // move a[j] to edx 
    cmp edi, edx // temp < a[j] 
    jle exitInner 
    push eax 



    mov eax,[esi] 
    add esi,4 
    mov esi,edi 



    pop eax 
    sub eax,4 // j-- 
    jmp innerLoop 



exitInner: 
    shr ecx, 1 
    cmp ebp, ecx 
    je exitOuter 
    sub esi,ebp 
    add ebp, 4//i++ 
    jmp outerLoop 

exitOuter : 
    sub esi, ebp 
    pop ebp 
    pop eax 


    ; ....... 
more: cmp ecx,0 
    jle done 
    ;......... 
    mov edx,arrayLen 
    sar edx,1 
    cmp ecx,edx 
    jg cont1 
    cmp halfpoint,1 
    je done 
cont1: ;..... 
    ;...... 
    ;....... 
    ;..... 
    mov [esi],eax 
    add esi,4 
    dec ecx 
    jmp more 
done: 
} 

return; 

} 

ответ

1

Вы никогда не писать в память. Проблема здесь:

mov eax,[esi] 
add esi,4 
mov esi,edi 

Вы хотите записать в память на ESI, не регистрировать ESI.

mov eax,[esi] 
add esi,4 
mov [esi],edi 
Смежные вопросы