有没有人知道如何镜像NASM / 8086中的位?
以下是一个例子:
镜像后 10100001
=> 10000101
问题更复杂,因为我应该只反映寄存器的奇数位:
这意味着:
10100001 => 00001011
76543210 => 76543210
即。 1< => 7和3< => 5
答案 0 :(得分:4)
明显的算法似乎是:
mask = 01010101...01010101
)temp = src AND mask
)src = src AND NOT mask
)src = src SHL 1
)src = src OR temp
)当然如果这只是8位值,那么你可以使用预先计算的256个元素LUT。
答案 1 :(得分:1)
我终于找到了解决问题的另一种方法。你的答案非常好,但是当你只有几分钟时间解决问题时,你昨天与我联系的算法很难实现。我没有说明,但这个作业实际上是一个考试问题,时间有限。
我认为斯坦福大学的算法更优化但实施起来有点困难,或者我错了?!
所以,这是我的想法:
MOV EAX,2862962005 ; Register where the odd bits must be mirrored
; Initialised with an random Number as example
; Result should be putted again in EAX
ODD_MIRRORED:
PUSH EBX ;put the values of the other registers on to the Stack
PUSH ECX ;probably this registers hold valuable datas inside
PUSH EDX
MOV EDX,0 ;EDX is counter, form 0 to 31
MOV EBX,0 ;EBX holds the inverted odd bits during the process
;For Auxiliary use only
iteration:
ROR EAX,1 ;EVEN VALUE? JUMP: EAX rotated to the right
ROL EBX,1 ; EBX rotated to the left (for mirroring)
INC EDX ; counter incremented
MOV ECX,0x00000001 ;ODD VALUE? USE mask and extract only one odd value
;from EAX at a given time
AND ECX,EAX
OR EBX,ECX ;put the value in EBX
ROR EAX,1 ;EAX rotated to the right
ROL EBX,1 ;EBX rotated to the left (for mirroring)
INC EDX ;counter incremented again
CMP EDX,31
JGE end ;if counter is greater or equal 31 jump at the end of the program
JMP iteration
end:
;now EAX was rotated to the initial position
AND EAX,0x55555555 ;we want only just to get away from old odd values with
;the mask 0x55555555 (0101), which is deleting the odd values
OR EAX,EBX ;finally we transfer the mirrored odd bits into EAX from the
;auxiliary variable EBX
POP EDX ;restore EBX,ECX,EDX
POP ECX
POP EBX
RET
我在NASM / 8086汇编程序中使用各种数字验证了此解决方案,并且可以正常工作! :P