【问题标题】:Get left shift lost value (inline assembler)获取左移丢失值(内联汇编器)
【发布时间】:2015-02-07 12:01:20
【问题描述】:

我想用内联汇编器实现一个函数,它取eax寄存器的值并将eax中每个4位的xor的结果放入ebx,我想用left实现它换班。

假设

eax value is : 1101.1010.0010.0011
ebx value is : 0

我想从eaxxorebx 值对丢失的值进行4 位左移:

所以结果一定是:

eax : 1010.0010.0011.0000
ebx : 1101

下一步:

eax : 0010.0011.0000.0000
ebx : 1101 xor 0010 = 1111

下一步:

eax : 0011.0000.0000.0000
ebx : 1111 xor 0010 = 1101

下一步:

 eax : 0000.0000.0000.0000
 ebx : 1101 xor 0011 = 1110

如何获得丢失的价值?

【问题讨论】:

  • 哦,来吧。你知道这与 C 或 C++ 无关。不要这样标记它。关注这些标签的人这样做是因为这对他们来说很有趣。你没有任何权利欺骗那些人阅读你的问题。
  • 好吧对不起,放轻松!

标签: assembly x86


【解决方案1】:

提前保存。

例如:

mov ebx, eax
shr eax, 4
and ebx, 15 ; this is the "lost" value
mov edx, eax
shr eax, 4
and edx, 15
xor ebx, edx
; etc

所有这些ands 都不是必需的,因为高位不与任何其他位交互,您可以在计算过程中忽略它们,最后用单个and 一次性丢弃它们(我把它留给读者练习)。

更好的是,由于 xor 是关联的,我们可以重新排列顺序以发挥我们的优势,并行处理几对。比如这样:

; first step
mov ebx, eax
shr eax, 8
xor eax, ebx  ; xor the first nibble with the third, and the second with the fourth
; second step
mov ebx, eax
shr eax, 4
xor ebx, eax  ; (first ^ third) ^ (second ^ fourth) and some leftover junk
; clean up
and ebx, 15   ; remove junk

【讨论】:

  • @SingaCpp 请尊重 SO 礼仪并接受此答案。
猜你喜欢
  • 1970-01-01
  • 2013-07-23
  • 2019-05-11
  • 2015-07-01
  • 2012-10-20
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多