Error on While loop

OP #1173968
Rate this post
• ▲ useful
▼ not useful
Hi there :-)

Im building on a project with the STM32. So using the GCC V 4.3.0
(WinARM_20080331_testing) I got into the following problem:

the usual

while( a <50){
   ++a;
}

works just fine! Then one time I used :

{
   ++a;
}while( a<50 );

of course with much more useful contend, than just incrementing a :-)
The result was: it didn't work. But wait: I debugged it with
Insight/OOCD and it went like this:
# entered loop
# executed statements (like ++a;) (checked with memory watch)
# checked condition (which was true, thus not quitting loop)
# DID NOT EXECUTE LOOP AGAIN ????

I did some research and found out, that the jump-mark, to which he
should jump on re-doing the loop was not the beginning of the loop but
the end, thus executing while for infinity (because the condition was
always true).
I thought "OK may be there was something wrong with my setup" so I tried
this:

made 'test.c' with the following contend (and really nothing else):
-------------------------------------------------------------------
int main(){
  int b=4;
  {
    ++b;
  }while( b<100);
  return b;
}
-------------------------------------------------------------------
below you will find the disambling done with insight. You can see that
the jump-mark ('<main+20>')is pointing to the beginning of 'while' not
the beginning of the loop. The cmd to compile was the following:
'arm-eabi-gcc-4.3.0 -mthumb -mcpu=cortex-m3 -g -c test.c -o test.o'
-------------------------------------------------------------------
  4  int main(){
-  0x0  <main>:    push  {r7}
-  0x2  <main+2>:    sub  sp, #12
-  0x4  <main+4>:    add  r7, sp, #0
  5    int b=4;
-  0x6  <main+6>:    mov.w  r3, #4  ; 0x4
-  0xa  <main+10>:    str  r3, [r7, #4]
  6
  7    {
  8      ++b;
-  0xc  <main+12>:    ldr  r3, [r7, #4]
-  0xe  <main+14>:    add.w  r3, r3, #1  ; 0x1
-  0x12  <main+18>:    str  r3, [r7, #4]
  9    }while( b<100);
-  0x14  <main+20>:    ldr  r3, [r7, #4]
-  0x16  <main+22>:    cmp  r3, #99
-  0x18  <main+24>:    ble.n  0x14 <main+20>
  10
  11    return b;
-  0x1a  <main+26>:    ldr  r3, [r7, #4]
-  0x1c  <main+28>:    mov  r0, r3
-  0x1e  <main+30>:    add.w  r7, r7, #12  ; 0xc
-  0x22  <main+34>:    mov  sp, r7
-  0x24  <main+36>:    pop  {r7}
-  0x26  <main+38>:    bx  lr
-------------------------------------------------------------------

Is this a bug in GCC (I searched the bug list but couldn't find
anything). I cannot imagine that nobody found that before ???
Btw. the sourcery g++ version of GCC did exactly the same.

HELP :-)
#1173969
Rate this post
• ▲ useful
▼ not useful
> made 'test.c' with the following contend (and really nothing else):
> -------------------------------------------------------------------
> int main(){
>   int b=4;
>   {
>     ++b;
>   }while( b<100);
>   return b;
> }


Hi, I'm no expert but I think the syntax for the loop is missing the
'do'.
Maybe you could try a do/while instead.

 do{
    ++b;
  }while( b<100);
#1173970
Rate this post
• ▲ useful
▼ not useful
Lain Iwakura wrote:
> Is this a bug in GCC (I searched the bug list but couldn't find
> anything). I cannot imagine that nobody found that before ???
> Btw. the sourcery g++ version of GCC did exactly the same.
>
> HELP :-)

I think you are missing the "do" part of the while statement. It should
be:

  int b=4;
  do {
    ++b;
  }while( b<100);

regards,
Giovanni
---
ChibiOS/RT http://chibios.sourceforge.net/

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.