still me. I am following the sample code given in a book but I got a segmentation fault error when linking extern asm file with cpp file
Could someone tell me what the issue is:
global _integer_add
; section .text i ran the code with and without "section .text"
_integer_add:
; the parameters are passed like this
; rdi, rsi, rdx, r8, r9
; a , b , c , d
; I think i need to align rsp to 16 multiple
sub rsp, 8
; mov a into rax
mov rax, rdi
; sum a and b together
add rax, rsi
; subtract from this sum c
sub rax, rdx
; sum d to this quantity
add rax, r8
ret
this is the assembly file. I tried to run both with and without the instruction sub rsp, 8. (technically I found this written in the form sub 8, rsp in a github repo, tried that too without any luck). Maybe I am getting wrong the mov add instruction. But i found this thanks to a previous question
before call the stack pointer must be aligned by a multiple of 16 bytes after call pushes the return value to the stack.This often fails by default without explicitly changing %rsp because call will store the 8 byte return address on the stack, so you usually need to do:
sub 8, rsp
call f
often this results in a segmentation fault. In case you need this too
extern "C" int integer_add(int a, int b, int c, int d);
solution
I actually found the problem. int is 32 bit so it worked using 32 bit registers (I also left out all the rsp register business):_integer_add
mov eax, edi
add eax, esi
sub eax, edx
add eax, r8d
ret
just in case someone else makes a rookie mistake as I did :)