看看你是否能发现错误 - Quicksort中的初学者错误

时间:2016-04-29 16:31:32

标签: assembly x86 gas att

我是一个初学者,试图编写一组快速排序函数并获得分段错误(核心转储)。

我的递归quicksort函数应该调用helper quicksort函数

  1. 调用分区函数来完成大部分实际工作和
  2. 调用自己以递归方式处理子数组:
  3. 我已经看了好几个小时,并试图找到造成这种情况的错误。任何帮助都会非常感激。提前感谢您成为一个真棒,善良的人类。

    [x86 AT& T语法]

        .text
        .globl  quicksort
    quicksort:
    
    #subroutine prologue
        pushl %ebp      #store stack frame of calling function on stack.
        movl %esp, %ebp     # use current stack pointer for called function
    
        #subroutine main body
        movl 8(%ebp), %edi  # ptr to array          in edi
        movl 12(%ebp), %ecx # num elements          in ecx
    
        jl quicksort_help   # CHECK
        ret
    
    quicksort_help:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        movl    16(%ebp), %eax
        cmpl    12(%ebp), %eax
        jle     exit
        subl    $4, %esp
        pushl   16(%ebp)
        pushl   12(%ebp)
        pushl   8(%ebp)
        call    partition
        addl    $16, %esp
        movl    %eax, -4(%ebp)
        subl    $4, %esp
        movl    -4(%ebp), %eax
        decl    %eax
        pushl   %eax
        pushl   12(%ebp)
        pushl   8(%ebp)
        call    quicksort
        addl    $16, %esp
        subl    $4, %esp
        pushl   16(%ebp)
        movl    -4(%ebp), %eax
        incl    %eax
        pushl   %eax
        pushl   8(%ebp)
        call    quicksort
        addl    $16, %esp
    
    exit:
        leave
        ret
    

1 个答案:

答案 0 :(得分:2)

#subroutine prologue
    pushl %ebp      #store stack frame of calling function on stack.
    movl %esp, %ebp     # use current stack pointer for called function
    #subroutine main body
    movl 8(%ebp), %edi  # ptr to array          in edi
    movl 12(%ebp), %ecx # num elements          in ecx
*** HERE IS SOMETHING MISSING ***
    jl quicksort_help   # CHECK
    ret

jl指令只会在LESS条件下跳转,但代码中不包含任何实际定义此条件的指令!