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

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

Re: [avr-gcc-list] Extremly confusing behaviour... (Suspectcompiler?, op


From: Mike Panetta
Subject: Re: [avr-gcc-list] Extremly confusing behaviour... (Suspectcompiler?, optimization error?)
Date: 14 May 2003 14:14:12 -0400

Ohh that may be it then...  According to the FAQ regs 18-27 are
"Call-Used" registers, so they should be saved before calling a function
if you use them.  On second look, it seems that srf08_read does not use
r18 or r19, so I have no clue what the problem is...  All this ASM code
is making my vision go blurry :P

For refrence the code is (again, sorry I recompiled) pasted below.

I have recompiled it since so here is a new listing of the do {} while;
loop:

                do
     1d0:       0f 90           pop     r0
     1d2:       0f 90           pop     r0
                {
                        delay = 1000;
     1d4:       88 ee           ldi     r24, 0xE8       ; 232
     1d6:       93 e0           ldi     r25, 0x03       ; 3
     1d8:       a0 e0           ldi     r26, 0x00       ; 0
     1da:       b0 e0           ldi     r27, 0x00       ; 0
     1dc:       89 83           std     Y+1, r24        ; 0x01
     1de:       9a 83           std     Y+2, r25        ; 0x02
     1e0:       ab 83           std     Y+3, r26        ; 0x03
     1e2:       bc 83           std     Y+4, r27        ; 0x04
                        while (delay--);
     1e4:       89 81           ldd     r24, Y+1        ; 0x01
     1e6:       9a 81           ldd     r25, Y+2        ; 0x02
     1e8:       ab 81           ldd     r26, Y+3        ; 0x03
     1ea:       bc 81           ldd     r27, Y+4        ; 0x04
     1ec:       01 97           sbiw    r24, 0x01       ; 1
     1ee:       a1 09           sbc     r26, r1
     1f0:       b1 09           sbc     r27, r1
     1f2:       89 83           std     Y+1, r24        ; 0x01
     1f4:       9a 83           std     Y+2, r25        ; 0x02
     1f6:       ab 83           std     Y+3, r26        ; 0x03
     1f8:       bc 83           std     Y+4, r27        ; 0x04
     1fa:       89 81           ldd     r24, Y+1        ; 0x01
     1fc:       9a 81           ldd     r25, Y+2        ; 0x02
     1fe:       ab 81           ldd     r26, Y+3        ; 0x03
     200:       bc 81           ldd     r27, Y+4        ; 0x04
     202:       8f 5f           subi    r24, 0xFF       ; 255
     204:       9f 4f           sbci    r25, 0xFF       ; 255
     206:       af 4f           sbci    r26, 0xFF       ; 255
     208:       bf 4f           sbci    r27, 0xFF       ; 255
     20a:       61 f7           brne    .-40            ; 0x1e4
                        PORTA |= _BV (PA1);
     20c:       d9 9a           sbi     0x1b, 1 ; 27
                        retval = srf08_read(1, 0, pingdata, 2);
     20e:       35 e0           ldi     r19, 0x05       ; 5
     210:       c3 2e           mov     r12, r19
     212:       d1 2c           mov     r13, r1
     214:       cc 0e           add     r12, r28
     216:       dd 1e           adc     r13, r29
     218:       02 e0           ldi     r16, 0x02       ; 2
     21a:       10 e0           ldi     r17, 0x00       ; 0
     21c:       20 e0           ldi     r18, 0x00       ; 0
     21e:       30 e0           ldi     r19, 0x00       ; 0
     220:       a6 01           movw    r20, r12
     222:       60 e0           ldi     r22, 0x00       ; 0
     224:       81 e0           ldi     r24, 0x01       ; 1
     226:       0e 94 56 04     call    0x8ac
                        PORTA &= ~(_BV(PA1));
     22a:       d9 98           cbi     0x1b, 1 ; 27
                        PORTB = retval;
     22c:       68 bb           out     0x18, r22       ; 24
                } while (retval != 2);
     22e:       60 17           cp      r22, r16
     230:       71 07           cpc     r23, r17
     232:       82 07           cpc     r24, r18
     234:       93 07           cpc     r25, r19
     236:       71 f6           brne    .-100           ; 0x1d4

Here is the listing from srf08_read:
000008ac <srf08_read>:

int32_t
srf08_read(uint8_t address, uint8_t reg, uint8_t *data, int32_t len)
{
     8ac:       6f 92           push    r6
     8ae:       7f 92           push    r7
     8b0:       8f 92           push    r8
     8b2:       9f 92           push    r9
     8b4:       af 92           push    r10
     8b6:       bf 92           push    r11
     8b8:       df 92           push    r13
     8ba:       ef 92           push    r14
     8bc:       ff 92           push    r15
     8be:       0f 93           push    r16
     8c0:       1f 93           push    r17
     8c2:       cf 93           push    r28
     8c4:       df 93           push    r29
     8c6:       cd b7           in      r28, 0x3d       ; 61
     8c8:       de b7           in      r29, 0x3e       ; 62
     8ca:       21 97           sbiw    r28, 0x01       ; 1
     8cc:       0f b6           in      r0, 0x3f        ; 63
     8ce:       f8 94           cli
     8d0:       de bf           out     0x3e, r29       ; 62
     8d2:       0f be           out     0x3f, r0        ; 63
     8d4:       cd bf           out     0x3d, r28       ; 61
     8d6:       d8 2e           mov     r13, r24
     8d8:       69 83           std     Y+1, r22        ; 0x01
     8da:       3a 01           movw    r6, r20
     8dc:       48 01           movw    r8, r16
     8de:       59 01           movw    r10, r18
    int32_t retval;

        if (address == 0)
     8e0:       88 23           and     r24, r24
     8e2:       09 f4           brne    .+2             ; 0x8e6
     8e4:       a8 c0           rjmp    .+336           ; 0xa36
                return -1;
        if (address > 0 && address < 17)
     8e6:       81 50           subi    r24, 0x01       ; 1
     8e8:       80 31           cpi     r24, 0x10       ; 16
     8ea:       18 f4           brcc    .+6             ; 0x8f2
        {
                address = (address - 1) * 2 + 0xe0;
     8ec:       dd 0c           add     r13, r13
     8ee:       8e ed           ldi     r24, 0xDE       ; 222
     8f0:       d8 0e           add     r13, r24
        }

rearb:

    retval = i2c_start();
     8f2:       0e 94 61 02     call    0x4c2
     8f6:       e8 2e           mov     r14, r24
     8f8:       ff 24           eor     r15, r15
     8fa:       e7 fc           sbrc    r14, 7
     8fc:       f0 94           com     r15
     8fe:       0f 2d           mov     r16, r15
     900:       1f 2d           mov     r17, r15
    switch (retval)
     902:       80 e1           ldi     r24, 0x10       ; 16
     904:       e8 16           cp      r14, r24
     906:       f1 04           cpc     r15, r1
     908:       01 05           cpc     r16, r1
     90a:       11 05           cpc     r17, r1
     90c:       a1 f0           breq    .+40            ; 0x936
     90e:       81 e1           ldi     r24, 0x11       ; 17
     910:       e8 16           cp      r14, r24
     912:       f1 04           cpc     r15, r1
     914:       01 05           cpc     r16, r1
     916:       11 05           cpc     r17, r1
     918:       3c f4           brge    .+14            ; 0x928
     91a:       88 e0           ldi     r24, 0x08       ; 8
     91c:       e8 16           cp      r14, r24
     91e:       f1 04           cpc     r15, r1
     920:       01 05           cpc     r16, r1
     922:       11 05           cpc     r17, r1
     924:       41 f0           breq    .+16            ; 0x936
     926:       87 c0           rjmp    .+270           ; 0xa36
     928:       88 e3           ldi     r24, 0x38       ; 56
     92a:       e8 16           cp      r14, r24
     92c:       f1 04           cpc     r15, r1
     92e:       01 05           cpc     r16, r1
     930:       11 05           cpc     r17, r1
     932:       f9 f2           breq    .-66            ; 0x8f2
    {
        case TW_REP_START:
        case TW_START:
            break;
        case TW_MT_ARB_LOST:
                        //printf("a");
            goto rearb;
     934:       80 c0           rjmp    .+256           ; 0xa36
        default:
                        //printf("A");
                        return -1;
    }

    retval = i2c_sla_w(address);
     936:       8d 2d           mov     r24, r13
     938:       0e 94 6d 02     call    0x4da
     93c:       e8 2e           mov     r14, r24
     93e:       ff 24           eor     r15, r15
     940:       e7 fc           sbrc    r14, 7
     942:       f0 94           com     r15
     944:       0f 2d           mov     r16, r15
     946:       1f 2d           mov     r17, r15
    switch (retval)
     948:       80 e2           ldi     r24, 0x20       ; 32
     94a:       e8 16           cp      r14, r24
     94c:       f1 04           cpc     r15, r1
     94e:       01 05           cpc     r16, r1
     950:       11 05           cpc     r17, r1
     952:       09 f4           brne    .+2             ; 0x956
     954:       6e c0           rjmp    .+220           ; 0xa32
     956:       81 e2           ldi     r24, 0x21       ; 33
     958:       e8 16           cp      r14, r24
     95a:       f1 04           cpc     r15, r1
     95c:       01 05           cpc     r16, r1
     95e:       11 05           cpc     r17, r1
     960:       0c f0           brlt    .+2             ; 0x964
     962:       51 c0           rjmp    .+162           ; 0xa06
     964:       88 e1           ldi     r24, 0x18       ; 24
     966:       e8 16           cp      r14, r24
     968:       f1 04           cpc     r15, r1
     96a:       01 05           cpc     r16, r1
     96c:       11 05           cpc     r17, r1
     96e:       09 f0           breq    .+2             ; 0x972
     970:       60 c0           rjmp    .+192           ; 0xa32
    {
        case TW_MT_SLA_ACK:
            break;
        case TW_MT_SLA_NACK:
                        //printf("B");
            goto quit;
        case TW_MT_ARB_LOST:
                        //printf("b");
            goto rearb;
        default:
                        //printf("B");
                        //printf("0x%x\n", retval);
            goto quit;
    }

    retval = i2c_write_bytes(&reg, 1);
     972:       41 e0           ldi     r20, 0x01       ; 1
     974:       50 e0           ldi     r21, 0x00       ; 0
     976:       60 e0           ldi     r22, 0x00       ; 0
     978:       70 e0           ldi     r23, 0x00       ; 0
     97a:       ce 01           movw    r24, r28
     97c:       01 96           adiw    r24, 0x01       ; 1
     97e:       0e 94 cf 02     call    0x59e
     982:       7b 01           movw    r14, r22
     984:       8c 01           movw    r16, r24
        if (retval != 1) // Something happened...
     986:       81 e0           ldi     r24, 0x01       ; 1
     988:       e8 16           cp      r14, r24
     98a:       f1 04           cpc     r15, r1
     98c:       01 05           cpc     r16, r1
     98e:       11 05           cpc     r17, r1
     990:       59 f0           breq    .+22            ; 0x9a8
        {
                if (retval == -2) // lost arb
     992:       8e ef           ldi     r24, 0xFE       ; 254
     994:       e8 16           cp      r14, r24
     996:       8f ef           ldi     r24, 0xFF       ; 255
     998:       f8 06           cpc     r15, r24
     99a:       8f ef           ldi     r24, 0xFF       ; 255
     99c:       08 07           cpc     r16, r24
     99e:       8f ef           ldi     r24, 0xFF       ; 255
     9a0:       18 07           cpc     r17, r24
     9a2:       09 f0           breq    .+2             ; 0x9a6
     9a4:       46 c0           rjmp    .+140           ; 0xa32
                {
                        //printf("c");
                        goto rearb;
     9a6:       a5 cf           rjmp    .-182           ; 0x8f2
                }
                else
                {
                        //printf("C");
                        //printf("%d\n", retval);
                        goto quit;
                }
        }

    retval = i2c_start();
     9a8:       0e 94 61 02     call    0x4c2
     9ac:       e8 2e           mov     r14, r24
     9ae:       ff 24           eor     r15, r15
     9b0:       e7 fc           sbrc    r14, 7
     9b2:       f0 94           com     r15
     9b4:       0f 2d           mov     r16, r15
     9b6:       1f 2d           mov     r17, r15
    switch (retval)
     9b8:       80 e1           ldi     r24, 0x10       ; 16
     9ba:       e8 16           cp      r14, r24
     9bc:       f1 04           cpc     r15, r1
     9be:       01 05           cpc     r16, r1
     9c0:       11 05           cpc     r17, r1
     9c2:       61 f0           breq    .+24            ; 0x9dc
     9c4:       81 e1           ldi     r24, 0x11       ; 17
     9c6:       e8 16           cp      r14, r24
     9c8:       f1 04           cpc     r15, r1
     9ca:       01 05           cpc     r16, r1
     9cc:       11 05           cpc     r17, r1
     9ce:       dc f4           brge    .+54            ; 0xa06
     9d0:       88 e0           ldi     r24, 0x08       ; 8
     9d2:       e8 16           cp      r14, r24
     9d4:       f1 04           cpc     r15, r1
     9d6:       01 05           cpc     r16, r1
     9d8:       11 05           cpc     r17, r1
     9da:       59 f5           brne    .+86            ; 0xa32
    {
        case TW_REP_START:
        case TW_START:
            break;
        case TW_MT_ARB_LOST:
            goto rearb;
        default:
                        goto quit;
    }

    retval = i2c_sla_r(address);
     9dc:       8d 2d           mov     r24, r13
     9de:       0e 94 79 02     call    0x4f2
     9e2:       e8 2e           mov     r14, r24
     9e4:       ff 24           eor     r15, r15
     9e6:       e7 fc           sbrc    r14, 7
     9e8:       f0 94           com     r15
     9ea:       0f 2d           mov     r16, r15
     9ec:       1f 2d           mov     r17, r15
    switch (retval)
     9ee:       80 e4           ldi     r24, 0x40       ; 64
     9f0:       e8 16           cp      r14, r24
     9f2:       f1 04           cpc     r15, r1
     9f4:       01 05           cpc     r16, r1
     9f6:       11 05           cpc     r17, r1
     9f8:       71 f0           breq    .+28            ; 0xa16
     9fa:       81 e4           ldi     r24, 0x41       ; 65
     9fc:       e8 16           cp      r14, r24
     9fe:       f1 04           cpc     r15, r1
     a00:       01 05           cpc     r16, r1
     a02:       11 05           cpc     r17, r1
     a04:       b4 f4           brge    .+44            ; 0xa32
     a06:       88 e3           ldi     r24, 0x38       ; 56
     a08:       e8 16           cp      r14, r24
     a0a:       f1 04           cpc     r15, r1
     a0c:       01 05           cpc     r16, r1
     a0e:       11 05           cpc     r17, r1
     a10:       09 f4           brne    .+2             ; 0xa14
     a12:       6f cf           rjmp    .-290           ; 0x8f2
     a14:       0e c0           rjmp    .+28            ; 0xa32
    {
        case TW_MR_SLA_ACK:
            break;
        case TW_MR_SLA_NACK:
            goto quit;
        case TW_MR_ARB_LOST:
            goto rearb;
        default:
            goto quit;
    }
        
        retval = i2c_read_bytes(data, len);
     a16:       b5 01           movw    r22, r10
     a18:       a4 01           movw    r20, r8
     a1a:       c3 01           movw    r24, r6
     a1c:       0e 94 8f 02     call    0x51e
     a20:       7b 01           movw    r14, r22
     a22:       8c 01           movw    r16, r24
        if (retval < 0)
     a24:       99 23           and     r25, r25
     a26:       2c f0           brlt    .+10            ; 0xa32
                goto quit;

        i2c_stop();
     a28:       0e 94 86 02     call    0x50c
        //printf(__FUNCTION__"() Return OK. (Returns %d)\n", retval);
        return retval;
     a2c:       c8 01           movw    r24, r16
     a2e:       b7 01           movw    r22, r14
     a30:       06 c0           rjmp    .+12            ; 0xa3e

quit:
    i2c_stop();
     a32:       0e 94 86 02     call    0x50c
        //printf(__FUNCTION__"() Return ERROR.\n");
        return -1;
     a36:       6f ef           ldi     r22, 0xFF       ; 255
     a38:       7f ef           ldi     r23, 0xFF       ; 255
     a3a:       8f ef           ldi     r24, 0xFF       ; 255
     a3c:       9f ef           ldi     r25, 0xFF       ; 255
}
     a3e:       21 96           adiw    r28, 0x01       ; 1
     a40:       0f b6           in      r0, 0x3f        ; 63
     a42:       f8 94           cli
     a44:       de bf           out     0x3e, r29       ; 62
     a46:       0f be           out     0x3f, r0        ; 63
     a48:       cd bf           out     0x3d, r28       ; 61
     a4a:       df 91           pop     r29
     a4c:       cf 91           pop     r28
     a4e:       1f 91           pop     r17
     a50:       0f 91           pop     r16
     a52:       ff 90           pop     r15
     a54:       ef 90           pop     r14
     a56:       df 90           pop     r13
     a58:       bf 90           pop     r11
     a5a:       af 90           pop     r10
     a5c:       9f 90           pop     r9
     a5e:       8f 90           pop     r8
     a60:       7f 90           pop     r7
     a62:       6f 90           pop     r6
     a64:       08 95           ret


I hope that was not too much text.

Thanks,
Mike


On Wed, 2003-05-14 at 13:29, Larry Barello wrote:
> It looks like the compiler assumes R18 & R19 are preserved across the call.  
> Isn't that
> a bug?  Gotta look at the assembly for srf08_read() to see what it does with 
> those
> registers.
> 




reply via email to

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