achaleus
Member level 5
I am experimenting on modifying assembly by calling c library from assembly, I ran into segmentation fault
exp1.c contains
---------------------------------------------------------------------
Any ideas why I got segmentation fault and how to make this working. I should only able to touch assembly
exp1.c contains
Code:
#include <stdio.h>
double *a,*b,*c;
int main()
{
double a_d = 1.1;
double b_d = 2.1;
double c_d;
c = &c_d;
a = &a_d;
b = &b_d;
*c = (*a + *b);
printf("\n%lf",*c);
}
add.c contains
double add(double a, double b){
double c = a*b;
return c;
}
I have created library add.c with name libtemp.a
the corresponding assembly code for exp1.c is
.file "exp1.c"
.comm a,8,8
.comm b,8,8
.comm c,8,8
.section .rodata
.LC2:
.string "\n%lf"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $48, %rsp
movabsq $4607632778762754458, %rax
movq %rax, -24(%rbp)
movabsq $4611911198408756429, %rax
movq %rax, -16(%rbp)
leaq -8(%rbp), %rax
movq %rax, c(%rip)
leaq -24(%rbp), %rax
movq %rax, a(%rip)
leaq -16(%rbp), %rax
movq %rax, b(%rip)
movq c(%rip), %rax
movq a(%rip), %rdx
movsd (%rdx), %xmm1
movq b(%rip), %rdx
movsd (%rdx), %xmm0
#addsd %xmm1, %xmm0 [B]# commented actual addition[/B]
call add [B] # added my library call here[/B]
movsd %xmm0, (%rax)
movq c(%rip), %rax
movq (%rax), %rax
movq %rax, -40(%rbp)
movsd -40(%rbp), %xmm0
movl $.LC2, %edi
movl $1, %eax
call printf
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
.section .note.GNU-stack,"",@progbits
---------------------------------------------------------------------
Any ideas why I got segmentation fault and how to make this working. I should only able to touch assembly
Last edited by a moderator: