2014-12-05 50 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