Calling an assembly function from C -
one of generated functions doesn't compute should , trying debug separately. have assembler , try call stripped down c program. however, reason end getting segfaults in function (so, calling function seems work, execution fails). there might wrong passing arguments..
the functions signature is
void func(int, int, float*, float*, float*);
the function ignores first 2 arguments , received 3 arrays of 32 floats each. add element-wise latter 2 , store result element-wise first array. however, in kind of weird order (as opposed streaming linearly through it, reason doing not in scope of question). that's offset_arrays
in assembler code.
i checked x86 calling conventions (that's architecture using) , first 6 integer or pointer arguments passed in registers rdi, rsi, rdx, rcx, r8, , r9.
here' function implementation:
.text .globl func .align 16, 0x90 .type func,@function func: .cfi_startproc xorl %eax, %eax movabsq $offset_array1, %r9 movabsq $offset_array, %r10 xorl %esi, %esi .align 16, 0x90 .lbb0_1: movq (%r9,%rax), %r11 movq (%r10,%rax), %rdi movss (%r8,%rdi,4), %xmm0 addss (%rcx,%rdi,4), %xmm0 movss %xmm0, (%rdx,%r11,4) incq %rsi addq $8, %rax cmpq $33, %rsi jb .lbb0_1 retq .ltmp0: .size func, .ltmp0-func .cfi_endproc .type offset_array,@object .section .rodata,"a",@progbits .align 16 offset_array: .quad 0 .quad 16 .quad 1 .quad 17 .quad 2 .quad 18 .quad 3 .quad 19 .quad 4 .quad 20 .quad 5 .quad 21 .quad 6 .quad 22 .quad 7 .quad 23 .quad 8 .quad 24 .quad 9 .quad 25 .quad 10 .quad 26 .quad 11 .quad 27 .quad 12 .quad 28 .quad 13 .quad 29 .quad 14 .quad 30 .quad 15 .quad 31 .size offset_array, 256 .type offset_array1,@object .align 16 offset_array1: .quad 0 .quad 16 .quad 1 .quad 17 .quad 2 .quad 18 .quad 3 .quad 19 .quad 4 .quad 20 .quad 5 .quad 21 .quad 6 .quad 22 .quad 7 .quad 23 .quad 8 .quad 24 .quad 9 .quad 25 .quad 10 .quad 26 .quad 11 .quad 27 .quad 12 .quad 28 .quad 13 .quad 29 .quad 14 .quad 30 .quad 15 .quad 31 .size offset_array1, 256 .section ".note.gnu-stack","",@progbits
i try call function c code:
float f0[32]; float f1[32]; float f2[32]; extern void func(int i0,int i1,float* dest,float* src0,float* src1); int main(int argc, char *argv[]) { func(0,0,f0,f1,f2); }
compiling both , linking with
gcc -o f.o -c -g f.s gcc -g -o test_f test_f.c f.o
and running though gdb results in
program received signal sigsegv, segmentation fault. func () @ f.s:17 17 movss %xmm0, (%rdx,%r11,4)
so, write memory, namely first array. why segfault , how call function correctly (without changing assembler code)?
the problem comes line
cmpq $33, %rsi
it should
cmpq $32, %rsi
you accessing junk in memory after .quad 31
, sticking r11 @ movq (%r9,%rax), %r11
Comments
Post a Comment