Why does IAR skip subroutine code while running project.

Discussion in 'Embedded Systems and Microcontrollers' started by jp1390, Aug 23, 2011.

  1. jp1390

    Thread Starter Member

    Aug 22, 2011
    45
    2
    Hello, I have encountered a problem with my code. I am programming a MSP430F425 microcontroller. I have written a program in C++ and I have noticed that some of my subroutines are being stepped over but are not being executed. This occurs even when I am using a step-by-step debugger.

    For instance, I have a conditional statement if (Found > 0) and in the Watch window Found = 1, but when I debug the code, it simply skips over the subroutines inside the body of this condition.

    Has anyone experienced this problem?

    Thank you.
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    C or C++ can do some silly things at times, correct things due to what you have told it, but just plain weirdness compared to what you intended.

    How is Found declared? If it is a signed bit then 1 may be less then zero as it represents a negative quantity. You would want it to be unsigned.

    Do you have optimization turned on? An optimizer can completely wipe out things like simple delay loops as the compiler doesn't see anything useful happening as the output of the loop never gets used, so it snips out your "dead code."
     
  3. jp1390

    Thread Starter Member

    Aug 22, 2011
    45
    2
    I have defined subroutines in another source file and have declared as 'extern' in the corresponding header file. This subroutine is then called in my main source file in the 'main (void)' function. If I call this subroutine without placing 'void' infront of it, the compiler will report an error: Undefined external function. On the other hand, if I do place 'void' there is no error but the subroutine is skipped when the program is run.

    Put void -> Skipped, no error
    Neglect void -> Error
     
  4. ftsolutions

    Active Member

    Nov 21, 2009
    48
    2
    IF you are using IAR tools, and have optimization turned on, the sequencing / execution of the code can be other than what it appears in the C source code window.

    But you've answered your own question - if the compiler complains about an undefined external function, it is not being properly linked or doesn't have scope to be linked within the module you are calling it from. Placing "void" in front of something is basically a universal wildcard/hack - it just says, use this as a void pointer and don't worry/complain about it. But, since the linker still cannot resolve its address, the compiler ignores it or optimizes it out. Look at the disassembly window when you single step to it to see. You need to change how your function is defined/called for the linker to link it with the proper branch address.
     
  5. ftsolutions

    Active Member

    Nov 21, 2009
    48
    2
    I.E. You should have the prototype of the function defined in a header file which is #included by the c source file where you have main(), or else you need to have the extern declaration for the called function placed in the c source file that contains your main() function.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I have seen this with the HI-Tech C compiler with full optimization turned on. The compiler detect that the variable is not used and hence omit it in the code generation. It can be a problem if you want to have some values used for debugging/dummy purpose only, or only want to run a program in the simulator. In HI-Tech C the trick is to define such variables as volatile. It could be that this also work in your compiler
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605

    Is that where you defined "Found"?

    I asked HOW you defined "Found".

    I understand you have several problems going on at once. It is best to try to solve them one by one. Yes, sometimes problem #7 on your list needs to be #1 and that makes #1-6 disappear, but trying to going about things methodically is always best.
     
Loading...