2014-12-05 51 views
-1

我试图在程序集(nasm)中编写插入排序,但它不起作用(总是发送段错误)。 我试图搜索问题的来源很长时间,我找不到它, 有人可以帮我找到问题吗? 谢谢。在程序集中插入排序不起作用

sort: 

pushad 
mov ebp,esp;creating stack frame 
mov ebx,[ebp+36]; ebx saves pointer to the array 
mov ecx,[ebp+40];ecx saves the srray size 
mov esi,4 ; i 


for_l1: 
    ;edx=temp,esi=i,edi=j 
    mov edx,[ebx+esi] 
    mov edi,esi;j=i 
    sub edi,4;j=i-1 
    while_l1: 


     cmp edx,[ebx+edi]; if(temp<array[j]) 
     jge end_while1 
     ;array[j+1]=array[j], j-=1 
     mov eax,[ebx+edi] 
     mov [ebx+edi+4],eax 
     sub edi,4 
     cmp edi,0 
     jge while_l1;if(j>=0) 
    end_while1: 
     ;array[j+1]=temp 
     mov[ebx+edi+4],edx 
     add esi,4;i+=1 
     dec ecx; decreasing the size of the array needed to sort 
     cmp ecx,1;if(size==1) 
     jg for_l1 
    finished1: 
     popad 
     ret 8 
+0

你怎么调用呢?为什么你使用'ebp + 36'?看起来非常大的抵消。你是否在调试器中验证了你正确地获取了参数?你有没有单步执行你的代码,看看它出错了? *编辑:哦,我看到大偏移是由于'pushad'。 – Jester 2014-12-05 16:46:31

+0

是的,大偏移确实是由于pushad,你注意到任何可能导致分段错误的东西吗? – user3475324 2014-12-05 16:57:10

+0

使用调试器。作为最基本的,告诉哪条指令错误。并展示你如何调用它。 – Jester 2014-12-05 17:00:35

回答

0

确定数组大小代表元素的数量,它不包含阵列的总长度?

那么也许你可以编写

mov ecx,[ebp+40];ecx saves the srray size 
shr ecx,2