在AT&T汇编中实现Mergesort函数以用于C程序

在AT&T汇编中实现Mergesort函数以用于C程序,c,assembly,mergesort,att,C,Assembly,Mergesort,Att,为了一个大学。我们的任务是在C和汇编语言中实现一个mergesort程序,并对两者进行比较,现在C语言的实现很好,但是,我在汇编部分遇到了一些问题 汇编代码导致第61行出现分段故障 movl %ecx, (%edx, %eax, 4) # tmp[i] = arr[n] 但我看不出这会导致它崩溃的原因,或者如何修复它 asm.S汇编,x86,AT&T语法 #include "asmdef.h" DECLARE_GLOBAL(mergesort) FUNC (merges

为了一个大学。我们的任务是在C和汇编语言中实现一个mergesort程序,并对两者进行比较,现在C语言的实现很好,但是,我在汇编部分遇到了一些问题

汇编代码导致第61行出现分段故障

movl %ecx, (%edx, %eax, 4)  # tmp[i] = arr[n]
但我看不出这会导致它崩溃的原因,或者如何修复它

asm.S汇编,x86,AT&T语法

#include "asmdef.h"

    DECLARE_GLOBAL(mergesort)


    FUNC (mergesort):


    pushl %ebp                      # Saving caller's EBP
    movl %esp, %ebp                 # Creating new stack frame

    movl 24(%ebp), %eax
    #movl 

    pushl %ebx                      # EBX register
    pushl %esi                      # ESI register is used as j
    pushl %edi                      # EDI register is used as n

                                    # EAX register is used as i
                                    # ECX register is used as mid
                                    # EDX register is used as max

    movl 8(%ebp), %ebx              # EBX = arr[]
    movl 12(%ebp), %esi             # ESI(j) = min
    movl 16(%ebp), %edi             # EDI(n) = mid
    addl $1, %edi                   # EDI(n) += 1
    movl 16(%ebp), %ecx             # ECX = mid
    movl 20(%ebp), %edx             # EDX = max

    movl %esi, %eax                 # i = min

    first_loop:
    first_if:
        cmpl %ecx, %esi             # compare j with mid
        jg second_if                # jump if j bigger than mid

    second_if:
        cmpl %edx, %edi             # Compares max with n
        jg end_of_first_loop        # Jumps if n bigger than max

    third_if:
        pushl %ecx                  # Pushing mid on stack meanwhile
        pushl %edx                  # Pushing max on stack meanwhile
        movl (%ebx, %esi, 4), %ecx  # ECX = arr[j]
        cmpl (%ebx, %edi, 4), %ecx  # Compare arr[j] with arr[n]
        jg first_else               # Jump to first else

                                    # Using EDX as tmp[]

        movl %ecx, (%edx, %eax, 4)  # tmp[i] = arr[j]
        incl %esi                   # Increment j with 1

        popl %edx                   # EDX = max
        popl %ecx                   # ECX = mid
        incl %eax                   # Increment i with 1

        jmp first_loop

    first_else:
        pushl %edx                  # Pushing max on stack
        pushl %ecx                  # Pushing mid on stack
        movl (%ebx, %edi, 4), %ecx  # ECX = arr[n]
        movl %ecx, (%edx, %eax, 4)  # tmp[i] = arr[n]
        incl %edi                   # Increment n with 1

        popl %ecx                   # ECX = mid
        popl %edx                   # EDX = max
        incl %eax                   # Increment i with 1        

        jmp first_loop


end_of_first_loop:
    jmp fourth_if

fourth_if:
    cmpl %ecx, %esi                 # Compares mid and j
    jg second_loop                  # Jumps to else if j is not bigger

    jmp second_else

second_loop:
    pushl %esi                      # Pushing j on stack 
    pushl %ecx                      # Pushing mid on stack
    #ESI = tmp meanwhile    
    #ECX = k meanwhile

    movl %edi, %ecx                 # m = n
    fifth_if:
        cmpl %edx, %ecx             # compares n <= max 
        jg end_1
        pushl %edx                  # push max to stack

        movl (%ebx, %ecx, 4), %edx  # EDX = arr[n]
        movl %edx, (%esi, %eax, 4)  # tmp[i] = arr[n]

        popl %edx                   # EDX = max
        incl %eax                   # Increments i with 1
        incl %ecx                   # Increments n with 1
        jmp fifth_if

end_1:
    popl %ecx                       # ECX = mid
    popl %esi                       # ESI = j   
    jmp last_loop

second_else:
    third_loop:
        pushl %edi                  # Pushing n to stack
        pushl %edx                  # Pushing max to stack

        movl %esi, %edi             # n = j
        sixth_if:
            cmpl %ecx, %esi         # if n > mid jump
            jg end_2
            pushl %ecx              # Pushing mid to stack

            movl (%ebx, %edi, 4), %ecx  # ECX = arr[n]
            movl %ecx, (%edx, %eax, 4)  # tmp[i] = arr[n]           

            popl %ecx               # ECX = mid
            incl %eax               # Increments i with 1
            incl %edi               # Increments n with 1
            jmp sixth_if

    end_2:
        popl % edx                  # EDX = max
        popl %edi                   # EDI = n
        jmp last_loop

last_loop:
    pushl %esi                      # push j to stack
    pushl %eax                      # push i to stack

    movl 12(%ebp), %esi             # n = min
    seventh_if:
        cmpl %edx, %esi             # if n > max jump
        jg end_3
        pushl %edx                  # push max to stack

        movl (%edx, %esi, 4), %eax  # EAX = tmp[n]
        movl %eax, (%ebx, %esi, 4)  # arr[n] = tmp[n]

        popl %edx                   # EDX = max
        incl %esi                   # Increment n with 1
        jmp seventh_if

end_3:
    popl %eax                       # EAX = i
    popl %esi                       # ESI = j
    jmp end_final

end_final:
    popl %edi
    popl %esi
    popl %ebx

    popl %ebp
    ret
主要.c.相关部分

#include <stdio.h>
#include <stdlib.h>



extern void mergesort(int arr[], int min, int mid, int max, int size);

void _part(int arr[], int min, int max, int size) {
    int mid;
    if(min < max) {
        mid = (min + max) / 2;
        _part(arr, min, mid, size);
        _part(arr, mid + 1, max, size);
        mergesort(arr, min, mid, max, size);
    }
}

int main() {
    int arrsize = 150;
    int arr[arrsize];
    int i, j;

    for(i = 0 ; i < arrsize ; i++) {
        j = rand()%1000;
        arr[i] = j;
    }

    _part(arr, 0, arrsize - 1, arrsize);
    printf("\n\t merge sorting completed \n");
        for(i = 0 ; i < arrsize ; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

使用调试器检查故障点处的edx和eax值,并查看哪一个是错误的。然后倒转看它是如何出错的。为什么有4个参数而不是3个参数用于_零件,特别是为什么有一个尺寸-1和一个尺寸参数?代码可以使用min、mid、max,也可以使用min、mid、size,但它不需要同时使用max和size。我个人的偏好是min,mid,size,其中左索引从min到mid-1,右索引从mid到size-1只使用小于循环结束检查。自下而上的合并排序可能更简单,但我不知道分配是否需要自上而下的合并排序。
#include <stdio.h>
#include <stdlib.h>



extern void mergesort(int arr[], int min, int mid, int max, int size);

void _part(int arr[], int min, int max, int size) {
    int mid;
    if(min < max) {
        mid = (min + max) / 2;
        _part(arr, min, mid, size);
        _part(arr, mid + 1, max, size);
        mergesort(arr, min, mid, max, size);
    }
}

int main() {
    int arrsize = 150;
    int arr[arrsize];
    int i, j;

    for(i = 0 ; i < arrsize ; i++) {
        j = rand()%1000;
        arr[i] = j;
    }

    _part(arr, 0, arrsize - 1, arrsize);
    printf("\n\t merge sorting completed \n");
        for(i = 0 ; i < arrsize ; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}