

Task

Demonstrate a method of deriving the Cyclic Redundancy Check from within the language.



The result should be in accordance with ISO 3309, ITU-T V.42, Gzip and PNG.

Algorithms are described on Computation of CRC in Wikipedia.
This variant of CRC-32 uses LSB-first order, sets the initial CRC to FFFFFFFF16, and complements the final CRC.

For the purpose of this task, generate a CRC-32 checksum for the ASCII encoded string:

 The quick brown fox jumps over the lazy dog


; link with PIOxxx.OBJ
 
        sectionbss
 
crctable.d:     resd 256
 
        sectiondata
 
havetable.b:    db 0
string:         db "The quick brown fox jumps over the lazy dog"
stringend:      
                db 13,10,0        ; carriage return and null terminator
 
        sectioncode
 
start!
        gosub initplatform
 
        beginfunc
        localvar crc.d
 
        callex ,printnt,"input = ".a
        callex ,printnt,string
 
        callex ,printnt,"The CRC-32 checksum = ".a
        callex crc,crc32,string,stringend
        callex ,printhexr,crc
 
        endfunc
        end
 
crc32:
        beginfunc bufend.d,buf.d
        localvar i.d,j.d,k.d,k2.d,crc.d
 
        ifunequal havetable,0,tabledone
        i=0
        whileless i,256
 
        k=i > j=8
        countdown j
        k2=k > k=_ shr 1
        ifequal k2 and 1,0,noxor > k=_ xor $EDB88320
noxor:
        nextcount
 
        crctable(i shl 2)=k
        i=_+1
        wend
 
        havetable=1
tabledone:
 
        crc=-1        
 
        whileless buf,bufend
        crc=_ shr 8 xor crctable(crc and $FF xor [buf].b shl 2)
        buf=_+1
        wend
 
        crc=_ xor -1
        endfunc crc
        returnex 8                        ; clean off 2 parameters from the stack
 