avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Incorrect Code?


From: User Tomdean
Subject: [avr-gcc-list] Incorrect Code?
Date: Mon, 25 Jul 2005 15:50:09 -0700 (PDT)

# avr-gcc --version
avr-gcc (GCC) 3.4.3

Optimization causes incorrect code.  Optimization, determining that I
an thru with a variable, optimizes it away, causing incorrect results.

tomdean

The code:

  char *bin_to_hex_ascii(uint16_t i, char *p) {
    unsigned int value;
    unsigned char c;
    *--p = 0;
    value = i;
    do {
      c = value % 16;
      value = value / 16;
          if (c < 10) *--p = c + '0';
      else        *--p = c + '7';
    } while (value != 0);
    return p;
  }

int main() {
...
  {
    char buf[]="0000";
    (void)bin_to_hex_ascii(0xDead,&buf[3]);
    serial_print(buf);
  }
  {
    serial_send('\n');
    (void)bin_to_hex_ascii(0xBeef,&buf[3]);
    serial_print(buf);
    serial_send('\n');
  }
...
}

Produces:

  f0:   8d ea           ldi     r24, 0xAD       ; 173
  f2:   9e ed           ldi     r25, 0xDE       ; 222
  f4:   0e 94 c5 00     call    0x18a <bin_to_hex_ascii>
  f8:   c8 01           movw    r24, r16
  fa:   0e 94 ad 00     call    0x15a <serial_print>
  fe:   8a e0           ldi     r24, 0x0A       ; 10
 100:   0e 94 c1 00     call    0x182 <serial_send>
 104:   b7 01           movw    r22, r14
 106:   8f ee           ldi     r24, 0xEF       ; 239
 108:   9e eb           ldi     r25, 0xBE       ; 190
 10a:   0e 94 c5 00     call    0x18a <bin_to_hex_ascii>
 10e:   c8 01           movw    r24, r16
 110:   0e 94 ad 00     call    0x15a <serial_print>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]