assembly code:
section .text
global calculateRoot
extern printFloat
calculateRoot:
push rbp ; save Base Pointer (bp) original value
mov rbp, rsp ; use base pointer to access stack contents
sub rsp, 55 ; allocating bytes in stack for local variables
call printFloat
mov rsp, rbp ; closing stack
pop rbp ; resotre rbp
ret
c code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void printFloat();
extern void calculateRoot(double num);
int main(){
double num = 43432432.23532534534543534;
calculateRoot(num);
printFloat(num);
return 0;
}
void printFloat(){
double h = 4332432525.324324;
printf("\nFloating Point Number %lf\n",h);
}
The above code produces a segmentation fault. i'm trying to understand why. As i tried to reproduce the problem in different ways, i found out that the seg fault only occurres when i try to print a float. It does not happen with int for example. Furthermore, i found out that different amount in the instruction "sub rsp,55" can fix the problem, i.e if it put 48 or 16 for example there will be no seg fault. Is there something i'm missing here ? Thanks a lot for every answer.
n.p - this code is not meant for anything. i created it when i found out strange things happen in my program. So this code was meant to found my mistake in my real program. And another thing i noticed, when i use 8 in "sub rsp,55" it creates a seg fault, so i created another function in assembly and called it from calculateRoot, in this function i made another "sub rsp,8" and it eliminated the seg fault(16 works when it is used in "sub rsp, 55").