assembly - 左移操作期间使用的寄存器

标签 assembly x86 x86-64 bit-shift

我有一个关于寄存器在一些基本操作中的使用的小问题。实际上,我已经看过在 XOR 或 AND 等操作期间生成的汇编代码,这些代码很容易理解。实际上,如果我们考虑 a = b & c,这将分 3 个步骤进行翻译:

  1. b 被移动到 %rax
  2. %rax = %rax + c
  3. %rax 被移到了

请注意,a、b 和 c 是无符号长变量。如果将加法替换为 XOR 或 OR,则此翻译也可用。无论如何,我检查了轮类是否也是这种情况,我发现了一件奇怪的事情:事实上,a = b << c 翻译如下

  1. b 被移动到 %rax
  2. c 被移动到 %rcx
  3. %rax = %rax <<(shlq) %cl
  4. %rax 被移到了

我不太确定我是否真的理解第二步和第三步。我想这是因为 %rax (b) 不能移动超过 63,否则,结果显然是 0。看起来 %cl 是一个 8 位寄存器,所以我认为这是一个快速的方法只选择有用的位而不是 %rcx 中的 64 位。对吗?

谢谢

最佳答案

这就是 shl 的工作原理。
来自英特尔手册 2B:

Shifts the bits in the first operand (destination operand) to the left or right by the number of bits specified in the second operand (count operand). Bits shifted beyond the destination operand boundary are first shifted into the CF flag, then discarded. At the end of the shift operation, the CF flag contains the last bit shifted out of the destination operand.

The destination operand can be a register or a memory location. The count operand can be an immediate value or the CL register. The count is masked to 5 bits (or 6 bits if in 64-bit mode and REX.W is used). The count range is limited to 0 to 31 (or 63 if 64-bit mode and REX.W is used). A special opcode encoding is provided for a count of 1.

可变长度移位必须使用cl

关于assembly - 左移操作期间使用的寄存器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37510116/

相关文章:

linux汇编反转一个字符串

assembly - ia32/ia64上的ADC和ADCX指令之间有什么区别?

assembly - x64 程序集中的堆栈对齐

了解 "volatile"关键字和比较的工作原理

assembly - ASM 循环更快?

linux - 从未对齐 RSP 的函数调用时,glibc scanf 出现段错误

c - 为什么编译器会生成这段代码?

c++ - 如何检查CPUID(EAX=14H,ECX=0)?

x86-64 - x86_64 是否与指令中的 aarch64 等效?

c++ - 如何在arm可执行文件中获取printf函数的地址