为什么 %eax 在调用 printf 之前归零?

编程入门 行业动态 更新时间:2024-10-17 17:16:37
本文介绍了为什么 %eax 在调用 printf 之前归零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用一些 x86.我正在使用 gcc -S -O0 在 64 位 mac 上进行编译.

I am trying to pick up a little x86. I am compiling on a 64bit mac with gcc -S -O0.

C 代码:

printf("%d", 1);

输出:

movl $1, %esi leaq LC0(%rip), %rdi movl $0, %eax ; WHY? call _printf

我不明白为什么在调用 'printf' 之前 %eax 被清除为 0.由于 printf 返回打印到 %eax 的字符数,我最好的猜测是将其归零以准备 printf 但我会假设printf 必须负责准备好它.另外,相比之下,如果我调用自己的函数 int testproc(int p1),gcc 认为不需要准备 %eax.所以我想知道为什么 gcc 对 printf 和 testproc 的处理方式不同.

I do not understand why %eax is cleared to 0 before 'printf' is called. Since printf returns the number of characters printed to %eax my best guess it is zeroed out to prepare it for printf but I would have assumed that printf would have to be responsible for getting it ready. Also, in contrast, if I call my own function int testproc(int p1), gcc sees no need to prepare %eax. So I wonder why gcc treats printf and testproc differently.

推荐答案

来自 x86_64 System V ABI 寄存器使用表:

From the x86_64 System V ABI register usage table:

  • %rax 临时寄存器;带有可变参数传递有关向量数量的信息使用的寄存器;第一个返回寄存器...
  • %rax       temporary register; with variable arguments passes information about the number of vector registers used; 1st return register ...
  • printf 是一个带可变参数的函数,使用的向量寄存器数量为零.

    printf is a function with variable arguments, and the number of vector registers used is zero.

    注意printf必须只检查%al,因为允许调用者在%rax的高字节中留下垃圾.(不过,xor %eax,%eax 是将 %al 归零的最有效方法)

    Note that printf must check only %al, because the caller is allowed to leave garbage in the higher bytes of %rax. (Still, xor %eax,%eax is the most efficient way to zero %al)

    见这个问答 和 x86 标签wiki 了解更多详细信息,或者如果上述链接过时,请访问最新的 ABI 链接.

    See the this Q&A and the x86 tag wiki for more details, or for up-to-date ABI links if the above link is stale.

更多推荐

为什么 %eax 在调用 printf 之前归零?

本文发布于:2023-11-13 02:16:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:eax   printf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!