Separating bits

Programming, for all ages and all languages.
Post Reply
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Separating bits

Post 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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Separating bits

Post 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
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: Separating bits

Post 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
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Separating bits

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
renovatio
Member
Member
Posts: 57
Joined: Fri May 23, 2008 5:13 am

Re: Separating bits

Post 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 :))
User avatar
Combuster
Member
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

Post 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
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Separating bits

Post by Owen »

The and/shift method actually executes faster than the bit instructions on anything newer than a 80486 IIRC
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Separating bits

Post by DeletedAccount »

Hi,
AND is called the bit tester :idea: .

OR is called the bit setter :idea:
Regards
Shrek
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Separating bits

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
madeofstaples
Member
Member
Posts: 204
Joined: Thu Apr 12, 2007 8:15 am
Location: Michigan

Re: Separating bits

Post 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.
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.
User avatar
Combuster
Member
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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply