home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / basic / library / tb / tbxm10 / chkcrc.inl next >
Text File  |  1987-07-25  |  5KB  |  94 lines

  1. SUB ChkCRC INLINE
  2. '; Modified 5/16/87 for TurboBASIC INLINE by Craig J. Kim  [73240,423]
  3. ';  Eliminated unnecessary local variables by directly using the passed
  4. ';  arguments - other than calculation parts, the entire routine is
  5. ';  completed rewritten for TurboBASIC.
  6. ';
  7. '; Heavy modifications 8/31/86 by Jim King
  8. '; Changed CRC_CALC from the awfulness it was to an algorithm suggested
  9. '; by Philip Burns.  In a test program, this algorithm is over 3 times as
  10. '; fast as the one previously used by RBBS-PC.
  11. '; Changed the loop that calculates checksum and calls the CRC to be more
  12. '; efficient (just about halved the number of instructions).
  13. '; Note that RBBS-PC.BAS was also modified so that it no longer tacks on
  14. '; two null bytes to the input string (they were necessary for the old CRC
  15. '; routine to work correctly).
  16. '; Once again, thanks to Philip Burns for suggesting the CRC algorithm.
  17. '; Many thanks also to John Souvestre, who helped me tweak the assembly
  18. '; routine to run even faster.
  19. ';
  20. '; Modified 8/24/85 for use with QuickBasic Compiler
  21. ';
  22. '; Call ChkCRC(CS$, Checksum%, CRC%, CRC.Hi%, CRCLo%)
  23. ';             16      12       0E     0A       6
  24. ';             22      18       14     10       6
  25. '; TurboBASIC's arguments are 32-bit pointers
  26. ';
  27. '; Local variable - use stack
  28. ';  CHK_SUM           DB 0   ==> [bp+2]
  29. ';
  30. ';
  31.     $INLINE &H55                    '  PUSH BP
  32.     $INLINE &H89,&HE5               '  MOV  BP,SP
  33.     $INLINE &H06                    '  push es
  34.     $INLINE &H1E                    '  push ds
  35.     $INLINE &HC6,&H46,&HFE,&H00     '  MOV  byte ptr [bp-2],0 ;INITIALIZE checksum
  36.     $INLINE &HC4,&H7E,&H16          '  les  di,[BP+22]        ;GET STRING DESCRIPTOR
  37.     $INLINE &H3E                    '  ds:
  38.     $INLINE &H8B,&H16,&H00,&H00     '  mov  dx,[0]
  39.     $INLINE &H52                    '  push dx
  40.     $INLINE &H1F                    '  pop  ds
  41.     $INLINE &H26                    '  es:
  42.     $INLINE &H8B,&H75,&H02          '  MOV  si,[di+2]         ;NOW si HOLDS THE ADDRESS OF THE STRING
  43.     $INLINE &H26                    '  es:
  44.     $INLINE &H8B,&H0D               '  MOV  cx,[di]           ;GET STRING LENGTH
  45.     $INLINE &H31,&HD2               '  XOR  DX,DX             ;initialize CRC value to 0
  46.     $INLINE &H1E                    '  PUSH DS
  47.                                     'LOOP1:
  48.     $INLINE &HAC                    '  LODSB                  ;get character into AL
  49.     $INLINE &H89,&HCF               '  MOV  DI,CX             ;SAVE CX
  50.     $INLINE &H00,&H46,&HFE          '  ADD  byte ptr [bp-2],AL ;ADD AL TO CHK_SUM
  51.                                     '; a separate procedure for additional speed.
  52.                                     '; DX contains the CRC value, AL has the new character.  Other registers
  53.                                     '; are used for temporary storage and scratch work.
  54.     $INLINE &H86,&HD6               '  XCHG DH,DL             ; CRC := Swap(CRC) XOR Ord(Ch);
  55.     $INLINE &H30,&HC2               '  XOR  DL,AL
  56.     $INLINE &H88,&HD0               '  MOV  AL,DL             ; CRC := CRC XOR ( Lo(CRC) SHR 4 );
  57.     $INLINE &HB1,&H04               '  MOV  CL,4
  58.     $INLINE &HD2,&HE8               '  SHR  AL,CL
  59.     $INLINE &H30,&HC2               '  XOR  DL,AL
  60.                                     '                         ; CRC := CRC XOR ( Swap(Lo(CRC)) SHL 4 )
  61.                                     '                         ;        XOR ( Lo(CRC) SHL 5 );
  62.     $INLINE &H88,&HD3               '  MOV  BL,DL
  63.     $INLINE &H88,&HD4               '  MOV  AH,DL
  64.     $INLINE &HD2,&HE4               '  SHL  AH,CL
  65.     $INLINE &H30,&HE6               '  XOR  DH,AH
  66.     $INLINE &H30,&HFF               '  XOR  BH,BH
  67.     $INLINE &HFE,&HC1               '  INC  CL
  68.     $INLINE &HD3,&HE3               '  SHL  BX,CL
  69.     $INLINE &H31,&HDA               '  XOR  DX,BX
  70.                                     '; end of the CRC calculation routine
  71.                                     ';
  72.     $INLINE &H89,&HF9               '  MOV  CX,DI             ;RESTORE CX
  73.     $INLINE &HE2,&HDA               '  LOOP LOOP1             ;do it again
  74.     $INLINE &H1F                    '  POP  DS                ;RESTORE DS
  75.     $INLINE &H89,&HD3               '  MOV  BX,DX             ;PASS BACK THE CRC VALUE
  76.     $INLINE &HC4,&H7E,&H06          '  les  di,[bp+6]         ; load pointer to integer into es:di
  77.     $INLINE &H26                    '  es:
  78.     $INLINE &H88,&H1D               '  mov  [di],bl
  79.     $INLINE &HC4,&H7E,&H0A          '  les  di,[bp+10]
  80.     $INLINE &H26                    '  es:
  81.     $INLINE &H88,&H3D               '  mov  [di],bh
  82.     $INLINE &HC4,&H7E,&H0E          '  les  di,[bp+14]
  83.     $INLINE &H26                    '  es:
  84.     $INLINE &H89,&H1D               '  mov  [di],bx
  85.     $INLINE &H31,&HDB               '  XOR  BX,BX
  86.     $INLINE &H8A,&H5E,&HFE          '  MOV  BL,byte ptr [bp-2] ;PASS BACK THE CHECK SUM
  87.     $INLINE &HC4,&H7E,&H12          '  les  di,[bp+18]
  88.     $INLINE &H26                    '  es:
  89.     $INLINE &H88,&H1D               '  mov  [di],bl
  90.     $INLINE &H1F                    '  pop  ds
  91.     $INLINE &H07                    '  POP  ES
  92.     $INLINE &H5D                    '  POP  BP
  93. END SUB
  94.