This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

For / next loop endless

I am using the 672x series of processors and am far along in my application development.

I have a strange problem, I have written the following code:

                for (li=26;li<256;li++){
                    EEdat[li]=0;}

li is currently an int variable, i have tried an unsigned short as well.  When my program runs it never leaves this loop.  Emulating it shows li increasing to 168 and then resetting to 0.  Interrupts are off so I am not branching somewhere which could cause problems.  This is in the main loop so stack is not being used.  I am baffled.  Any ideas at what might be going on.

 

  • I assume optimization is disabled if you are stepping through the code? Are you able to see any code in the disassembly window that might zero out the register containing the contents of the li variable? It is strange that the 'reset' would occur at li=168 as you are not hitting any sort of boundary.

  • Just some thoughts:

    Is it possible that the address of EEdat[168] == address of variable li.  That would explain the reset.  You say you are not using a stack, but if you are running C code, you must be using a stack - so where is the stack pointer pointing to?.  If li is a local variable it is likely that it is on the stack.  How and where is EEdat allocated?

    Regards,

    Daniel

  • Hello,

    I had a similar problem, I read some document on optimization techinque and I changed my loop so it looks like that:

    #pragma UNROLL(230);
     for(li=256;li != 26;li--)
      EEdat[li] = 0;

    The UNROLL pragma tells the compiler how many iterations exactly will be performed in loop.

    I am not sure that this is what solved the problem, since I've added some other things as well in my program, and I am using a DSP BIOS, not in the main, like you,

    But the problem has dissapeared.

    If You try this and it helps, please notify me, I am curious to know if this was the thing that solved the problem in my project.

     

    Regards

    Arye

     

  • here is the dissassembly:

                      for (li=26;li<256;li++){

    1001F810 02000D2A            MVK.S2        0x001a,B4
    1001F814 02005BFE            STW.D2T2      B4,*+B15[91]
    1001F818 00002000            NOP           2
    1001F81C 01808028            MVK.S1        0x0100,A3
    1001F820 009078F8            CMPGT.L1X     A3,B4,A1
    1001F824 90000810     [!A1]  B.S1          C$DW$L$_main$99$E (PC+64 = 0x1001f860)
    1001F828 00008000            NOP           5
    1001F82C          C$DW$L$_main$99$B, C$L69, C$DW$L$_main$98$E:
    1001F83C 02005BEE            LDW.D2T2      *+B15[91],B4
    1001F840 00006000            NOP           4
    1001F844 0210205A            ADD.L2        1,B4,B4
    1001F848 02005BFE            STW.D2T2      B4,*+B15[91]
    1001F84C 00002000            NOP           2
    1001F850 0280802A            MVK.S2        0x0100,B5
    1001F854 00148AFA            CMPLT.L2      B4,B5,B0
    1001F858 2FFFFD90     [ B0]  B.S1          C$DW$L$_main$98$E (PC-20 = 0x1001f82c)
    1001F85C 00008000            NOP           5

                      EEdat[li]=0;}

    1001F82C          C$DW$L$_main$99$B, C$L69, C$DW$L$_main$98$E:
    1001F82C 023C9A43            ADDAH.D2      SP,B4,B4
    1001F830 028000FA ||         ZERO.L2       B5
    1001F834 0291C2D6            STH.D2T2      B5,*+B4[14]                                             when li=168 and this line is executed li is reverted to 0
    1001F838 00002000            NOP           2

    Does this show anything strange?

  • ok, it is true that variable li is being overwritten.  When I changed

    EEdat[li]=0;} to EEdat[li]=10;}  and step through 168 then becomes 10.  and I now see the real stupid problem

    I declared EEdat[128] and not to 256.

    Thank you for your replies.

  • Nice find Dan and Daniel :)