[英]Understanding assembly code when converting from unsigned char to long long
Consider code c.c
考虑代码
c.c
void f(unsigned char *a, long long *b)
{
*b = (long long)*a;
}
Compile it with编译它
$ gcc -Og -S c.c
where在哪里
$ gcc --version
gcc (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 10.2.0
and my machine is a 64-bit Windows 10.我的机器是 64 位 Windows 10。
Among other lines, I get the assembly code as follows除其他外,我得到如下汇编代码
01 movzbl (%rcx), %eax
02 movq %rax, (%rdx)
My question is: Why isn't the first line written in this way我的问题是:为什么第一行不是这样写的
01 movzbq (%rcx), %rax
What if the higher 32 bits of %rax
originally had some non-zero bits, and were not set to zero after movzbl (%rcx), %eax
?如果
%rax
的高 32 位最初有一些非零位,并且在movzbl (%rcx), %eax
之后没有设置为零怎么办? Won't these non-zero bits (if any) get copied to (%rdx)
by movq %rax, (%rdx)
?这些非零位(如果有)不会被
movq %rax, (%rdx)
复制到(%rdx)
rdx) 吗?
A follow-up question is: Even the above concern is unneeded, still, why isn't the first line written in this way一个后续问题是:即使上面的关注也不需要,仍然,为什么第一行不是这样写的
01 movzbq (%rcx), %rax
ie governed by which rule the translation from C to assembly code is done in the given way?即从 C 到汇编代码的转换受哪个规则的约束以给定的方式完成?
(I have some knowledge with C but am new to assembly code.) (我对 C 有一些了解,但对汇编代码不熟悉。)
Update: Would like to make some clarification after I read the comments (appreciate all of them).更新:想在我阅读评论后做出一些澄清(感谢所有评论)。 A comment says the function is unnecessary, and I may just do that assignment.
一条评论说 function 是不必要的,我可以做那个任务。 That is right.
没错。 As another comment rightly puts, this is a pared-down example.
正如另一条评论正确地指出的那样,这是一个精简的例子。 What I want to understand is simply why the C-to-assembly translation happens this way when casting a
unsigned char
to long long
.我想了解的只是为什么在将
unsigned char
转换为long long
时会以这种方式发生 C 到程序集的转换。
movzbl
1) zero extends to 32 bit ('z'), and 2) zero extends to 64 bit (32 bit operands are implicitly “zero extended”) for %eax
. movzbl
1) 零扩展到 32 位 ('z'),以及 2) 零扩展到 64 位(32 位操作数隐含地“零扩展”)用于%eax
。
32-bit instruction movzbl
's encoding is shorter than the 64-bit instruction movzbq
's encoding. 32 位指令
movzbl
的编码比 64 位指令movzbq
的编码短。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.