Weird results

Discussion in 'Programmer's Corner' started by Graham1904, Nov 12, 2011.

  1. Graham1904

    Thread Starter New Member

    Nov 2, 2009
    I have just started with C after 15-years of assembly programming :)

    I have downloaded PICC 9.83 and am using PIC16F688 as a default PIC as I have plenty of them I want to test out using C.

    I have written a dummy delay routine to see whether it will compile and run using MPLAB 8.80 SIM:

    #include <htc.h>

    void main(void)
    unsigned char DLY5N;
    unsigned char a,b,c,i;
    DLY5N = 100;
    a = 0;
    b = 2;
    c = 0;
    for (i=1; i=DLY5N; i++)

    a = b + c + 1;
    goto LOOP;

    The compiler states _a and _i not used ???? and the disassembly listing shows some very weird results and the program will not step past the "for" statement.

    Any help to get off the first very basic steps will be appreciated.
  2. bertus


    Apr 5, 2008

    I am not familiar with C, but I think the declaration of the variables is not correct.
    A char declaration is for a text string.

    This is from this page:

  3. kubeek


    Sep 20, 2005
    A is not used because it is only written but never read.
    Other compilers would also complain about the for, because you have assignment instead of comparison. That is = instead of ==. That is a typical C error, you can try the -Wall compiler option to print more warnings which might show the warning about it.
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    The variables are optimized away. The optimizer sense they only take up RAM space and do nothing good ;). It can be strange then debugging code just to explore. As beginners often do. What you can do is to set your compiler in "Lite" mode. I guess you have the free version. And after 30 or 60 days it will only work in Lite mode anyway. You could also declare the variables volatile
    Code ( (Unknown Language)):
    2. volatile unsigned char DLY5N;
    3. volatile unsigned char a,b,c,i;
  5. DumboFixer

    Active Member

    Feb 10, 2009
    I think your problem lies here

    Code ( (Unknown Language)):
    1. for (i=1; i=DLY5N; i++)
    Your starting condition sets I = 1 - no problem
    Your loop step is i++ - no problem

    Your terminating condition is i = DLY5N - A problem

    You termination condition is a variable assignment. Did you mean i == DLY5N or i < DLY5N ?
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009


    = for assignment
    == for logical.

    MOST logical operators have 2 characters in C.

    == equals
    >= greater than or equal
    <= less than or equal
    != not equal
    || or
    && and

    and then there is the elusive

    ! not operator


    < less than
    > greater than

    not to be confused with
    << bitwise shift left
    >> bitwise shift right

    You can find a whole list of operators here.
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    The statements:

    Code ( (Unknown Language)):
    2.     for (i=1; i=DLY5N; i++)
    3.     {
    4.         NOP();
    5.     }
    Is an interesting collection. It looks like a small delay was the intended result, but that is definitely not what is happening.

    The for loop is poorly formed. "for" loops are of the form:

    Code ( (Unknown Language)):
    2.     for (  { starting value assignment }  ;
    3.            { completion test }            ;
    4.            { increment/decrement }        )
    The issue here is "i=DLY5N" which is an assignment and not a comparison. Now when an assignment is used in place of a comparison the value assigned is used as the comparison result. So any result that is zero is False, any non-zero result is True.

    Thus "i=DLY5N" where DLY5N is 100 is always True, Hence the for loop never terminates.

    One other point is the "NOP();" statement inside the {block}: this is unnecessary. A better way to write this structure is:

    Code ( (Unknown Language)):
    2.     for (i=1; i==DLY5N; i++);
    Of course, if your reason for the NOP() was to give a breakpoint for debugging then by all means leave it in.

    Keep in mind that since the value of i is never used outside of the for loop some compilers may remove this entire block of code, optimizing it away completely. This may be prevented by declaring the i variable a volatile.
  8. RiJoRI

    Well-Known Member

    Aug 15, 2007
    Rule of thumb:

    Turn off ALL optimization when developing code. It WILL allow you to keep your sanity.

    P.S., I think declaring the variables as "volatile" will help, also. It tells the compiler "Hands off, buddy!"
    ErnieM likes this.