Forums - Compiler bug? byte array issue

1 post / 0 new
Compiler bug? byte array issue
Join Date: 29 Jul 16
Posts: 25
Posted: Wed, 2016-11-23 13:36

I am writing data to a large flash memory and wanted to follow the write with a read and verify operation. The verify code reported a failure even though subsequent investigations showed that the flash write had been successful. So the problem lies with the verification code.

Because the flash read and write code acts on 16-bit words and my original data is in an 8-bit byte array, my verification code does a comparison of words, and collates two of the bytes with this line (inside a loop):

valueWord = value[j] + (value[j+1] << 8);

This line produces an error when j=0 (but not when j>0 - the problem only happens the first time round the loop). On the other hand, this line always gives the expected result:

valueWord = (value[j] & 0xff) + (value[j+1] << 8);

It should not be necessary to mask off the MS bits of value[j] since it should be treated as an 8-bit quantity. IMHO this is a bug.
Behaviour is consistent with this: the first time round the loop the value[j] is treated as a 16-bit word, and the value accumulating in valueWord includes the next byte in the array treated as the 8 MS bits of a 16-bit word. This next byte is value[j+1], with the overall effect:

valueWord = ( value[j] + (value[j+1] << 8) ) + (value[j+1] << 8);

So for example when value[0] = 0x30 and value[1] = 0x01 the correct value of the 16-bit word should be 0x0130
but instead valueWord = 0x0230.

This hypothesis is bourne out by this code:
PRINT("[%04x %04x %04x] ", valueWord, value[j] + 0x100, (value[j] & 0xff) + 0x100);

which gives this result (the first time round the loop, when j=0): [0230 0230 0130]

I have the compiler list files if anyone wants to check teh code (I don't understand the assembler, so can't see where the bug lies).

Regards - Charles

  • Up0
  • Down0

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.