Page 1 of 1
Separating bits
Posted: Mon Dec 15, 2008 8:08 am
by renovatio
Hello, thanks for reading my topic. Let's suppose I have a byte, and I want to copy each bit to a different variable. How can I do this in assembly?
Example:
Byte =10010011
Bit1 = 1
Bit2 = 1
Bit3 = 0
Bit4 = 0
Bit5 = 1
Bit6 = 0
Bit7 = 0
Bit8 = 1
Thanks in advance.
Re: Separating bits
Posted: Mon Dec 15, 2008 8:16 am
by AJ
Hi,
I would use a combination of the AND operator and right rotations. There may be a more optimised way than that, though.
Cheers,
Adam
Re: Separating bits
Posted: Mon Dec 15, 2008 9:08 am
by renovatio
Thanks for your help AJ. I found a better way with the BT instruction:
mov ax, 10010011
bt ax, 2
CF = second bit from ax
Re: Separating bits
Posted: Mon Dec 15, 2008 9:43 am
by Troy Martin
Nifty, I wonder if that's in the 186 instruction set, that way I can use it in my kernel!
Let me guess: you're implementing printing a binary number?
EDIT: Awww, damn, 386.
Re: Separating bits
Posted: Mon Dec 15, 2008 10:38 am
by renovatio
Not, i'm writting a fat12 driver, and i want to know the attributes of a file...
. The instruction is able to use since 386 (sorry for my english
)
Re: Separating bits
Posted: Mon Dec 15, 2008 11:28 am
by Combuster
hmm 80186?
Code: Select all
mov cx, 8
mov dl, BYTE
lea di, [destination]
.loop:
ror dl, 1
sbb al, al
neg al
stosb
loop .loop
or something like that
Re: Separating bits
Posted: Mon Dec 15, 2008 11:55 am
by Owen
The and/shift method actually executes faster than the bit instructions on anything newer than a 80486 IIRC
Re: Separating bits
Posted: Mon Dec 15, 2008 1:01 pm
by DeletedAccount
Hi,
AND is called the bit tester
.
OR is called the bit setter
Regards
Shrek
Re: Separating bits
Posted: Tue Dec 16, 2008 10:33 pm
by Troy Martin
Combuster: isn't lea reg,[label] slower and bigger than mov reg,label? There are less calculations to do, IIRC.
OP: When I did my hex printing functions, I did ror ax,4 and then shr ax,4 to isolate the lower nybble. Technically, you could use that in conjunction with more shifting to isolate a single bit. That's how I'd do it with 186 instructions.
Re: Separating bits
Posted: Wed Dec 17, 2008 5:58 am
by madeofstaples
Troy Martin wrote:Combuster: isn't lea reg,[label] slower and bigger than mov reg,label? There are less calculations to do, IIRC.
OP: When I did my hex printing functions, I did ror ax,4 and then shr ax,4 to isolate the lower nybble. Technically, you could use that in conjunction with more shifting to isolate a single bit. That's how I'd do it with 186 instructions.
If bits frequently need to be tested, it may be helpful to implement
de Bruijn Sequence. If you are sure of your target architecture, then there's no need to calculate a de Bruijn sequence and build an appropriate hash table at run-time.
Re: Separating bits
Posted: Wed Dec 17, 2008 5:12 pm
by Combuster
LEA is fast. One clock on 486/586, i.e. the same as a regular move.
The point is, I pumped out a method that should work, used LEA to make it obvious that an address is to be used, to give one possible approach. If you're worried about clocks cycles I'd start with loop unrolling and lookup tables instead of shifts.