Separating bits
Separating bits
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.
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
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
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
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
mov ax, 10010011
bt ax, 2
CF = second bit from ax
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: Separating bits
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.
Let me guess: you're implementing printing a binary number?
EDIT: Awww, damn, 386.
Re: Separating bits
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 )
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Separating bits
hmm 80186?
or something like that
Code: Select all
mov cx, 8
mov dl, BYTE
lea di, [destination]
.loop:
ror dl, 1
sbb al, al
neg al
stosb
loop .loop
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Separating bits
The and/shift method actually executes faster than the bit instructions on anything newer than a 80486 IIRC
-
- Member
- Posts: 566
- Joined: Tue Jun 20, 2006 9:17 am
Re: Separating bits
Hi,
AND is called the bit tester .
OR is called the bit setter
Regards
Shrek
AND is called the bit tester .
OR is called the bit setter
Regards
Shrek
- Troy Martin
- Member
- Posts: 1686
- Joined: Fri Apr 18, 2008 4:40 pm
- Location: Langley, Vancouver, BC, Canada
- Contact:
Re: Separating bits
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.
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.
-
- Member
- Posts: 204
- Joined: Thu Apr 12, 2007 8:15 am
- Location: Michigan
Re: Separating bits
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.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.
Some people are offended by the verifiable truth; such people tend to remain blissfully unencumbered by fact.
If you are one of these people, my posts may cause considerable discomfort. Read at your own risk.
If you are one of these people, my posts may cause considerable discomfort. Read at your own risk.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Separating bits
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.
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.