home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / clipper / snip0693.zip / CRC32.ASM < prev    next >
Assembly Source File  |  1993-03-13  |  11KB  |  254 lines

  1. ;Name    CRC32.ASM
  2.  
  3. ;Author  (C) 1986 Gary S. Brown.  No restrictions apply.
  4.  
  5. ;1992.04.19 Rewrite - Bruce Gavin  Translated C  -> ASM.
  6.  
  7. ;1992.11.3  Rewrite - Terry Carmen re-wrote to be callable
  8. ;                     from Clipper S'87 or 5.01
  9.  
  10. ; CRC32 takes a single argument as a string, and returns
  11. ; the CRC32 to Clipper as an 8 digit character string
  12.  
  13. ; Returns NIL if called with incorrect arguments or an empty string
  14.  
  15. ; CRC32 polynomial is edb88320h, and will produce identical CRCs as PKIZP
  16.  
  17.  
  18. public crc32
  19.  
  20. extrn   __parinfo:far
  21. extrn   __parc:far
  22. extrn   __parclen:far
  23. extrn   __retc:far
  24. extrn   __ret:far
  25.  
  26. assume cs:_prog, ds:datasg, es:nothing
  27.  
  28. dgroup  group   datasg
  29. datasg  segment public  para 'DATA'
  30.  
  31. Crc32_Tbl       Label Dword
  32.         DD      000000000h,  077073096h,  0EE0E612Ch,  0990951BAh
  33.         DD      0076DC419h,  0706AF48Fh,  0E963A535h,  09E6495A3h
  34.         DD      00EDB8832h,  079DCB8A4h,  0E0D5E91Eh,  097D2D988h
  35.         DD      009B64C2Bh,  07EB17CBDh,  0E7B82D07h,  090BF1D91h
  36.         DD      01DB71064h,  06AB020F2h,  0F3B97148h,  084BE41DEh
  37.         DD      01ADAD47Dh,  06DDDE4EBh,  0F4D4B551h,  083D385C7h
  38.         DD      0136C9856h,  0646BA8C0h,  0FD62F97Ah,  08A65C9ECh
  39.         DD      014015C4Fh,  063066CD9h,  0FA0F3D63h,  08D080DF5h
  40.         DD      03B6E20C8h,  04C69105Eh,  0D56041E4h,  0A2677172h
  41.         DD      03C03E4D1h,  04B04D447h,  0D20D85FDh,  0A50AB56Bh
  42.         DD      035B5A8FAh,  042B2986Ch,  0DBBBC9D6h,  0ACBCF940h
  43.         DD      032D86CE3h,  045DF5C75h,  0DCD60DCFh,  0ABD13D59h
  44.         DD      026D930ACh,  051DE003Ah,  0C8D75180h,  0BFD06116h
  45.         DD      021B4F4B5h,  056B3C423h,  0CFBA9599h,  0B8BDA50Fh
  46.         DD      02802B89Eh,  05F058808h,  0C60CD9B2h,  0B10BE924h
  47.         DD      02F6F7C87h,  058684C11h,  0C1611DABh,  0B6662D3Dh
  48.         DD      076DC4190h,  001DB7106h,  098D220BCh,  0EFD5102Ah
  49.         DD      071B18589h,  006B6B51Fh,  09FBFE4A5h,  0E8B8D433h
  50.         DD      07807C9A2h,  00F00F934h,  09609A88Eh,  0E10E9818h
  51.         DD      07F6A0DBBh,  0086D3D2Dh,  091646C97h,  0E6635C01h
  52.         DD      06B6B51F4h,  01C6C6162h,  0856530D8h,  0F262004Eh
  53.         DD      06C0695EDh,  01B01A57Bh,  08208F4C1h,  0F50FC457h
  54.         DD      065B0D9C6h,  012B7E950h,  08BBEB8EAh,  0FCB9887Ch
  55.         DD      062DD1DDFh,  015DA2D49h,  08CD37CF3h,  0FBD44C65h
  56.         DD      04DB26158h,  03AB551CEh,  0A3BC0074h,  0D4BB30E2h
  57.         DD      04ADFA541h,  03DD895D7h,  0A4D1C46Dh,  0D3D6F4FBh
  58.         DD      04369E96Ah,  0346ED9FCh,  0AD678846h,  0DA60B8D0h
  59.         DD      044042D73h,  033031DE5h,  0AA0A4C5Fh,  0DD0D7CC9h
  60.         DD      05005713Ch,  0270241AAh,  0BE0B1010h,  0C90C2086h
  61.         DD      05768B525h,  0206F85B3h,  0B966D409h,  0CE61E49Fh
  62.         DD      05EDEF90Eh,  029D9C998h,  0B0D09822h,  0C7D7A8B4h
  63.         DD      059B33D17h,  02EB40D81h,  0B7BD5C3Bh,  0C0BA6CADh
  64.         DD      0EDB88320h,  09ABFB3B6h,  003B6E20Ch,  074B1D29Ah
  65.         DD      0EAD54739h,  09DD277AFh,  004DB2615h,  073DC1683h
  66.         DD      0E3630B12h,  094643B84h,  00D6D6A3Eh,  07A6A5AA8h
  67.         DD      0E40ECF0Bh,  09309FF9Dh,  00A00AE27h,  07D079EB1h
  68.         DD      0F00F9344h,  08708A3D2h,  01E01F268h,  06906C2FEh
  69.         DD      0F762575Dh,  0806567CBh,  0196C3671h,  06E6B06E7h
  70.         DD      0FED41B76h,  089D32BE0h,  010DA7A5Ah,  067DD4ACCh
  71.         DD      0F9B9DF6Fh,  08EBEEFF9h,  017B7BE43h,  060B08ED5h
  72.         DD      0D6D6A3E8h,  0A1D1937Eh,  038D8C2C4h,  04FDFF252h
  73.         DD      0D1BB67F1h,  0A6BC5767h,  03FB506DDh,  048B2364Bh
  74.         DD      0D80D2BDAh,  0AF0A1B4Ch,  036034AF6h,  041047A60h
  75.         DD      0DF60EFC3h,  0A867DF55h,  0316E8EEFh,  04669BE79h
  76.         DD      0CB61B38Ch,  0BC66831Ah,  0256FD2A0h,  05268E236h
  77.         DD      0CC0C7795h,  0BB0B4703h,  0220216B9h,  05505262Fh
  78.         DD      0C5BA3BBEh,  0B2BD0B28h,  02BB45A92h,  05CB36A04h
  79.         DD      0C2D7FFA7h,  0B5D0CF31h,  02CD99E8Bh,  05BDEAE1Dh
  80.         DD      09B64C2B0h,  0EC63F226h,  0756AA39Ch,  0026D930Ah
  81.         DD      09C0906A9h,  0EB0E363Fh,  072076785h,  005005713h
  82.         DD      095BF4A82h,  0E2B87A14h,  07BB12BAEh,  00CB61B38h
  83.         DD      092D28E9Bh,  0E5D5BE0Dh,  07CDCEFB7h,  00BDBDF21h
  84.         DD      086D3D2D4h,  0F1D4E242h,  068DDB3F8h,  01FDA836Eh
  85.         DD      081BE16CDh,  0F6B9265Bh,  06FB077E1h,  018B74777h
  86.         DD      088085AE6h,  0FF0F6A70h,  066063BCAh,  011010B5Ch
  87.         DD      08F659EFFh,  0F862AE69h,  0616BFFD3h,  0166CCF45h
  88.         DD      0A00AE278h,  0D70DD2EEh,  04E048354h,  03903B3C2h
  89.         DD      0A7672661h,  0D06016F7h,  04969474Dh,  03E6E77DBh
  90.         DD      0AED16A4Ah,  0D9D65ADCh,  040DF0B66h,  037D83BF0h
  91.         DD      0A9BCAE53h,  0DEBB9EC5h,  047B2CF7Fh,  030B5FFE9h
  92.         DD      0BDBDF21Ch,  0CABAC28Ah,  053B39330h,  024B4A3A6h
  93.         DD      0BAD03605h,  0CDD70693h,  054DE5729h,  023D967BFh
  94.         DD      0B3667A2Eh,  0C4614AB8h,  05D681B02h,  02A6F2B94h
  95.         DD      0B40BBE37h,  0C30C8EA1h,  05A05DF1Bh,  02D02EF8Dh
  96.  
  97.         crcstring     db '        ',0
  98.  
  99. datasg ends
  100.  
  101.  
  102.  
  103. _prog segment 'CODE'
  104.  
  105. Crc32  Proc  far
  106.                 push    bp
  107.                 mov     bp,sp
  108.                 push    ds
  109.                 push    es
  110.                 push    si
  111.                 push    di
  112.  
  113.                 mov     ax, 0
  114.                 push    ax
  115.                 call    __parinfo           ; how many params?
  116.                 add     sp,2
  117.                 cmp     al,1                ; should be 1
  118.                 jne     @@bad_exit          ; if not, we're out of here..
  119.  
  120.                 mov     ax, 1               ; check the type
  121.                 push    ax
  122.                 call    __parinfo
  123.                 add     sp,2
  124.                 and     ax,1                ; is it a string?
  125.                 cmp     ax, 1
  126.                 jne     @@bad_exit          ; if not, we're out of here..
  127.  
  128.                 mov     ax, 1
  129.                 push    ax
  130.                 call    __parclen           ; how big is the string?
  131.                 add     sp,2
  132.                 cmp     ax,0
  133.                 je      @@bad_exit          ; don't bother with null strings
  134.  
  135.                 mov     cx,ax               ; save the length
  136.                 push    cx                  ; because clipper screws it up
  137.  
  138.                 mov     ax, 1
  139.                 push    ax
  140.                 call    __parc              ; where is the string?
  141.                 add     sp,2                ; address in DX:AX
  142.                 pop     cx                  ; get the length back
  143.  
  144.                 mov     es, dx              ; ES:DI point to it
  145.                 mov     di, ax
  146.  
  147.                 mov     ax, seg datasg      ; use our data segment instead
  148.                 mov     ds, ax              ; of Clippers
  149.  
  150.                 mov     ax,0ffffh           ; pre-load initial CRC = FFFFFFFFh
  151.                 mov     dx,ax               ; so the first byte gets included
  152.                                             ; in the CRC calculation
  153. @@nextbyte:
  154.                 mov     bl,es:[di]          ; Get byte from buffer
  155.                 inc     di                  ; Set next buffer pointer
  156.                 xor     bh,bh               ; Convert BL to BX
  157.                 xor     bl,al               ; Calculate table index
  158.                 shl     bx,1                ; Word offset
  159.                 shl     bx,1                ; DWord offset
  160.  
  161.                 ; 0 -> DH -> DL -> AH -> AL -> bit bucket
  162.                 mov     al,ah               ; Shr AH into AL
  163.                 mov     ah,dl               ; Shr DL into AH
  164.                 mov     dl,dh               ; Shr DH into DL
  165.                 xor     dh,dh               ; DH = 0
  166.  
  167.                 ;Get new CRC from table
  168.                 xor     ax,word ptr crc32_tbl[bx]    ; Get new CRC-LO
  169.                 xor     dx,word ptr crc32_tbl[bx][2] ; Get new CRC-HI
  170.  
  171.                 loop    @@nextbyte          ; Go until done
  172.  
  173.                 ;Post-condition returned CRC
  174.                 not     ax                 ; Invert AX
  175.                 not     dx                 ; Invert DX
  176.  
  177.                 mov     bx, seg crcstring  ; point ds:si to our string
  178.                 mov     ds, bx
  179.                 mov     si, offset crcstring
  180.  
  181.                 mov     bx, dx             ; start converting bin -> ascii
  182.                 call bin2asc               ; from the left (upper word)
  183.  
  184.                 mov     bx, ax             ; do the lower word
  185.                 call bin2asc               ; leaves ASCII in [crcstring]
  186.                 jmp short @@returnstring
  187.  
  188. @@bad_exit:     call __ret                  ; return NIL
  189.                 jmp short @@pops            ; the funny jump is because
  190. ; Initial CRC pre-conditioning is 0FFFFFFFFh.
  191.                                             ; we're out of range for a
  192.                                             ; conditional jump on the 80xxx
  193.  
  194. @@returnstring: mov    bx, offset crcstring ; point to our string
  195.                 mov    ax, seg crcstring    ; so Clipper can find it
  196.                 push   ax                   ; push seg:ofs on stack
  197.                 push   bx
  198.  
  199.                 mov    ax, seg dgroup       ; point ds to Clipper's dataseg
  200.                 mov    ds, ax               ; so it doesn't get all confused
  201.  
  202.                 call    __retc              ; send the data back to Clipper
  203.  
  204.                 add    sp,4                 ; fix the stack
  205.  
  206. @@pops:         pop    di
  207.                 pop    si
  208.                 pop    es
  209.                 pop    ds
  210.                 pop    bp
  211.                 Ret
  212. Endp
  213.  
  214. bin2asc proc near               ; displays the contents of BX as ASCII
  215.  
  216.                 push   ax
  217.                 push   bx
  218.                 push   cx
  219.                 push   dx
  220.  
  221.                 mov    ch, 4h
  222. rotate:
  223.                 mov    cl, 4h
  224.                 rol    bx, cl
  225.                 mov    al, bl
  226.                 and    al, 0fh
  227.                 add    al, '0'
  228.                 cmp    al, '9'
  229.                 jle    printit
  230.                 add    al, 7   ; skip over the funny punctuation,
  231.                                ; do the alpha digits A - F
  232.  
  233. printit:
  234.                 mov    [si], al
  235.                 inc    si
  236.  
  237.                 dec    ch
  238.                 cmp    ch, 0
  239.                 jnz    rotate
  240.  
  241.                 pop    dx
  242.                 pop    cx
  243.                 pop    bx
  244.                 pop    ax
  245.                 ret
  246.  
  247. bin2asc endp
  248.  
  249. Ends                            ; End of code Segment
  250. End
  251.  
  252.  
  253. ;******************END OF CODE************************
  254.