Hi,

bsf gives us the first bit 1 in a DWORD ...

Is there an instruction (Pentium and AMD compatible) which gives us the position of the first byte 1 in an array of byte? What is the cost? is there a limitation for the array size ?

Thanks
Posted on 2002-04-03 05:48:02 by DarkEmpire


bsf gives us the first bit 1 in a DWORD ...


From the MASM32 opcode help file:

bsf

Scans source operand for first bit set. Sets ZF if a bit is found
set and loads the destination with an index to first set bit. Clears
ZF is no bits are found set. BSF scans forward across bit pattern

So I guess that is what you want to do, but on bytes....

Is there an instruction (Pentium and AMD compatible) which gives us the position of the first byte 1 in an array of byte? What is the cost? is there a limitation for the array size ?
Thanks


Windows might have a problem with using scas, I never used it either, so don't really know if it would work. The processor really doesn't understand the concept of array, a linear block of memory. So your best bet would probably be a small loop:

lea eax, myarray <get addr of array
mov edx, eax <backup addr of array
loop:
cmp byte ptr ,1 <is eax 1?mightbetotallywronghere
je found <is equal? jump to found
inc eax <inc eax to check next byte
jmp loop <go to loop to test next
found:
sub eax, edx <edx has the old address. eax-edx should give correct number

I'm horribly tierd right now, so I sure I goofed something up.
The code will overrun and crash your program as it is since I do not check for the length of the array. I didn't try to optomize either or find the best solution. Good luck.
Posted on 2002-04-03 07:58:30 by ThoughtCriminal


Windows might have a problem with using scas

nope, it doesn't.



mov al,1
lea edi,array
mov ecx,sizeofarray
repne scasb

if ecx is > 0, edi-1 is the first byte with the value of 1.
you can assume the direction flag to be clear.
Posted on 2002-04-03 11:07:09 by Tola
The Opcode help file in MASM32 made it sound like you neeeded to use the ES segment. But I guess that does not apply in protected mode.

Thanks for the correction.
Posted on 2002-04-04 01:15:56 by ThoughtCriminal
It does but then again Win32 uses a flat memory model where ES=DS.. so no problems here. :)
Posted on 2002-04-04 04:06:32 by Maverick