冒泡排序后访问违规写入位置

时间:2017-02-15 11:31:44

标签: assembly visual-c++ x86 bubble-sort

/*
for i=0 to n-1 do
   for j=n to  i+1 do
      if (vector[j-1]<vector[j])
         swap(vector[j-1],vector[j])
      end_if
   end_for
end_for
*/
#include <iostream>
using namespace std;

void bubbleSort(int *v) {
    int n = 19;
    for (int i = 0; i < 20; i++)
    {
        printf("<%d> ", v[i]);
    }
    _asm
    {
        mov esi, v 
        mov ebx, n 
        mov ecx, 0
        loopi:
            mov edi, esi
            mov edx, n
        loopj:
            mov eax, [edi + 4 * 18]
            mov esp, [edi + 4 * 19] 
            cmp eax, esp
        jb no_swap
            xchg eax, [edi + 4 * 19] // swap
            mov [edi + 4 * 18], eax 
       no_swap :
            sub edi, 4 
            dec edx
            cmp edx, ecx
            ja loopj
            inc ecx
            cmp ecx, ebx
            jb loopi
    }
    printf("%d\n", v[1]);
    for (int i = 0; i < 20; i++)
    {
        printf("%d\n", v[i]);
    }
}
int main() {
    int vector[20] = {1,3,4,5,2,10,13,15,6,9,20,7,11,19,8,12,18,14,17,16};
    bubbleSort(vector);
    /*for (int i = 0; i < 20; i++)
    {
        printf("%d\n",vector[i]);
    }*/
    return 0;
}

问题出在排序向量之后,如果我尝试做任何崩溃并显示访问违规消息。我试图调试它,但我找不到任何问题,矢量排序很好,我没有访问v的任何内存。你能帮我吗?

0 个答案:

没有答案