Sorting 程序集中的插入排序不起作用

Sorting 程序集中的插入排序不起作用,sorting,assembly,x86,nasm,insertion-sort,Sorting,Assembly,X86,Nasm,Insertion Sort,我曾尝试在汇编中编写插入排序(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: ;ed

我曾尝试在汇编中编写插入排序(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
排序:
普沙德
mov-ebp,esp;创建堆栈帧
mov-ebx,[ebp+36];ebx保存指向数组的指针
mov-ecx,[ebp+40];ecx保存srray大小
movesi,4;我
对于_l1:
;edx=温度,esi=i,edi=j
mov edx,[ebx+esi]
mov edi、esi;j=i
次电子数据交换,4;j=i-1
而_l1:
cmp edx,[ebx+edi];如果(温度=0)
结束时1:
;阵列[j+1]=温度
mov[ebx+edi+4],edx
添加esi,4;i+=1
dec-ecx;减少排序所需的数组大小
cmp-ecx,1;如果(大小==1)
jg表示_l1
完成1:
波帕德
ret 8
您确定数组大小表示元素的数量,并且不包含数组的总长度吗

那么也许你可以编写代码

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

你是怎么调用这个的?为什么要使用ebp+36?看起来偏移量很大。您是否在调试器中验证了参数是否正确?您是否单步执行了代码以查看哪里出错*编辑:哦,我看到大偏移量是由于
pushad
造成的。是的,大偏移量确实是由于pushad造成的,您注意到任何可能导致分段错误的东西吗?使用调试器。作为一个简单的细节,告诉哪些指令错误。并展示如何调用它。