Heya .. needed a procedure which primarily tests whether some data has any bits that are zero (ie is not all FF bytes) and secondly returns the BIT INDEX of the first zero bit detected, if any.
I'm not even sure it's right, but it seems to work fine.
Can anyone offer an optimisation?



AnyBitsClear proc uses edi pImage, cblen
mov edi,pImage
xor ebx,ebx
xor eax,eax
.while ebx<cblen
bt dword ptr [edi], ebx ;the destination bit indexed by the source value is copied into the CarryFlag
jnc gotcha
inc ebx
.endw
return -1
gotcha:
return ebx
AnyBitsClear endp
Posted on 2004-08-31 22:05:41 by Homer
Use bsf or bsr. There is no need for the loop.
Posted on 2004-09-01 02:33:45 by roticv
d'oh !
Thanks - I just couldn't remember it :)

Oh dear...
these opcodes scan for the first bit which is SET
I was after the first bit which is CLEAR.

I'm still in a pickle.
It looks like if I wanna use this I have to reverse all my existing logic :(
Posted on 2004-09-01 02:57:35 by Homer
It doesn't take much imagination to produce the following routine:
AnyBitsClear:

push esi
push ebx
mov esi,[esp+12]
mov edx,[esp+16]
mov ebx,edx
back:
lodsd
not eax
jz next
bsf eax,eax
sub eax,edx
jae notfound
add eax,ebx
pop ebx
pop esi
ret 8
next:
sub edx,32
ja back
notfound:
stc
pop ebx
pop esi
sbb eax,eax
ret 8
Posted on 2004-09-01 04:10:29 by Sephiroth3
d'oh !
Thanks - I just couldn't remember it :)

Oh dear...
these opcodes scan for the first bit which is SET
I was after the first bit which is CLEAR.

I'm still in a pickle.
It looks like if I wanna use this I have to reverse all my existing logic :(


NOT the data first then. :-D
Posted on 2004-09-01 06:59:00 by roticv
ahh.. it's just an array of 1's and 0's in this case - I'll change the logic, thankfully its only a few lines over a few procs.

In case you're wondering, it's part of my p2p thingy.
I borrowed the notion of a PieceMap from BitTorrent.
The bits simply denote which "pieces" of a file we have / need,
since like BT this p2p stripes the file and transmissions occur in random order of pieces.
Posted on 2004-09-01 08:24:55 by Homer
roticv was suggesting that you simply not before doing a bit scan so instead of
bsf eax

do
not eax

bsf eax

or whatever... :)
Posted on 2004-09-01 08:39:14 by Eóin
Yeah I got that, I'm not sure I can use bsf for this regardless.
bsf dest, src : scans src for first bit set to 1, returning its index in dest
This means it doesn't know when to stop?
You can't scan a known length with this opcode.
It assumes a STOP bit exists.
I don't have that luxury, and don't want to add it as it means (GASP) a potential whole extra byte in the few packets that carry a piecemap.
Bah, maybe I should :)
Posted on 2004-09-01 09:18:18 by Homer
Its easy to check initially to see if no stop bit exists;
; checking for first zero bit in eax

not eax
test eax,eax
jz noZeroBits
bsf edx,eax
jmp endCheck

noZeroBits:
mov edx,-1
endCheck:
; now either edx is the zero based index of the free bit or -1 if there were none

Just a suggestion :)
Posted on 2004-09-01 10:01:24 by Eóin
Pheraphs when you do the not, you can test if zero for see if exist the stop bit use bsf, and continue to test the next part of the string?, or you can use the zf when use bsf for know if the dest is undefined. http://folk.uio.no/botnen/intel/vt/reference/vc19a.htm By the way, you know other place where the mnemonics are listed?
Posted on 2004-09-01 10:02:43 by rea
Hello,

And what about using the 'BT' instruction (or BTC/BTR/BTS) in a loop? The Reg16 operand can be a counter, incremented/decremented at each pass, the test of a 0 bit is made with a jc (JMP if Carry).

Just a proposition...

Regards, Neitsa.


:oops: me stupid, thanks sephiroth...
Posted on 2004-09-01 10:46:44 by Neitsa
EvilHomer2K: The BSF instruction checks only the addressed data item or register. It does not continue on indefinitely when the operand is zero. Instead, it sets ZF=1. However, in my code above, I skip the BSF part altogether when the loaded bitfield is all ones. You might want to use it.

Neitsa: That was the initial attempt, with which he was dissatisfied.
Posted on 2004-09-01 11:16:33 by Sephiroth3