为什么 %eax 在调用 printf 之前归零? [英] Why is %eax zeroed before a call to 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 之前归零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!