I am trying to decipher some assembly code that involves multiple left rotations on an 8-bit binary number.
For reference, the code is:
lab: rol dl,1
rol dl,1
dec ecx
jnz lab
The dec
and jnz
isn't an issue, but is there to show that the 2 rol
s are executed several times.
What I am trying to do is figure out a mathematical equivalent of this code, such as a formula. I'm certainly not looking for a complete formula to tell me the whole code, but I would like to know if there is a formula that gives the equivalent (in denary) of a single left rotation.
I've tried figuring this out with a couple of different numbers, but cannot see a link between the two results. For example: if the start number is 115 it comes out as 220, but if the start number开发者_运维百科 is 99 it comes out as 216.
Given your sample results, I assume we are treating the 8-bit quantity as unsigned.
The 7 low-order bits are shifted left, multiplying that part of the number by 2; and the high-order bit is swapped around to the beginning.
Thus, (x % 128) * 2 + (x / 128)
, using the usual integer div/mod operators.
Shifting a byte containing number X by one bit (position) left is equal to multiplying the number X by 2:
x << 1 <==> x = x * 2
精彩评论