Keil Logo

GENERAL: ASSEMBLY CODE FOR CRC-16 FOR SDLC

QUESTION

I need to implement CRC16 for a string that varies from 10 to 1000 bytes long. Where can I find an algorithm for that?

ANSWER

The following code generates the same CRC that is used by SDLC controllers:

typedef unsigned int word;
typedef unsigned char byte;

extern alien word crc_gen(byte b,word residue);

/* This makes it callable from C and PLM.
Residue must be seeded with a value.
crc_gen is called for every byte. */

/* Example: */

        byte message[20];
        byte i;
        word residue;

        residue=0;
        for(i=0;i<sizeof(message);i++)
        {
          residue=crc_gen(message[i],residue);
        }

/* The crc is now in residue. */

The following assembly code implements the CRC:

$title(16 bit crc for polynomial X16+X12+X5+1 for SDLC)
public  crc_gen,?crc_gen?byte,crc16
        ;CRC subroutine
        ;CRC uses most significant bit
        ;r6, r7 is CRC residue
        ;acc is data byte

crc_code segment code
crc_data segment data

        rseg    crc_data
?crc_gen?byte:
cdat:   ds      1
cres:   ds      2


        rseg    crc_code
        using   0

crc_gen:mov     a,cdat
        mov     r1,cres
        mov     r0,cres+1
crc16:  xrl     a,r1            ; xor data byte
        mov     r3,a            ; temp store in r3
        swap    a               ; rotate right four bits
        mov     r2,a            ; temp save in r2
        xrl     a,r3
        anl     a,#0f0h
        xrl     a,r0
        mov     r4,a            ; temp save
        mov     a,r2
        rl      a
        anl     a,#1fh          ; mask
        xrl     a,r4
        mov     r4,a            ; save
        mov     a,r3
        rl      a
        anl     a,#1
        xrl     a,r0
        xrl     a,r4
        mov     r7,a            ;low byte is complete
        mov     a,r2
        anl     a,#0fh
        xrl     a,r3
        mov     r1,a
        mov     a,r2
        xrl     a,r3
        rl      a
        anl     a,#0e0h
        xrl     a,r1
        mov     r6,a

        ret

        end

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Monday, June 7, 2004


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.