Board index » delphi » Inline byte count

Inline byte count

Hello,

I want to use for the first time the INLINE function in turbo pascal.
This is what I want it to do: count the number of binary 0 or 1 in an
integer.

I have started looking for the list of hexadecimal codes for the X86
instruction set. I'll need it to understand the examples in the Turbo
Pascal manual. I have the asm X86 FAQ and newsgroups on hand but still
I haven't found the codes.

Of course, if anyone out there can painlessly and effortlessly email
me an example of such a function then I would be very grateful.

Thank you.

Gilles Lehoux, P.Eng.    gleh...@generation.net
Mehcanical Engineer

 

Re:Inline byte count


Quote
In article <4sth2c$...@chinook.Generation.NET> G. Lehoux wrote:
>Hello,

>I want to use for the first time the INLINE function in turbo pascal.
>This is what I want it to do: count the number of binary 0 or 1 in an
>integer.

>I have started looking for the list of hexadecimal codes for the X86
>instruction set. I'll need it to understand the examples in the Turbo
>Pascal manual. I have the asm X86 FAQ and newsgroups on hand butstill
>I haven't found the codes.

>Of course, if anyone out there can painlessly and effortlessly email
>me an example of such a function then I would be very grateful.

Gilles, the idea of hand-crafting the necessary hexadecimal instructions
in this day and age is a little more effort than most of us would care
invest, even for a short inline function.  The easiest way to obtain the
codes is to use an assembler which could be used to produce a listing
something like -

  6 0000  59                         pop  CX
  7 0001  33 C0                      xor  AX,AX
  8 0003  E3 06                  @1: jcxz @2
  9 0005  D1 E9                      shr  CX,1
 10 0007  14 00                      adc  AL,0
 11 0009  EB F8                      jmp  SHORT @1
 12 000B                         @2:

or --
   6 0000  5A                         pop  DX
   7 0001  33 C0                      xor  AX,AX
   8 0003  D1 EA                  @1: shr  DX,1
   9 0005  14 00                      adc  AL,0
  10 0007  0B D2                      or   DX,DX
  11 0009  75 F8                      jnz  @1

Which can then be easily converted to -

FUNCTION BitCount(i: Integer): Integer;
INLINE ( $59          {     pop  CX       }
        /$33/$C0      {     xor  AX,AX    }
        /$E3/$06      { @1: jcxz @2       }
        /$D1/$E9      {     shr  CX,1     }
        /$14/$00      {     adc  AL,0     }
        /$EB/$F8      {     jmp  SHORT @1 }
       );             { @2:

or --
FUNCTION BitCount(i: Integer): Integer;
INLINE ( $5A          {     pop  DX       }
        /$33/$C0      {     xor  AX,AX    }
        /$D1/$EA      { @1: shr  DX,1     }
        /$14/$00      {     adc  AL,0     }
        /$0B/$D2      {     or   DX,DX    }
        /$75/$F8      {     jnz  @1       }
       );

There are utilities that will convert an assembly listing into an
appropriate TP inline statement.  But for infrequent small tasks I
find the above technique sufficient.

BTW, You might want to take a look at the table look-up used by the
"Count" method in the bitset unit available from my home page at
    http://members.gnn.com/rdonais/indext.html

or via ftp from
    ftp://members.gnn.com/rdonais/tpascal/bitset.zip

    ...red

Other Threads