At 09:31 AM 2/20/98 -0400, you wrote:
<snip>
{Quote hidden}>Bitmasks work almost like bit pointers. The bitmask 00000001b is the "bit
>pointer" to bit 0. 00001000b points to bit 3. Say your "bit pointer" is
>in the file register "BitP". "bsf BitP" and "bcf BitP" can be simulated
>with these little functions.
>
>bsfbitp ; subroutine to set the bit in portb pointed to by BitP
> movf BitP,w
> iorwf portb,f
> return
>
>bcfbitp ; subroutine to clear the bit in portb pointed to by BitP
> comf BitP,w
> andwf portb,f
> return
>
>Use rotates to increment and decrement these "bit pointers". If the carry
>bit gets set, you've reached the end of the byte.
>
>I'm sure someone on the list has a clever way of converting a number in
>the range 0 to 7 into a bitmask with one bit set and vice versa.
>
>--
>
TakeThisOuTpaulhEraseMEspam_OUThamjudo.com http://www.hamjudo.com
>The April 97 WebSight magazine describes me as "(presumably) normal".
>
I have never really participated in the discussions about neat programming
algorithims but I have always been fascinated by them. Since the challenge
has been issued for a "clever" way of creating a bitmask, I decided to give
it a try. The methods given here are probably not the most efficient in
size or speed, but I am looking to improve my skills at programming tight
algorithims so I thought I would throw them out here for critique.
Here is the fastest routene I could come up with to convert a register
INDEX to a bitmask in BitP
This routene ignores the upper five bits of INDEX, and sets the bit in BitP
which corresponds to the value of the lower three bits of INDEX.
It preserves W and needs no other registers besides INDEX and BitP.
It is 13 words long and takes 12 cycles for INDEX[bits 0,1,2] < 4 and 13
cycles otherwise.
bcf STATUS,C
clrf BitP,F
bsf BitP,0
btfss INDEX,2
goto NOT4
clrf BitP,F
bsf BitP,4
NOT4 btfsc INDEX,1
rlf BitP,F
btfsc INDEX,1
rlf BitP,F
btfsc INDEX,0
rlf BitP,F
Here's the smallest routene:
It looks at the whole INDEX register, sets the bit in BitP which
corresponds to the value in INDEX. It trashes W, also needs no additional
registers, and is only 8 words long. It takes 8 cycles for INDEX < 2 and 8
+ 4*(INDEX-1) for INDEX > 1
bcf STATUS,C
movlw 1
movwf BitP
addwf INDEX,F
ALOOP rlf BitP,F
decfsz INDEX,F
goto ALOOP
rrf BitP,F
DISCLAIMER: Anyone can use these routenes for anything, however, I have not
tested them.
Sean
+--------------------------------+
| Sean Breheny |
| Amateur Radio Callsign: KA3YXM |
| Electrical Engineering Student |
+--------------------------------+
Fight injustice, please look at
http://homepages.enterprise.net/toolan/joanandrews/
Personal page: http://www.people.cornell.edu/pages/shb7
RemoveMEshb7TakeThisOuTcornell.edu
Phone(USA): (607) 253-0315