$R-,S-

Unit CRCS;

Interface

FUNCTION CRC (VAR buf; len : WORD) : WORD;

FUNCTION ChkSum (VAR buf; len : WORD): WORD;

Implementation

TYPE CrcTabType = ARRAY [BYTE] OF WORD;

VAR CrcTab : CrcTabType;

FUNCTION CRC (VAR buf; len : WORD) : WORD;
BEGIN
Inline(
  $1E                    {push ds}
  /$1E                   {push ds}
  /$07                   {pop es}
  /$8D/$3E/>CRCTAB       {lea di,[>crctab]}
  /$C5/$76/<BUF          {lds si,[bp<buf]}
  /$8B/$4E/<LEN          {mov cx,[bp<len]}
  /$31/$D2               {xor dx,dx}
  /$E3/$13               {jcxz done}
  /$FC                   {cld}
                         {l1:}
  /$AC                   {lodsb}
  /$30/$D0               {xor al,dl}
  /$88/$C3               {mov bl,al}
  /$88/$F2               {mov dl,dh}
  /$30/$FF               {xor bh,bh}
  /$88/$FE               {mov dh,bh}
  /$D1/$E3               {shl bx,1}
  /$26/$33/$11           {es: xor dx,[di+bx]}
  /$E2/$EE               {loop l1}
                         {done:}
  /$89/$56/$FE           {mov [bp-2],dx}
  /$1F                   {pop ds}
);
END;

FUNCTION ChkSum (VAR buf; len : WORD): WORD;
BEGIN
InLine(
  $1E                    {  push ds}
  /$C5/$76/<BUF          {  lds si,[bp<buf]}
  /$8B/$4E/<LEN          {  mov cx,[bp<len]}
  /$31/$D2               {  xor dx,dx}
  /$89/$D0               {  mov ax,dx}
  /$FC                   {  cld}
  /$88/$CB               {  mov bl,cl}
  /$D1/$E9               {  shr cx,1}
  /$D1/$E9               {  shr cx,1}
  /$41                   {  inc cx}
  /$80/$E3/$03           {  and bl,3}
  /$74/$15               {  jz add0}
  /$80/$FB/$02           {  cmp bl,2}
  /$77/$07               {  ja add3}
  /$74/$08               {  je add2}
  /$EB/$09               {  jmp short add1}
                         {add4:}
  /$AC                   {  lodsb}
  /$01/$C2               {  add dx,ax}
                         {add3:}
  /$AC                   {  lodsb}
  /$01/$C2               {  add dx,ax}
                         {add2:}
  /$AC                   {  lodsb}
  /$01/$C2               {  add dx,ax}
                         {add1:}
  /$AC                   {  lodsb}
  /$01/$C2               {  add dx,ax}
                         {add0:}
  /$E2/$F2               {  loop add4}
                         {done:}
  /$89/$56/$FE           {  mov [bp-2],dx}
  /$1F                   {  pop ds}
);
END;

BEGIN
InLine(
   $1E                   {push ds}
  /$07                   {pop es}
  /$8D/$3E/>CRCTAB       {lea di,[>crctab]}
  /$BE/$08/$84           {mov si,$8408}
  /$FC                   {cld}
  /$31/$DB               {xor bx,bx}
  /$89/$D9               {mov cx,bx}
                         {l2:}
  /$89/$D8               {mov ax,bx}
  /$B1/$08               {mov cl,8}
                         {l3:}
  /$D1/$E8               {shr ax,1}
  /$73/$02               {jnc l4}
  /$31/$F0               {xor ax,si}
                         {l4:}
  /$E2/$F8               {loop l3}
  /$AB                   {stosw}
  /$FE/$C3               {inc bl}
  /$75/$EF               {jnz l2}
);
END.
