[comp.sys.ibm.pc.programmer] CCITT CRC-16, code w/o tables

JRD@cc.usu.edu (Joe Doupnik) (07/16/90)

CRC calculation, the fast way. From MS-DOS Kermit, file MSSCOM.ASM. Copyright
but free to use non-commercially.
	Joe D.
----------------------
; Calculate the CRC of the null-terminated string whose address is in BX.
; Returns the CRC in CX.  Destroys BX and AX.
; The CRC is based on the SDLC polynomial: x**16 + x**12 + x**5 + 1.
; By Edgar Butt  28 Oct 1987 [ebb].
; Enter with initial CRC in DX (normally 0).
crcclc: push	dx
	mov	cl,4			; load shift count
crc0:   mov	ah,[bx]			; get the next char of the string
        or	ah,ah			; if null, then we're done
        jz	crc1			; z = null, stop
        inc	bx
        xor	dl,ah			; XOR input with lo order byte of CRC
        mov	ah,dl			; copy it
        shl	ah,cl			; shift copy
        xor	ah,dl			; XOR to get quotient byte in ah
        mov	dl,dh			; high byte of CRC becomes low byte
        mov	dh,ah			; initialize high byte with quotient
        xor	al,al
        shr	ax,cl			; shift quotient byte
        xor	dl,ah			; XOR (part of) it with CRC
        shr	ax,1			; shift it again
        xor	dx,ax			; XOR it again to finish up
        jmp	short crc0
crc1:   mov	cx,dx			; return CRC in CX
        pop	dx
        ret
-------------------------------