On my raspberry-pi I have two files: main.s and file.c.
file.c contains a function called foo that prints the floating-point number 1.4
#include <stdio.h>
void foo(){
double a = 1.4;
printf("%f\n", a);
}
main.s contains a main-function that calls foo and returns 0
.global main
main:
push {lr}
bl foo
mov r0, #0
pop {pc}
When I compile and run the program it prints a garbage decimal number.
$ gcc file.c main.s -o a
$ ./a
190359901426432118378104432082065795719817104605055420380549740704558175549767996835670175700923114787186017481619470916538605140186901692001326762660598893470884230006875222134524739584.000000
However if I change main.s to
.global main
main:
push {r4, lr}
bl foo
mov r0, #0
pop {r4, pc}
it works as expected
$ gcc file.c main.s -o b
$ ./b
1.400000
Why does the main-function need to push r4 for printf to work correctly?
(Compiling the main-function written in C produces the assembly that works, which is how I found it.)