"Segmentation fault (core dumped)" Assembly and c linked program

0
########
# int mycompare( int size, int x[], int y[] )
#
.text
.global mycompare

mycompare:
    pushl %ebp
    movl %esp, %ebp
    subl $4 , %esp
    movl $0 , -4(%ebp)

    movl 12(%ebp), %edx # int x[]
    movl 16(%ebp), %ebx # int y[]
    mov $0, %ecx
ciclo:
    cmp 8(%ebp) , %ecx
    je fim

    mov (%edx,%ecx,4), %eax
    cmp (%ebx,%ecx,4), %eax
    jne fim_wrong
    inc %ecx
    jmp ciclo

fim_wrong:
    mov %ecx, %eax

    mov %ebp, %esp
    pop %ebp
    ret

fim:
    mov $0xFFFFFFFF, %eax

    mov %ebp, %esp
    pop %ebp
    ret

And the c Main


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

#define MAX 1024
#define MAXLINE 2048

extern int mycompare( int size, int x[], int y[] );


int readOneArr(int max, int a[]) {
    char line[MAXLINE];
    if (fgets(line, MAXLINE, stdin) == NULL) exit(1);
    int i=0;
    char *curr;
    char *new=line;
    do {
        curr=new;
        a[i] = strtol(curr, &new, 10);
        i++;
    } while (i<max && curr!=new);
    return i-1;
}

int readArrs(int max, int a[], int b[]) {
    int na = readOneArr( max, a );
    int nb = readOneArr( max, b );
    if (na!=nb) return -1;
    return na;
}

int main() {
    int i, n;
    int a1[MAX], a2[MAX];

    n = readArrs(MAX, a1, a2);
    if(n == -1) {
        fprintf(stderr,"wrong input!\n");
        return 1;
    }
    i=mycompare(n, a1, a2);
    if ( i == -1 )
        printf("equal\n");
    else
        printf("different at %d\n", i);
    return 0;
}


I use the following cc settings to compile

cc -m32 -Wall -std=c11 -o main.c func.s

Its a program that compares two arrays of integers returning the first index where they differ, or -1 if the arrays are equal. The arrays have the same size and that size is also an argument for the function.

Its giving me :

Segmentation fault (core dumped)

If i try for example to input:

1 2 4 7 10 
1 2 4 7 10 
c
assembly
x86
calling-convention
asked on Stack Overflow May 9, 2020 by Mauricio Polvora • edited May 9, 2020 by Peter Cordes

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0