home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PROTOCOL / TPHYD100.ZIP / CRC.ASM next >
Assembly Source File  |  1993-08-18  |  11KB  |  307 lines

  1. ; ============================================================ Rev. 15 Jul 1993
  2. ; Routines for table driven CRC-16 & CRC-32, including building tables
  3. ; Refer to CRC.DOC for information and documentation.
  4. ; This file in 80x86 ASM is 100% interchangable with the portable CRC.C
  5. ; Initially written to see how CRC calculation could be optimized in assembly
  6. ; Reads the data in words instead bytes at a time, makes asm even 20-25% faster
  7. ; Tried specifics for 386 and up (SHR EAX,8 etc), but didn't wasn't much faster
  8. ; Source compiles cleanly in TASM. May require some minor adjustments for MASM.
  9. ; -----------------------------------------------------------------------------
  10. ;              Information collected and edited by Arjen G. Lentz
  11. ;            Sourcecode in C and 80x86 ASM written by Arjen G. Lentz
  12. ;                 COPYRIGHT (C) 1992-1993; ALL RIGHTS RESERVED
  13. ;  
  14. ; CONTACT ADDRESS
  15. ;  
  16. ; LENTZ SOFTWARE-DEVELOPMENT    Arjen Lentz @
  17. ; Langegracht 7B                AINEX-BBS +31-33-633916
  18. ; 3811 BT  Amersfoort           FidoNet 2:283/512
  19. ; The Netherlands               f512.n283.z2.fidonet.org
  20. ;
  21. ;
  22. ; DISCLAIMER
  23. ;  
  24. ; This information is provided "as is" and comes with no warranties of any
  25. ; kind, either expressed or implied. It's intended to be used by programmers
  26. ; and developers. In no event shall the author be liable to you or anyone
  27. ; else for any damages, including any lost profits, lost savings or other
  28. ; incidental or consequential damages arising out of the use or inability
  29. ; to use this information.
  30. ;  
  31. ;  
  32. ; LICENCE
  33. ;  
  34. ; This package may be freely distributed provided the files remain together,
  35. ; in their original unmodified form.
  36. ; All files, executables and sourcecode remain the copyrighted property of
  37. ; Arjen G. Lentz and LENTZ SOFTWARE-DEVELOPMENT.
  38. ; Licence for any use granted, provided this notice & CRC.DOC are included.
  39. ; For executable applications, credit should be given in the appropriate
  40. ; places in the program and documentation.
  41. ; These notices must be retained in any copies of any part of this
  42. ; documentation and/or software.
  43. ;
  44. ; Any use of, or operation on (including copying/distributing) any of
  45. ; the above mentioned files implies full and unconditional acceptance of
  46. ; this licence and disclaimer.
  47. ;
  48. ; =============================================================================
  49.  
  50. ; Make sure you set the model and language right, or your program will crash!
  51.         .MODEL Large,Pascal    ; Model   : Tiny,Small,Medium,Compact,Large,Huge
  52.                                ; Language: C,Pascal,Basic,Fortran,Prolog
  53.  
  54.  
  55. ; -----------------------------------------------------------------------------
  56.         .CODE
  57.         LOCALS
  58.  
  59.         PUBLIC crc16init,  crc16block
  60.         PUBLIC crc16rinit, crc16rblock
  61.         PUBLIC crc32init,  crc32block
  62.  
  63.  
  64. ; -----------------------------------------------------------------------------
  65. crc16init PROC
  66.         ARG     crctab:DWORD, poly:WORD
  67.         USES    si, di
  68.  
  69.         les     di, [crctab]            ; ES:DI = crctab
  70.         mov     bx, [poly]              ; BX    = polynomial
  71.         sub     si, si                  ; i = 0
  72.         cld
  73.  
  74. @@itop: mov     ax, si                  ; crc = i
  75.         mov     cx, 8                   ; j = 8
  76. @@jtop: shr     ax, 1                   ; crc >>= 1
  77.         jnc     @@jbit                  ; if (!carry)
  78.         xor     ax, bx                  ;    crc ^= poly
  79. @@jbit: loopnz  @@jtop                  ; if (--CX != 0 && crc) goto @@jtop
  80.         stosw                           ; crctab[i] = crc
  81.  
  82.         inc     si
  83.         cmp     si, 255                 ; if (++i <= 255)
  84.         jle     @@itop                  ;    goto @@itop
  85.  
  86.         ret
  87. crc16init ENDP
  88.  
  89.  
  90. ; -----------------------------------------------------------------------------
  91. crc16block PROC
  92.         ARG     crctab:DWORD, crc:WORD, buf:DWORD, len:WORD
  93.         USES    ds, si, di
  94.  
  95.         les     di, [crctab]            ; ES:DI = crctab
  96.         mov     bx, [crc]               ; BX    = crc
  97.         lds     si, [buf]               ; DS:SI = buf
  98.         mov     cx, [len]               ; CX    = len
  99.         cld
  100.  
  101.         shr     cx, 1
  102.         jnc     @@even
  103.  
  104.         lodsb                           ; get single byte
  105.         xor     bl, al                  ; BL ^= AL
  106.         mov     ah, bh                  ; AH = BH
  107.         sub     bh, bh                  ; BH = 0
  108.         shl     bx, 1                   ; BX *= 2 (mul for word array)
  109.         mov     bx, es:[di + bx]        ; BX = crctab[BX]
  110.         xor     bl, ah                  ; BL ^= AH
  111.  
  112. @@even: jcxz    @@fini                  ; if (!len) goto @@fini
  113.  
  114. @@itop: lodsw                           ; get next two bytes
  115.         xor     bl, al                  ; first byte
  116.         xor     ah, bh                  ; already merge with second byte
  117.         sub     bh, bh
  118.         shl     bx, 1
  119.         mov     bx, es:[di + bx]
  120.         xor     bl, ah                  ; second byte
  121.         mov     ah, bh
  122.         sub     bh, bh
  123.         shl     bx, 1
  124.         mov     bx, es:[di + bx]
  125.         xor     bl, ah
  126.  
  127.         loop    @@itop                  ; if (--CX != 0) goto @@itop
  128.  
  129. @@fini: mov     ax, bx
  130.         ret
  131. crc16block ENDP
  132.  
  133.  
  134. ; -----------------------------------------------------------------------------
  135. crc16rinit PROC
  136.         ARG     crctab:DWORD, poly:WORD
  137.         USES    si, di
  138.  
  139.         les     di, [crctab]            ; ES:DI = crctab
  140.         mov     bx, [poly]              ; BX    = polynomial
  141.         sub     si, si                  ; i = 0
  142.         cld
  143.  
  144. @@itop: mov     ax, si                  ; crc = i << 8
  145.         xchg    ah, al
  146.         mov     cx, 8                   ; j = 8
  147. @@jtop: shl     ax, 1                   ; crc <<= 1
  148.         jnc     @@jbit                  ; if (!carry)
  149.         xor     ax, bx                  ;    crc ^= poly
  150. @@jbit: loopnz  @@jtop                  ; if (--CX != 0 && crc) goto @@jtop
  151.         stosw                           ; crctab[i] = crc
  152.  
  153.         inc     si
  154.         cmp     si, 255                 ; if (++i <= 255)
  155.         jle     @@itop                  ;    goto @@itop
  156.  
  157.         ret
  158. crc16rinit ENDP
  159.  
  160.  
  161. ; -----------------------------------------------------------------------------
  162. crc16rblock PROC
  163.         ARG     crctab:DWORD, crc:WORD, buf:DWORD, len:WORD
  164.         USES    ds, si, di
  165.  
  166.         les     di, [crctab]            ; ES:DI = crctab
  167.         mov     bx, [crc]               ; BX    = crc
  168.         lds     si, [buf]               ; DS:SI = buf
  169.         mov     cx, [len]               ; CX    = len
  170.         cld
  171.  
  172.         shr     cx, 1
  173.         jnc     @@even
  174.  
  175.         lodsb                           ; get single byte
  176.         mov     ah, bl                  ; AH = BL
  177.         mov     bl, bh                  ; BL = BH
  178.         xor     bl, al                  ; BL ^= AL
  179.         sub     bh, bh                  ; BH = 0
  180.         shl     bx, 1                   ; BX *= 2 (mul for word array)
  181.         mov     bx, es:[di + bx]        ; BX = crctab[BX]
  182.         xor     bh, ah                  ; BH ^= AH
  183.  
  184. @@even: jcxz    @@fini                  ; if (!len) goto @@fini
  185.  
  186. @@itop: lodsw                           ; get next two bytes
  187.         xor     ah, bl                  ; already merge with second byte
  188.         mov     bl, bh
  189.         xor     bl, al                  ; first byte
  190.         sub     bh, bh
  191.         shl     bx, 1
  192.         mov     bx, es:[di + bx]
  193.         xor     bh, ah                  ; second byte
  194.         mov     ah, bl
  195.         mov     bl, bh
  196.         sub     bh, bh
  197.         shl     bx, 1
  198.         mov     bx, es:[di + bx]
  199.         xor     bh, ah
  200.  
  201.         loop    @@itop                  ; if (--CX != 0) goto @@itop
  202.  
  203. @@fini: mov     ax, bx
  204.         ret
  205. crc16rblock ENDP
  206.  
  207.  
  208. ; -----------------------------------------------------------------------------
  209. crc32init PROC
  210.         ARG     crctab:DWORD, poly:DWORD
  211.         USES    si, di
  212.  
  213.         les     di, [crctab]            ; ES:DI = crctab
  214.         mov     bx, [word ptr poly]     ; BX = poly (low)
  215.         mov     bp, [word ptr poly + 2] ; BP = poly (high)
  216.         sub     si, si                  ; i = 0
  217.  
  218. @@itop: mov     ax, si                  ; crc (low)  = i
  219.         sub     dx, dx                  ; crc (high) = 0
  220.         mov     cx, 8                   ; j = 8
  221. @@jtop: shr     dx, 1                   ; crc (high) >>= 1
  222.         rcr     ax, 1                   ; crc (low)  >>= 1
  223.         jnc     @@jbit                  ; if (!carry)
  224.         xor     ax, bx                  ;    crc (low)  ^= polyl
  225.         xor     dx, bp                  ;    crc (high) ^= polyh
  226. @@jbit: loop    @@jtop                  ; if (--CX != 0) goto @@jtop
  227.         stosw                           ; crctab[i] = crc (low)
  228.         mov     ax, dx
  229.         stosw                           ; crctab[i] = crc (high)
  230.  
  231.         inc     si
  232.         cmp     si, 255                 ; if (++i <= 255)
  233.         jle     @@itop                  ;    goto @@itop
  234.  
  235.         ret
  236. crc32init ENDP
  237.  
  238.  
  239. ; -----------------------------------------------------------------------------
  240. crc32block PROC
  241.         ARG     crctab:DWORD, crc:DWORD, buf:DWORD, len:WORD
  242.         USES    ds, si, di
  243.  
  244.         les     di, [crctab]            ; ES:DI = crctab
  245.         mov     ax, [word ptr crc]      ; AX    = crc (low)
  246.         mov     dx, [word ptr crc + 2]  ; DX    = crc (high)
  247.         lds     si, [buf]               ; DS:SI = buf
  248.         mov     cx, [len]               ; CX    = len
  249.         cld
  250.  
  251.         shr     cx, 1
  252.         jnc     @@even
  253.  
  254.         mov     bl, al                  ; BL = AL
  255.         lodsb                           ; get single byte
  256.         xor     bl, al                  ; BL ^= AL
  257.         mov     al, ah                  ; AL = AH
  258.         mov     ah, dl                  ; AH = DL
  259.         mov     dl, dh                  ; DL = DH
  260.         sub     dh, dh                  ; DH = 0
  261.         sub     bh, bh                  ; BH = 0
  262.         shl     bx, 1                   ; BX *= 4 (mul for dword array)
  263.         shl     bx, 1
  264.         xor     ax, es:[di + bx]        ; AX ^= crctab[BX]
  265.         add     bx, 2                   ; BX += 2 (add for second word)
  266.         xor     dx, es:[di + bx]        ; DX ^= crctab[BX]
  267.  
  268. @@even: jcxz    @@fini                  ; if (!len) goto @@fini
  269.  
  270. @@itop: mov     bx, ax
  271.         lodsw                           ; get next to bytes
  272.         xor     bl, al
  273.         mov     al, bh
  274.         xor     al, ah                  ; already merge with second byte
  275.         mov     ah, dl
  276.         mov     dl, dh
  277.         sub     dh, dh
  278.         sub     bh, bh
  279.         shl     bx, 1
  280.         shl     bx, 1
  281.         xor     ax, es:[di + bx]
  282.         add     bx, 2
  283.         xor     dx, es:[di + bx]
  284.         mov     bl, al
  285.         mov     al, ah
  286.         mov     ah, dl
  287.         mov     dl, dh
  288.         sub     dh, dh
  289.         sub     bh, bh
  290.         shl     bx, 1
  291.         shl     bx, 1
  292.         xor     ax, es:[di + bx]
  293.         add     bx, 2
  294.         xor     dx, es:[di + bx]
  295.  
  296.         loop    @@itop                  ; if (--CX != 0) goto @@itop
  297.  
  298. @@fini: ret
  299. crc32block ENDP
  300.  
  301.  
  302.         END
  303.  
  304. ; end of crc.asm --------------------------------------------------------------
  305.