我有一个关于寄存器在一些基本操作中的使用的小问题。实际上,我已经看过在 XOR 或 AND 等操作期间生成的汇编代码,这些代码很容易理解。实际上,如果我们考虑 a = b & c,这将分 3 个步骤进行翻译:
- b 被移动到 %rax
- %rax = %rax + c
- %rax 被移到了
请注意,a、b 和 c 是无符号长变量。如果将加法替换为 XOR 或 OR,则此翻译也可用。无论如何,我检查了轮类是否也是这种情况,我发现了一件奇怪的事情:事实上,a = b << c 翻译如下
- b 被移动到 %rax
- c 被移动到 %rcx
- %rax = %rax <<(shlq) %cl
- %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/