【问题标题】:Insertion sort not swapping插入排序不交换
【发布时间】:2016-04-25 07:11:38
【问题描述】:

我必须在 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;

}

【问题讨论】:

    标签: visual-c++ assembly x86 masm insertion-sort


    【解决方案1】:

    你永远不会写入内存。问题出在这里:

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

    你想写入内存ESI,而不是注册ESI

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

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多