I try to implement the C sqrt function in Nasm elf64, it works correctly without argument (with the value of "a" define in the function), but when I try to pass an argument to this function ,the code return an error "Stopped reason: SIGFPE".
Here's my code
The c function
int le_sqrt(int a) {
int n=1;
int number = a;
for (int i=0; i<10; i ) {
n=(n number/n)/2;
}
return n;
}
The nasm program
bits 64
global _start
extern le_sqrt
_start:
mov rbp,9 ;argument
push rbp ;same argument push
call le_sqrt ; my c function
mov rax,60 ;exit program
mov rsi,0
syscall
CodePudding user response:
If you want to call le_sqrt(9)
with System V AMD64 ABI calling convention, do this:
_start:
mov rdi,9
call le_sqrt
CodePudding user response:
SIGFPE
usually happens when you divide a number by 0
. In your assembly program, you are using mov rbp, 9
for passing an argument to c function, which might be wrong in your case. It becomes obvious since you're getting SIGFPE
. See Microsoft calling conventions and System V ABI calling conventions (for 64-bit). For 32-bit, follow these calling conventions.