Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 装配快速除以2_Assembly_Division - Fatal编程技术网

Assembly 装配快速除以2

Assembly 装配快速除以2,assembly,division,Assembly,Division,在汇编中,有没有比下面示例中的方法更快的带符号除以2的方法 ... mov ecx, 2 idiv ecx push eax #push the result ... 当然可以: sar操作码与shr的不同之处在于,最高有效位(符号)保留在sar中,并在shr中设置为0。Wikipedia上的页面在一般上下文中显示了有关此操作的更多详细信息 请注意,在2的补码机(x86)上,这实际上计算下限(eax/2)。特别是,这意味着对于整数x: 对于x=0,结果为0 对于x>0,结果是地板(x/2)

在汇编中,有没有比下面示例中的方法更快的带符号除以2的方法

...
mov ecx, 2
idiv ecx
push eax #push the result
...
当然可以:

sar
操作码与
shr
的不同之处在于,最高有效位(符号)保留在
sar
中,并在
shr
中设置为0。Wikipedia上的页面在一般上下文中显示了有关此操作的更多详细信息

请注意,在2的补码机(x86)上,这实际上计算
下限(eax/2)
。特别是,这意味着对于整数x:

  • 对于x=0,结果为0
  • 对于x>0,结果是地板(x/2)
  • 对于x<0,结果也是floor(x/2)或-ceil(-x/2)

后一个结果给出的结果可能出乎意料。例如,-3SAR 1导致-2,而不是-1。另一方面,3 sar 1的结果是1。

准确地说。正如格雷格指出的,向右移动1等于除以2:-)
sar eax, 1