gpt4 book ai didi

c - 为什么在将 C 向下转换从 unsigned int 转换为 unsigned char 时,movl 比 movb 更受欢迎?

转载 作者:行者123 更新时间:2023-12-04 04:25:01 25 4
gpt4 key购买 nike

考虑向下类型转换的精简示例 unsignedunsigned char ,

void unsigned_to_unsigned_char(unsigned *sp, unsigned char *dp)
{
*dp = (unsigned char)*sp;
}
上面的 C 代码被翻译成汇编代码 gcc -Og -S作为
movl    (%rdi), %eax
movb %al, (%rsi)
出于什么原因,C 到汇编的翻译不是如下所示?
movb    (%rdi), %al
movb %al, (%rsi)
是因为这不正确,还是因为 movlmovb 更传统,或更短的编码?

最佳答案

当新的低字节与相应 32/64 位寄存器的旧高字节合并时,写入 8 位 x86 寄存器可能会导致额外的合并微操作。这也可能导致对寄存器先前值的意外数据依赖性。
出于这个原因,通常只写入 x86 上通用寄存器的 32/64 位变体通常是个好主意。

关于c - 为什么在将 C 向下转换从 unsigned int 转换为 unsigned char 时,movl 比 movb 更受欢迎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68420792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com