Re: Incorrect code

Ilya Lesokhin <[email protected]>
Newsgroups gmane.comp.hardware.avr.gcc
Message-ID <CAEPKs0fSzojZZryVH8C-PZ8LUMRyL87Ok0XdzCQ9Kw_dciRV=Q@mail.gmail.com>
you didn't assign anything to ptr1. so *ptr1++ = *ptr2++; is undefined.


On Sat, Aug 9, 2014 at 3:51 PM, Thomas D. Dean <[email protected]>
wrote:

> Is this a problem with -Os?
>
> It looks like the compiler has decided that cs is always zero in the code
> below.  The cs calculation appears correct.
>
> In spi_copy.c
>
> ...
> uint8_t spi_copy_in(uint8_t *dest, uint8_t *source, unsigned int max_len) {
>   uint8_t cs;
>   uint8_t len;
>   uint8_t *ptr1, *ptr2;
>
>   cs = 0;
>   len = source[0];
>   if (len > max_len) return 0xff;
>   ptr2 = &source[1];
>   do {
>         cs += *ptr2++;
>   } while (--len);
>   if (cs == 0) {
>         len = source[0] - 1; // do not need the checksum
>         ptr2 = &source[1];
>         do {
>           *ptr1++ = *ptr2++;
>         } while (--len);
>   }
>   return cs;
> }
> ...
>
> > make
> ...
> avr-gcc -Os -Wall -Wmissing-prototypes  -fno-strict-aliasing -Wall
> -I../include -I../../Class -mmcu=atmega128 -c -o spi_copy.o
> ../../Class/spi_copy.c
> ...
>
> > avr-objdump -d ng02.elf | more
> ...
>
> 0000077c <spi_copy_in>:
>  77c:   fb 01           movw    r30, r22
>  77e:   20 81           ld      r18, Z
>  780:   82 2f           mov     r24, r18
>  782:   90 e0           ldi     r25, 0x00       ; 0
>  784:   48 17           cp      r20, r24
>  786:   59 07           cpc     r21, r25
>  788:   38 f0           brcs    .+14            ; 0x798 <spi_copy_in+0x1c>
>  78a:   31 96           adiw    r30, 0x01       ; 1
>  78c:   80 e0           ldi     r24, 0x00       ; 0
>  78e:   91 91           ld      r25, Z+
>  790:   89 0f           add     r24, r25
>  792:   21 50           subi    r18, 0x01       ; 1
>  794:   e1 f7           brne    .-8             ; 0x78e <spi_copy_in+0x12>
>  796:   08 95           ret
>  798:   8f ef           ldi     r24, 0xFF       ; 255
>  79a:   08 95           ret
>
> Tom Dean
>
> _______________________________________________
> AVR-GCC-list mailing list
> [email protected]
> https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
>

_______________________________________________
AVR-GCC-list mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.