ARM Technical Support Knowledge Articles

ASSEMBLY CODE FOR CRC-16 FOR SDLC

Applies to: General Topics

Answer

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

Article last edited on: 2004-06-07 10:05:41

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential