1
votes

I want to implement with inline assembler a function that take the value of eax register and put the result of xor of each 4-bit from eax into ebx, I want to implement it using left shift.

Suppose that the value of

eax value is : 1101.1010.0010.0011
ebx value is : 0

I want to do a left shift of 4-bit from eax and xor the lost value with ebx value:

So the result must be:

eax : 1010.0010.0011.0000
ebx : 1101

Next:

eax : 0010.0011.0000.0000
ebx : 1101 xor 0010 = 1111

Next:

eax : 0011.0000.0000.0000
ebx : 1111 xor 0010 = 1101

Next:

 eax : 0000.0000.0000.0000
 ebx : 1101 xor 0011 = 1110

How to get the lost value?

1
Oh come on. You know this has absolutely zero to do whatsoever with either C or C++. Don't tag it as such. People following those tags do so because that's what's interesting to them. You don't have any sort of right to trick those people into reading your question. - user743382
ok sorry, take it easy ! - SingaCpp

1 Answers

0
votes

By saving it beforehand.

For example:

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

All those ands are not really necessary, since the upper bits don't interact with any other bits, you can just ignore them during the calculation and discard them all at once in the end with a single and (I leave that as exercise for the reader).

Even better, since xor is associative, we can rearrange the order to our advantage, taking several pairs in parallel. For example like this:

; 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