[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Endless loop: uchar c; for (c=1; c; c++)
From: |
Dmitry K. |
Subject: |
Re: [avr-gcc-list] Endless loop: uchar c; for (c=1; c; c++) |
Date: |
Wed, 7 Mar 2007 12:31:09 +1000 |
User-agent: |
KMail/1.5 |
On Wednesday 07 March 2007 04:10, Eric Weddington wrote:
[...]
> It seems that when compiling to assembler (-S as Dmitry recommends above),
> then you'll note that the epilogue is set to noreturn, which also ends up
> taking out the final breq of the outer loop, as there is nothing to branch
> to anymore.
>
> However, when I compile and assemble, and I look at a requested listing
> from the assembler (basically using the WinAVR Makefile Template), the code
> looks fine. An epilog exists, and the correct end condition for the loop is
> in the code. This is also the case when I do a disassembly on the object
> code (avr-objdump -d <object>); the code looks fine, an epilog exists, and
> the branch is in the code.
>
> Does anyone have an idea why using avr-gcc -S ... would be so different
> then the actual code generated (as compared to the disassembly)?
I try to compile without `-S' option: result is the same.
More, the program below is run under the simulator (I use simulavr)
and exit with code 11 (20.2e6 cloks) which illustrates an error:
Program:
~~~~~~~
/* This program is compiled as endless loop: condition `c1 != 0'
is not tested. In original (real) program there was an another
error: in conditional expression based on c1 and c2 inside the
loops (here this is removed).
Compiler: avr-gcc 4.1.1
Options: -W -Wall -Os */
void exit (int);
void foo (const char *s1, const char *s2)
{
static unsigned char n;
if ((*s1 == 1) && (*s2 == 1)) {
/* The big number 10 is to enlarge a simulation time, number 1
is sufficient to demonstrate an error. */
if (++n > 10)
exit (n);
}
}
int main ()
{
unsigned char c1, c2;
char s1[2] = ".";
char s2[2] = ".";
for (c1 = 1; c1; c1++) {
for (c2 = 1; c2; c2++) {
s1[0] = c1;
s2[0] = c2;
foo (s1, s2);
}
}
return 0;
}
Disassemble:
~~~~~~~~~~~
...
0000007a <main>:
7a: cb e5 ldi r28, 0x5B ; 91
7c: d2 e0 ldi r29, 0x02 ; 2
7e: de bf out 0x3e, r29 ; 62
80: cd bf out 0x3d, r28 ; 61
82: 80 91 60 00 lds r24, 0x0060
86: 90 91 61 00 lds r25, 0x0061
8a: 9a 83 std Y+2, r25 ; 0x02
8c: 89 83 std Y+1, r24 ; 0x01
8e: 9c 83 std Y+4, r25 ; 0x04
90: 8b 83 std Y+3, r24 ; 0x03
92: 01 e0 ldi r16, 0x01 ; 1
94: 0d c0 rjmp .+26 ; 0xb0 <main+0x36>
96: 09 83 std Y+1, r16 ; 0x01
98: 1b 83 std Y+3, r17 ; 0x03
9a: 6c 2f mov r22, r28
9c: 7d 2f mov r23, r29
9e: 6d 5f subi r22, 0xFD ; 253
a0: 7f 4f sbci r23, 0xFF ; 255
a2: 8c 2f mov r24, r28
a4: 9d 2f mov r25, r29
a6: 01 96 adiw r24, 0x01 ; 1
a8: d4 df rcall .-88 ; 0x52 <foo>
aa: 1f 5f subi r17, 0xFF ; 255
ac: a1 f7 brne .-24 ; 0x96 <main+0x1c>
ae: 0f 5f subi r16, 0xFF ; 255
b0: 11 e0 ldi r17, 0x01 ; 1
b2: f1 cf rjmp .-30 ; 0x96 <main+0x1c>
...
Dmitry.
Re: [avr-gcc-list] Endless loop: uchar c; for (c=1; c; c++), Shaun Jackman, 2007/03/07