为什么printf似乎要求主要功能推动R4以打印浮子?
在我的Raspberry-pi上,我有两个文件: main.s 和 file.c 。
file.c 包含一个称为 foo 的函数,该功能打印了浮点数1.4
#include <stdio.h>
void foo(){
double a = 1.4;
printf("%f\n", a);
}
main.s 包含一个称为的主功能foo 并返回0
.global main
main:
push {lr}
bl foo
mov r0, #0
pop {pc}
时,当我编译并运行程序时,它会打印一个垃圾数字。
$ gcc file.c main.s -o a
$ ./a
190359901426432118378104432082065795719817104605055420380549740704558175549767996835670175700923114787186017481619470916538605140186901692001326762660598893470884230006875222134524739584.000000
但是,如果我将 main.s 更改
.global main
main:
push {r4, lr}
bl foo
mov r0, #0
pop {r4, pc}
为预期,
$ gcc file.c main.s -o b
$ ./b
1.400000
为什么主要功能需要推动R4才能使printf正确工作?
(编译C中编写的主要功能会产生有效的组件,这就是我找到它的方式。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从评论:
from comments: