home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_10 / 1010058b < prev    next >
Text File  |  1992-08-10  |  3KB  |  158 lines

  1. ;   FILE: upd32.asm
  2. ;   DATE: 910917:1711
  3. ;   LMOD: 911112:1637
  4. ;   FOR:  calculate crc-32 of buffer contents
  5. ;
  6.       TITLE UPD32 - crc calculation
  7.       comment #
  8. DYNAMICRO CONSULTING LIMITED, 1991
  9. """"""""""""""""""""""""""""""""""
  10. This module implements 32-bit cyclic redundancy
  11. checksum calculation.  Routine _upd32 is
  12. called with the checksum and byte pointer and
  13. length of string to calculate on the stack,
  14. and returns with the new checksum in bx&dx:ax
  15. (bx:ax for MSC, dx:ax for ECO)
  16. #
  17. ; segment and group definitions
  18.       include amodel.inc
  19.       if    ECO        ; Ecosoft C 4.13
  20.       include \eco\headers\pro.h
  21. $d$dataseg segment public 'data2'
  22.       endif
  23.       if    MSC        ; Microsoft C 6.00
  24.       .DATA
  25.       endif
  26.  
  27. ; the following tables were produced by program
  28. ; mkcrc32.c (listing 3).  Since you can obtain
  29. ; them in full by running that program, we're
  30. ; omitting most of them here to save listing space.
  31.  
  32. ; crc tables: CRC-32, polynomial 04c11db7
  33.  
  34. crct0      db    000h,004h,009h,00dh    ; 00-03
  35.       db    013h,017h,01ah,01eh    ; 04-07
  36.       db    026h,022h,02fh,02bh    ; 08-0b
  37. ; ...
  38.       db    0afh,0abh,0a6h,0a2h    ; f8-fb
  39.       db    0bch,0b8h,0b5h,0b1h    ; fc-ff
  40.  
  41. crct1      db    000h,0c1h,082h,043h    ; 00-03
  42.       db    004h,0c5h,086h,047h    ; 04-07
  43.       db    008h,0c9h,08ah,04bh    ; 08-0b
  44. ; ...
  45.       db    0b0h,071h,032h,0f3h    ; f8-fb
  46.       db    0b4h,075h,036h,0f7h    ; fc-ff
  47.  
  48. crct2      db    000h,01dh,03bh,026h    ; 00-03
  49.       db    076h,06bh,04dh,050h    ; 04-07
  50.       db    0edh,0f0h,0d6h,0cbh    ; 08-0b
  51. ; ...
  52.       db    010h,00dh,02bh,036h    ; f8-fb
  53.       db    066h,07bh,05dh,040h    ; fc-ff
  54.  
  55. crct3      db    000h,0b7h,06eh,0d9h    ; 00-03
  56.       db    0dch,06bh,0b2h,005h    ; 04-07
  57.       db    0b8h,00fh,0d6h,061h    ; 08-0b
  58. ; ...
  59.       db    0b1h,006h,0dfh,068h    ; f8-fb
  60.       db    06dh,0dah,003h,0b4h    ; fc-ff
  61.  
  62.       if    ECO
  63. $d$dataseg  ends
  64.       endif
  65.       page
  66.       public _upd32
  67.       if    ECO
  68.       if    BIGCODE
  69. $c$_upd32 segment word public 'code'
  70.       else
  71. $b$prog      segment public 'code'
  72.       endif
  73.       endif
  74.       if    MSC
  75.       .CODE
  76.       endif
  77. ; int upd32(long crc,char *buf,int len) {
  78. ; /*
  79. _upd32      proc
  80.       push    bp
  81.       mov    bp,sp
  82.       push    si
  83.       push    di
  84.       if    BIGDATA
  85.       push    ds
  86.       push    es
  87.       mov    ax,seg DGROUP
  88.       mov    ds,ax
  89.       mov    ax,[bp][6+PARMLOC]    ; buf,seg
  90.       mov    es,ax
  91.       endif
  92.       mov    ax,[bp][PARMLOC]    ; crc,low
  93.       mov    dx,[bp][2+PARMLOC]    ; crc,hi
  94.       mov    si,[bp][4+PARMLOC]    ; buf,ofs
  95.       if    BIGDATA
  96.       mov    cx,[bp][8+PARMLOC]    ; len
  97.       else
  98.       mov    cx,[bp][6+PARMLOC]    ; len
  99.       endif
  100. updl:
  101. ; do { c code is presented in comments
  102. ;     get the next byte to process
  103.       mov    di,ax            ; crc,low
  104.       if    BIGDATA
  105.       mov    al,es:[si]        ; byte
  106.       inc    si
  107.       else
  108.       lodsb                ; byte
  109.       endif
  110.  
  111. ; p=crct0+(al^((crc>>24)&0xff);
  112.       mov    bx,offset DGROUP:crct0    ; table
  113. ;     xor byte with high 8 bits of old crc
  114.       xor    al,dh
  115. ;     add to crct0 base addr
  116.       xor    ah,ah
  117.       add    bx,ax    ; bx is p
  118.       mov    ax,di    ; crc,low
  119.  
  120. ; crc=((*p^((crc>>16)&0xff))<<24)+
  121. ;     ((*(p+256)^((crc>>8)&0xff))<<16)+
  122. ;     ((*(p+512)^(crc&0xff))<<8)+*(p+768);
  123.       xor    dl,[bx]    ; *p ^ old crc's 3rd byte
  124.       mov    dh,dl    ; to msb
  125.       inc    bh    ; p+256
  126.       xor    ah,[bx] ; xor old crc's 2nd byte
  127.       mov    dl,ah    ; put in 3rd
  128.       inc    bh    ; p+512
  129.       xor    al,[bx] ; xor old crc's lsb
  130.       mov    ah,al    ; to second byte
  131.       inc    bh    ; p+768
  132.       mov    al,[bx]    ; add *(p+768)
  133. ;     hand compilation has its advantages!!!
  134.  
  135. ; } while (--len);
  136.       loop    updl
  137.  
  138.       mov    bx,dx    ; copy crc hi to bx
  139.       if    BIGDATA
  140.       pop    es
  141.       pop    ds
  142.       endif
  143.       pop    di
  144.       pop    si
  145.       pop    bp
  146.       ret    ; new crc in dx/bx:ax
  147. _upd32      endp
  148.  
  149.       if    ECO
  150.       if    BIGCODE
  151. $c$_upd32 ends
  152.       else
  153. $b$prog      ends
  154.       endif
  155.       endif
  156.       end
  157. ; */ }
  158.