Arduino, C code and problem, for-loop, do-while-loop


8 Bit MCU, ATMEGA2560, Arduino 0022, the GCC and C++ library are used.
something is happened and the code is not functional as expected, this is going to see what/why and how…..

//C code, works fine

    for (int i = 0; i < 512; i += 2) {     // OK
    __asm ("nop");
    __asm ("pop r27");
    }

.

//assembler list and nemonics
    1b12:    80 e0           ldi    r24, 0x00    ; 0
    1b14:    90 e0           ldi    r25, 0x00    ; 0
    1b16:    00 00           nop
    1b18:    bf 91           pop    r27
    1b1a:    02 96           adiw    r24, 0x02    ; 2
    1b1c:    22 e0           ldi    r18, 0x02    ; 2
    1b1e:    80 30           cpi    r24, 0x00    ; 0
    1b20:    92 07           cpc    r25, r18
    1b22:    c9 f7           brne    .-14         ; 0x1b16 <_ZN3IDE20ideWriteSectorFasterEmh+0xee>

r25:r24 = i, 16 bit, init to zero
nop // used to easy trace asm code
pop r27 // used to easy trace asm code
adiw r24, 0x02 // i +=2, i = i +2
ldi r18, 0x02 // load 2 for comprasion
cpi R24, 0 // test i = ??00
cpc R25, 2 // test i = 0x0200 or 512
brne .14 = if test result is not equal to 0x0200, go back and loop for next i+2

.
.
.
.
.
.

//C code, not working but why !!!
  for (uint8_t i = 0; i <= 0xff; i++) {    //256 words, = 512 byte, NG why
    __asm ("nop");
    __asm ("pop r28");
}

.

//assembler list and nemonics, still do not know why!!!
1b0c:    10 93 0b 01     sts    0x010B, r17
1b10:    18 b9           out    0x08, r17    ; 8
1b12:    00 00           nop
1b14:    cf 91           pop    r28
1b16:    fd cf           rjmp    .-6          ; 0x1b12 <_ZN3IDE20ideWriteSectorFasterEmh+0xea>
1b18:    8f ef           ldi    r24, 0xFF    ; 255
1b1a:    80 93 0a 01     sts    0x010A, r24
1b1e:    87 b9           out    0x07, r24    ; 7
1b20:    10 92 0b 01     sts    0x010B, r1

excepts nop & pop, all other do not make sense if the for-loop with 8bit counter is working….but why ?? even there are no any asm generated for such loop and control…why ?
.
.
.
.
.

//C code, works fine
uint8_t i =0;
    __asm ("nop");
    __asm ("pop r29");
do {
    i++;
    __asm ("nop");
    __asm ("pop r30");
  }   while (i != 0) ;

.

//assembler list and nemonics
    1b12:    00 00           nop
    1b14:    df 91           pop    r29
    1b16:    80 e0           ldi    r24, 0x00    ; 0
    1b18:    00 00           nop
    1b1a:    ef 91           pop    r30
    1b1c:    81 50           subi    r24, 0x01    ; 1
    1b1e:    e1 f7           brne    .-8          ; 0x1b18 <_ZN3IDE20ideWriteSectorFasterEmh+0xf0>

this is working perfectly and assembled code is much consistent to the C code, only 8 bit counter is used and no more reduntant code or assembly!

nop // trace asm easy
pop r29 // trace asm easy
ldi r24, 0x00 // uint8_t i = 0, 8 bit counter value exactly
nop // trace asm easy for do-loop
pop r30 // trace asm easy do-lop
subi r24, x01 // i = i -1, 0-1 = -1 = 0xff
brne .-8 // if not zero, go loop next

.
.
.
.
.

//C code, works fine
uint8_t i =0;
    __asm ("nop");
    __asm ("pop r29");
do {
    i++;
    __asm ("nop");
    __asm ("pop r30");
  }   while (i != 10) ;

.

//assembler list and nemonics
    1b12:	00 00       	nop
    1b14:	df 91       	pop	r29
    1b16:	80 e0       	ldi	r24, 0x00	; 0
    1b18:	8f 5f       	subi	r24, 0xFF	; 255
    1b1a:	00 00       	nop
    1b1c:	ef 91       	pop	r30
    1b1e:	8a 30       	cpi	r24, 0x0A	; 10
    1b20:	d9 f7       	brne	.-10     	; 0x1b18 <_ZN3IDE20ideWriteSectorFasterEmh+0xf0>

how about if condition of test of i is not looping from 0 to 0, but 0 to 10 ?
oh….bug or……?

廣告

3 thoughts on “Arduino, C code and problem, for-loop, do-while-loop

  1. Modern compiler is capable to optimize this Always True conditions. So it become a forever loop in assembly( rjmp xxxx).

    1. this is interesting to know what if such for-loop will works out.
      “i++" or “++i" was not matter to such code generation and no effect at all. seems the C++ compiler has done something and evaluates “i" before process any loop & code generating, and then no looping code generated.

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s