I'm generating a CRC by passing the initial value, length of array and a pointer to the array then returning the CRC from the CRC generator module which is split across 4 8 bit wide registers in memory. I'm getting some differing output code results when returning the CRC value (16 bit):
return CRC.CHECKSUM1 << 8 | CRC.CHECKSUM0; 5132: 30 91 d5 00 lds r19, 0x00D5 5136: 20 91 d4 00 lds r18, 0x00D4 513a: 93 2f mov r25, r19 513c: 80 e0 ldi r24, 0x00 ; 0 513e: ac 01 movw r20, r24 5140: 42 2b or r20, r18 5142: 9a 01 movw r18, r20 } 5144: c9 01 movw r24, r18 5146: 08 95 ret
uint16_t result; uint8_t * res8 = (uint8_t*)&result; res8 = CRC.CHECKSUM0; 513e: 80 91 d4 00 lds r24, 0x00D4 5142: 89 83 std Y+1, r24 ; 0x01 res8 = CRC.CHECKSUM1; 5144: 90 91 d5 00 lds r25, 0x00D5 return result;
uint8_t res8; uint16_t * result = (uint16_t*)&res8; res8 = CRC.CHECKSUM0; 5132: 80 91 d4 00 lds r24, 0x00D4 res8 = CRC.CHECKSUM1; 5136: 90 91 d5 00 lds r25, 0x00D5 return *result; } 513a: 08 95 ret
Easily 3 is the best efficiency but a round the houses way of doing it. is there an expression like in 1 that would produce code like 3?
I'm not understanding why Y is being written to in 2 either.
The function looks like this:
uint16_t calcCRC_CCITT(uint16_t crc, uint8_t len, uint8_t * data)
I've just put the code that matters to try and make it readable.
I'm trying the GCC compiler that comes with studio 6.1 and haven't run this through WINAVR yet.
Apologies if this has been answered before I did a quick search but did not return anything that seemed to fit.