Search code examples
ceclipseassemblyarmsubroutine

mixing c and assembly (ARM) subroutine


i have to make a surbroutine in two files, i have a problem to build the project, there is an error:

undefined reference to c

I don't know what happen, i'm trying to send the C[0] memory direction that's why the c=C[0], is it ok? do i have to to it with "i" too?. thank you guys.

#include <stdio.h>

const int N = 3;
extern int abs_escalar(int,int,int);
extern int abs(int x){
    if(x<0){
        x=0-x;
    }
    return x;
}
extern int c;
extern int i;
int main() {

    int A[3][3]={{1,2,3},{5,7,3},{1,5,9}};
    int B[3][3]={{3,5,7},{4,2,6},{1,8,3}};
    int C[3];
    c = C[0];
    for(i=0;i<N;i++){
        C[i] = abs_escalar(A[N][N],B[N][N],N);
    }


    return 0;
}

.asm (please ignore the spanish comments)

.extern abs;
.extern C;
.extern i;

.global abs_escalar;
.text
abs_escalar:

            ldr r4,=C;
            ldr r5,=i;
            mov r6,#0; //hace la vez de j y de cero en c[i] = 0;
            str r6,[r4,r5,lsl#2];

for:        cmp r6,r2 //r2 es n
            beq finfor
            mul r7,r2,r5 //mult ncolumnas * i
            add r7,r7,r6 //suma para el indice
            ldr r7,[r0,r7,lsl#2] //Cargo el dato de A[i][j]
            push {r0-r2} //guardo la dir de memoria de A , de B y n porque lo voy a necesitar
            mov r0,r7 //envio el dato en r0
            bl abs
            mov r7,r0 //guardo el dato en r0
            pop {r0-r2} //recupero la dir de A
            ldr r8,[r1,r7,lsl#2] //cargo el dato de B[i][j]
            push {r0-r2}
            mov r0,r8
            bl abs
            mov r8,r0
            pop {r0-r2}
            ldr r9,[r4,r5,lsl#2]
            mul r10,r7,r8
            add r9,r9,r10
            add r6,r6,#1 //aumento el indice j
            b for
finfor:     //str r9,[r4,r5,lsl#2] solo enviamos el dato la funcion principal la guarda
            mov r0,r9
            mov pc,lr


            .end

Solution

  • Where is the variable c defined? Writing

    extern int c;
    

    only declares c, you still need to define it somewhere.