<<Jacques: I know I can count. I want to know if there is a BUILT-IN function doing the job for me. >>
There is no built-in function and, like you, I think there should be, just as there should be sets beyond 256 elements. Many years ago, I wrote a universal bit counter, which follows.
PhR
--------------- FUNCTION card (var tar; size: integer): integer; assembler; //tested (*Reg usage: eax=result ebx=work reg ecx=nibble counter edx=@last byte in tar, +1 esi=@next elm in tar edi=@last dw in tar, +1 ebp=0 (to shorten opcodes)*) (*NB the main engine is countNibble. The setup gets tar into esi and sets two limits for it: full dw's, and bytes (i.e. full length). CountNibble is fed from dw's until the first limit is reached, then from the remaining bytes*) ASM (*setup*) push ebx push esi push edi push ebp mov esi, eax //set esi mov ebx, edx and ebx, 3 add edx, eax //set edx mov edi, edx sub edi, ebx //set edi xor eax, eax //set eax xor ebp, ebp //set ebp
@doDws: cmp esi, edi jnb @doBytes mov ebx, [esi] add esi, 4 mov ecx, 8 //eight nibbles per dw @countNibble: //repeats save three loop's shr ebx, 1 //adding four more would only save one loop adc eax, ebp shr ebx, 1 adc eax, ebp shr ebx, 1 adc eax, ebp shr ebx, 1 adc eax, ebp loop @countNibble jmp @doDws
@doBytes: cmp esi, edx jz @exit mov bl, [esi] inc esi mov ecx, 2 //two nibbles jmp @countNibble
@exit: pop ebp pop ?edi pop esi pop ebx END; ---------------------------