home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CRC_ASM.ZIP / CRC.ASM
Assembly Source File  |  1989-03-19  |  3KB  |  130 lines

  1. title XMODEM CRC Algorithm 8086 / Lattice C Compiler
  2. page ,132
  3. include model.ash
  4. include lattice.ash
  5. ;*                                    *
  6. ;*    clrcrc();                            *
  7. ;*    CLRCRC - A call to this entry resets the CRC accumulator.    *
  8. ;*         It must be called at the start of each message.    *
  9. ;*                                    *
  10. ;*         Entry Parameters: None.                *
  11. ;*                                    *
  12. ;*         Exit Conditions:  CRC accumulator cleared.        *
  13. ;*                                    *
  14. ;*    udcrc(c);                            *
  15. ;*    char c;                                *
  16. ;*    UPDCRC - A call to this entry updates the CRC accumulator.    *
  17. ;*         It must be called once for each byte in the        *
  18. ;*         message for which the CRC is being calculated.        *
  19. ;*                                    *
  20. ;*         Entry Parameters:       a byte to be included        *
  21. ;*                     in the CRC calculation.    *
  22. ;*                                    *
  23. ;*         Exit Conditions:  CRC accumulator updated.        *
  24. ;*                                    *
  25. ;*                                    *
  26. ;*    crc= fincrc();                            *
  27. ;*    unsigned crc;                            *
  28. ;*    FINCRC - A call to this entry finishes the CRC calculation    *
  29. ;*         for a message which is to be TRANSMITTED. It must    *
  30. ;*         be called after the last byte of the message has    *
  31. ;*         been passed thru UPDCRC. It returns the calculated    *
  32. ;*         CRC bytes, which must be transmitted as the final    *
  33. ;*         two bytes of the message.                *
  34. ;*                                    *
  35. ;*        Note that this returns a single 16 bit value;         *
  36. ;*        if transmitting bytes, it must be transmitted         *
  37. ;*        upper half first then lower half:            *
  38. ;*                                    *
  39. ;*            output (crc >> 8);                *
  40. ;*            output (crc);                    *
  41. ;*                                    *
  42. ;*                                    *
  43. ;*         Entry Parameters: None.                *
  44. ;*                                    *
  45. ;*         Exit Conditions:  calculated CRC bytes.        *
  46. ;*                                    *
  47. ;*    error= chkcrc();                        *
  48. ;*    int error;                            *
  49. ;*    CHKCRC - A call to this routine checks the CRC bytes of        *
  50. ;*         a RECEIVED message and returns a code to indicate    *
  51. ;*         whether the message was received correctly. It must    *
  52. ;*         be called after the message AND the two CRC bytes    *
  53. ;*         have been received AND passed thru UPDCRC.        *
  54. ;*                                    *
  55. ;*         Entry Parameters: None.                *
  56. ;*                                    *
  57. ;*         Exit Conditions:  0 if message ok.            *
  58. ;*                   non-zero if message garbled.        *
  59. ;*                                    *
  60. page
  61. ;
  62. ;Our very own little bit of data space.
  63. ;
  64. dseg
  65. crc    dw (?)        ;calculated CRC,
  66. endd
  67.  
  68. cseg
  69. ;
  70. ;Initialize the CRC.
  71. ;
  72. func clrcrc
  73.     mov    ax,data
  74.     mov    ds,ax
  75.     mov    crc,0
  76. endf clrcrc
  77. ;
  78. ;Update the CRC with the new byte. The method used is 
  79. ;the CCITT polynomial:
  80. ;
  81. ;    x^16 + x^12 + x^5 + 1
  82. ;
  83. ;An alternate method often used in synchronous
  84. ;protocols is:
  85. ;
  86. ;    x^16 + x^15 + x^2 + 1
  87. ;
  88. ;Which can be generated by changing the XOR pattern
  89. ;from 1021 hex to 8005 hex.
  90. ;
  91. func updcrc
  92.     mov    ax,data
  93.     mov    ds,ax
  94.     mov    bx,crc        ;BX == CRC a reg for speed,
  95.     mov    cx,8        ;CX == bits in a byte,
  96.     mov    ax,arg0        ;AL == msg byte,
  97.  
  98. u1:    rcl    al,1        ;MSB -> carry,
  99.     rcl    bx,1        ;    -> CRC LSB,
  100.     jnc    u2
  101.     xor    bx,1021h
  102. u2:    loop    u1
  103.  
  104.     mov    crc,bx
  105. endf updcrc
  106. ;
  107. ;Finish off the CRC.
  108. ;
  109. func fincrc
  110.     mov    ax,data
  111.     mov    ds,ax
  112.     xor    ax,ax        ;do two zeros
  113.     push    ax
  114.     call    updcrc
  115.     call    updcrc
  116.     pop    ax
  117.     mov    ax,crc        ;return finished CRC
  118. endf fincrc
  119. ;
  120. ;Check the calculated CRC. Return 0 if OK.
  121. ;
  122. func chkcrc
  123.     mov    ax,data
  124.     mov    ds,ax
  125.     mov    ax,crc
  126. endf chkcrc
  127.  
  128. endc
  129.     end
  130.