2

我正在尝试从 C 转换为汇编两个函数,一个对索引数组进行排序,另一个从索引开始查找最小数字。当我运行它工作的程序时,新数组被排序但最后它给了我这个错误

1 2 3 3 4 6 7
*** stack smashing detected ***: ./invidia terminated
Annullato (core dump creato)

这是我的 .c 文件

#include <stdio.h>
#include <stdlib.h>
int* sort(int* v, int dim);
int min(int v[], int dim, int t);
int main(){
    int v[] = {1,3,2,4,6,7,3};
    int dim = 7,i;
    int *c = sort(v,dim);
    for (i = 0; i<dim; i++)
         printf("%d ", c[i]);
    return 0;
}

/*int* sort(int* v, int dim){
    int i,j=0,t;
    int mino,temp;
    for (t = 0; t<dim; t++){
        for(i=t;i<dim;i++){
            mino = min (v,dim,t); <- min number from index t
            if (v[i]==mino){
                temp = v[j];
                v[j] = v[i];
                v[i] = temp;
                j++;
                break;  
            }   
        }
    }
    return v;


}
int min(int v[], int dim, int t){
    int i, min = 1000;
    for (i=t; i<dim;i++)
        if (v[i]<min)
            min = v[i];

    return min;

}
*/

和我的 .s 文件

.globl sort
sort:       #v->ebx dim-> esi i->edi, j->ecx t->edx, mino->eax, temp ->ebp
        pushl %ebx
        pushl %esi
        pushl %edi
        pushl %ebp
        subl $32, %esp
        movl 52(%esp), %ebx
        movl 56(%esp), %esi
        movl $0, %edi
        movl $0, %ecx
        movl $0, %eax
        movl $0, %edx
        movl $0, %ebp
FOR1:
        cmpl %esi, %edx
        jge EXIT
        movl %edx, %edi
FOR2:

        cmpl %esi, %edi
        jge EXIT2
        movl %ebx, (%esp)
        movl %esi, 4(%esp)
        movl %edx, 8(%esp)
        movl %ebx, 12(%esp)
        movl %eax, 16(%esp)
        movl %ecx, 20(%esp)
        movl %edx, 24(%esp)
        call min
        movl 12(%esp), %ebx
        movl 20(%esp), %ecx
        movl 24(%esp), %edx
        cmpl (%ebx, %edi, 4), %eax
        jne PSEUDOEND
        movl (%ebx, %ecx, 4), %ebp
        movl %esi, 12(%esp)
        movl (%ebx, %edi, 4), %esi
        movl %esi, (%ebx, %ecx, 4)
        movl %ebp, (%ebx, %edi, 4)
        movl 12(%esp), %esi
        incl %ecx
        incl %edx
        jmp FOR1

PSEUDOEND: 
        incl %edi
        jmp FOR2
EXIT:
        movl %ebx, %eax
        popl %ebp
        popl %edi
        popl %esi
        popl %ebx
        addl $32, %esp
        ret

EXIT2:
        incl %edx
        jmp FOR1

.globl min
min:
        movl 4(%esp), %ecx
        movl 8(%esp), %edx
        movl 12(%esp), %ebx
        movl $1000, %eax
FOR:
        cmpl %edx, %ebx
        jge END
        cmpl %eax, (%ecx, %ebx, 4)
        cmovl (%ecx, %ebx, 4), %eax
        incl %ebx
        jmp FOR
END:

        ret

编译我使用

gcc -m32 filename.c filename.s -o filename

我不认为问题出在程序集翻译中,因为它正确打印了新数组。可能是什么问题呢?

4

0 回答 0