This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ASM CRC

Hello All,

I have this asm crc calculation code:

            ADDCHECK   PUSH    ACC                 ;for 8031
                         ADD     A,CHEKSUM
                         RLC     A
                         JC      ADCK3
                           XRL     A,#11011101b    ;(#0DDh)
            ADCK3        MOV     CHEKSUM,A
                         CLR     C                 ;if CHK >= 244 then CHK= CHK-244
                         SUBB    A,#224            ;(#0E0h)
                         JC      ADCK4
                           MOV     CHECKSUM,A      ;<CHK> always < 224 !!!
            ADCK4      POP     ACC
                       RET
;==============================================================

            ADDCHECK:  PUSH    AX                  ;for 8086
                         ADD     AL,CHECKSUM
                         RCL     AL,1
                         JC      ADCK3
                           XOR     AL,11011101b
            ADCK3:       MOV     CHECKSUM,AL
                         SUB     AL,224
                         JC      ADCK4
                           MOV     CHECKSUM,AL
            ADCK4:     POP     AX
                       RET


I want to adapt for my current MCU C8051F340, as included asm code or "C" source code.
(I'm "C" proggramer). Can you help me?

Thank you for Attention!

  • Is there actually a shortage of CRC implementations in 'C' ... ?

    Anyhow, here's how to create a C51 assembler module callable from 'C':

    www.8052.com/.../149030

  • Hello Mr.Neil,
    This is my "C" code. I think it work correct. I have no experience with 8051 assembler and especially with Silabs C8051Fxxx!

    #define GET_CERRY_BIT(X) ((X >> 8) & 1)
    
    
    unsigned char GetCrc(unsigned char *buffer, unsigned char bufsize) {
    
            unsigned char i;
       unsigned int AL;
       unsigned char checksum,temp;
       bit carry;
    
       if(bufsize == 0)
          return(0);
    
       checksum = 0;
    
       for(i = 0; i < bufsize; i++) {
    
        AL = buffer[i];
    
        AL+=checksum;
    
        if (AL & 0x100){
    
                            AL <<= 1;
                            AL |= 1;
                            carry = GET_CERRY_BIT(AL);
                    }
        else{
    
                            AL <<= 1;
                            carry = GET_CERRY_BIT(AL);
                    }
        if (carry==0)
           AL^=0xDD;
    
        temp = (unsigned char)AL;
    
        if (temp < 224)
           checksum = temp;
        else
           checksum = temp-224;
       }
       return checksum;
    }
    

  • >>I think it work correct.

    Perhaps you have some example data with a sum so you can actually validate it?

    Testing it against the x86 code suggests it is good

    unsigned char GetCrc2(unsigned char *buffer, size_t bufsize) // sourcer32@gmail.com
    {
      unsigned short checksum = 0;
    
      while(bufsize--)
      {
        checksum += *buffer++;
    
        checksum <<= 1;
    
        if ((checksum & 0x200) != 0) // ADD carry
          checksum |= 1;
    
        if ((checksum & 0x100) == 0) // RCL carry
          checksum ^= 0xDD;
    
        checksum &= 0xFF; // Modulo 256
    
        if (checksum >= 0xE0) // Modulo 224
          checksum -= 0xE0;
      }
    
      return((unsigned char)checksum);
    }
    
    int main(int argc, char **argv)
    {
      unsigned char test1[] = { 0x01,0x02,0x03,0x04,0x05,0x06 }; // 0x4A
      unsigned char test2[] = { 0x11,0x22,0x33,0x44,0x55,0x66 }; // 0x23
      unsigned char test3[] = { 0x66,0x55,0x44,0x33,0x22,0x11 }; // 0x59
    
      printf("%02X\n", test(test1, sizeof(test1))); // Assembler
      printf("%02X\n", test(test2, sizeof(test2)));
      printf("%02X\n", test(test3, sizeof(test3)));
      putchar('\n');
    
      printf("%02X\n", GetCrc(test1, sizeof(test1))); // Yours
      printf("%02X\n", GetCrc(test2, sizeof(test2)));
      printf("%02X\n", GetCrc(test3, sizeof(test3)));
      putchar('\n');
    
      printf("%02X\n", GetCrc2(test1, sizeof(test1))); // Mine
      printf("%02X\n", GetCrc2(test2, sizeof(test2)));
      printf("%02X\n", GetCrc2(test3, sizeof(test3)));
      putchar('\n');
    
      return(1);
    }