在《深入理解计算机系统中》练习题3.20中有一个问题实在想不通。
负数的算数右移k位在汇编代码中为什么要加上一个2的k次方-1的偏移量?
直接算数右移不就可以得到正确结果吗?
是必须这样吗?
请大神指点一下!谢谢!
#include<stdio.h>
long arith(long x);
int main(void)
{
long a=-16;
long b;
b=arith(a);
return 0;
}
long arith(long x)
{
return x/8;
}
这是gcc编译的汇编。
main:
.LFB23:
.cfi_startproc
movl $0, %eax
ret
.cfi_endproc
.LFE23:
.size main, .-main
.globl arith
.type arith, @function
arith:
.LFB24:
.cfi_startproc
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
ret
.cfi_endproc