为什么printf似乎要求主要功能推动R4以打印浮子?

发布于 2025-02-07 05:23:12 字数 1021 浏览 3 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2025-02-14 05:23:12

从评论:

我认为此问题描述了您的问题:github.com/zephyrproject-rtos/zephyr/issues/2108堆栈需要将功能输入时的8个字节边界对齐。推动一个32位寄存器只能给您4个字节对齐

from comments:

I think this issue describes your problem: github.com/zephyrproject-rtos/zephyr/issues/2108 The stack needs to be aligned to 8 byte boundaries on function entry. Pushing one 32bit register gives you only 4 byte alignment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文