LZ: RE: TI variable bit?


[Prev][Next][Index][Thread]

LZ: RE: TI variable bit?



Are you sure that is how you reference the bit?  If that is so, then you
would need an extra routine to handle a bit at 0.  If, however, the bits
are 0 1 2 3 4 5 6 7, then

 GetBit:
   inc  b		;Number of rotations to make
   ld   a, (de)	;Load Byte to test
 rotate:
   rla			;Rotation a Left and put leftmost bit in Carry Flag
   dec  b		;Dec b sets Zero flag, but not Carry Flag
   ret  Z		;Return if b is 0
   jr rotate	;Otherwise do again

should work.  However if your bit reference is 1 2 3 4 5 6 7 8 then you
can omit the "inc  b" line.

> 
> Help! I can't figure out what's wrong with this routine.
> Ok, let's say I need to access a variable bit in indirect memory.
> I have the variable in register b, except it's not your regular way of
> numbering the bits.
> 
>                 MSBit         LSBit
> byte:           0 1 0 0 1 1 0 1
> regular way:    7 6 5 4 3 2 1 0
> my way:         1 2 3 4 5 6 7 0
> 
> Would something like this work?
> 
> ;input: b=no. of bit
> ;       (de)=byte
> ;output: copy the bit to the carry flag
> GetBit:
>         xor     a               ;same as ld a,0
>         cp      b               ;b is the number of the bit
>         ld      a,(de)          ;the byte to test is at (de)
>         jr      z,endrotate     ;if b=0, goto endrotate
> rotate:
>         rlca                    ;get the important bit into bit 0
>         djnz    rotate
> endrotate:
>         srl     a               ;copy the LSBit to the carry flag
>         ret
> 
> Thanks,
> 
> --
> Terry Peng
> email-  tpeng@geocities.com
> web-    http://www.geocities.com/SiliconValley/Bay/5104/index.html