This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

local variables lose values ?

Hello,

I have a project (C166/HUGE memory model), where some local variables seems to lose their values. Everything worked fine, until I added some more code to a specific function (and one more local variable):

unsigned char Ptmp=0,FSzu=0,IsCancelled=1,errors=0,
FSDin,f3=0,f4=0,Abort=0,ii,done=0;

"done=0" was added to the list. Now, the compiled program seems to have a problem with the variable IsCancelled (set to 1 initially, but whenever referred to it in the function, it returns 0).

The .LST file shows this now (IsCancelled is not "working" anymore):

NAME            CLASS    SPACE  TYPE   OFFSET   SIZE
----------------------------------------------------
  Ptmp . . . .  --reg--         uchar  -----    1
  FSzu . . . .  auto            uchar     2H    1
  IsCancelled.  auto            uchar     0H    1
  errors . . .  auto            uchar     6H    1
  FSDin. . . .  auto            uchar     8H    1
  f3 . . . . .  auto            uchar     AH    1
  f4 . . . . .  auto            uchar     CH    1
  Abort. . . .  auto            uchar     EH    1
  ii . . . . .  --reg--         uchar  -----    1
  done . . . .  auto            uchar     2H    1

The old .LST file, when everything was still working, showed this:

NAME            CLASS    SPACE  TYPE   OFFSET   SIZE
----------------------------------------------------
  Ptmp . . . .  auto            uchar     0H    1
  FSzu . . . .  auto            uchar     2H    1
  IsCancelled.  --reg--         uchar  -----    1
  errors . . .  auto            uchar     6H    1
  FSDin. . . .  auto            uchar     8H    1
  f3 . . . . .  auto            uchar     AH    1
  f4 . . . . .  auto            uchar     CH    1
  Abort. . . .  auto            uchar     EH    1
  ii . . . . .  --reg--         uchar  -----    1

Ok, the class of the variable in question has changed, but it should still work, shouldn't it?
What could cause the problem, and how would one prevent this from happening?

Any help would be great!

Thank you in advance.
Holger

Parents
  • Hello again,

    I forgot to mention that I am using version 3.12 of the C166 package.

    Playing around with some options in MicroVision, I found that it seems to work fine again after using "Use static memory for non-register automatics" in the compiler options.

    This gives me an .LST file showing:

    NAME            CLASS    SPACE  TYPE   OFFSET   SIZE
    ----------------------------
      Ptmp . . . .  auto     NDATA  uchar     0H    1
      FSzu . . . .  auto     FCODE  uchar     2H    1
      IsCancelled.  auto     NDATA  uchar     2H    1
      errors . . .  auto     FCODE  uchar     6H    1
      FSDin. . . .  auto     FCODE  uchar     8H    1
      f3 . . . . .  auto     FCODE  uchar     AH    1
      f4 . . . . .  auto     FCODE  uchar     CH    1
      Abort. . . .  auto     FCODE  uchar     EH    1
      ii . . . . .  auto     NDATA  uchar     4H    1
      done . . . .  auto     NDATA  uchar     6H    1
    

    What happens here, why does it not work in the first version, and more interesting would be: Why does the compiler not complain about anything that causes malfunctions in the compiled program?

    Thank you!
    Holger

Reply
  • Hello again,

    I forgot to mention that I am using version 3.12 of the C166 package.

    Playing around with some options in MicroVision, I found that it seems to work fine again after using "Use static memory for non-register automatics" in the compiler options.

    This gives me an .LST file showing:

    NAME            CLASS    SPACE  TYPE   OFFSET   SIZE
    ----------------------------
      Ptmp . . . .  auto     NDATA  uchar     0H    1
      FSzu . . . .  auto     FCODE  uchar     2H    1
      IsCancelled.  auto     NDATA  uchar     2H    1
      errors . . .  auto     FCODE  uchar     6H    1
      FSDin. . . .  auto     FCODE  uchar     8H    1
      f3 . . . . .  auto     FCODE  uchar     AH    1
      f4 . . . . .  auto     FCODE  uchar     CH    1
      Abort. . . .  auto     FCODE  uchar     EH    1
      ii . . . . .  auto     NDATA  uchar     4H    1
      done . . . .  auto     NDATA  uchar     6H    1
    

    What happens here, why does it not work in the first version, and more interesting would be: Why does the compiler not complain about anything that causes malfunctions in the compiled program?

    Thank you!
    Holger

Children
  • Local variables must not loose their values, no doubt about that. In order to see what's happening exactly you have to look through the generated code listing and maybe single-step through the code in the simulator. The problem is, with so many local variables it will be hard to keep track of them all.
    The "Use static memory for non-register automatics" option in essence adds the "static" storage class modifier to every non-register local variable. The problem is, it can make all your functions non-reentrant. To avoid that, you can declare only some of your local variables as "static". Hopefully, it will help you avoid the problem of local variables loosing their values and you will still be able to decide which function must be reentrant and which one doesn't have to be.
    And the last but not the least: with such coding style (lots of automatic variables) you can easily run out of stack space, which can cause all sorts of problems. Check that you have enough stack memory allocated for user stack.
    Regards,
    Mike

  • How can I prevent "auto" variables? The handbook talks a lot about how they are handled and placed, but as far as I see there is no way to directly place a variable in a specific place.

    Coming from C51, there was always an easy way to declare a variable as

    unsigned char xdata varname;
    
    to make sure that it is placed in the memory area specified.
    Is there anything similar for the C166?

    Thank you, again!
    Holger

  • The static keyword is designed for that. By default, local variables have the auto storage class pecifier. You can find details in K&R, for example.

  • Hello!

    what would be a reason against making "everything static" with the compiler option?

    Functions wouldn't be reentrant anymore, but if you don't have any functions that have to be, is there any reason why not to do this?

    ISRs should also run if they're static, since a new interrupt during the execution of the previous one would just wait until the previous one is handled.

    Thanks for any advice!
    Holger

  • "what would be a reason against making "everything static" with the compiler option?"

    That'd mean you'd need to allocate RAM for every single local variable.
    "Conventional" 'C' compilers allocate local variables on the Stack, so you only need enough RAM for the maximum number of local variables in use simultaneously
    C51 uses a data overlaying technique to achieve a similar end.
    I dunno how C166 does it.