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

why does this assembly fail?

Hello,

I have this C code

int value_a, value_b ;
value_a = value_b ;

that translates to this assembly:

0x0002B128  E58D600C  STR       R6,[R13,#0x000C]

but the assignment fails - both variables are allocated on the stack. if I allocate the target variable in RAM (say, using "static") it works. alignment issue? something?

Parents Reply Children
  • sorry for the incomplete explanation. the variable "value_b" is initialized. even if I do something like this (with another variable):

    int value_c = 0
    

    it might fail, hence filling the variable with what I regard as garbage. this only happens if R13 is involved.

    actually, I have this code:

    int   value_c = 0,
                     value_d = 0,
                     value_e = 0,
                     value_f = 0,
    

    value_d and value_f are initialized to 0, the others not!

  • value_d and value_f are initialized to 0, the others not!

    The compiler is not required to do everything that's stated in the source code. If variables are never used, the compiler is free to ignore them.

    You may want to try to lower the optimization settings, or declare variables as volatile, if you want the compiler to follow your code more rigorously.

  • it might fail,

    You're still not making any sense. Either it fails, or it doesn't. So which is it?

    You've been asked what exactly went wrong. Now please look at your answer again and tell us: does that look like you fulfilled that request?

    As others have said, this looks like you're basing your idea of what it means for code like this to "fail" on incorrect assumptions about what a C compiler is required to make of your source code. Just because you define and initialize a variable doesn't mean the compiled code has to perform that initialization. And even if it is performed, it may happen at an unexpected time.

  • ok I understand. thanks for your help.