Why does IAR skip subroutine code while running project.

Thread Starter

jp1390

Joined Aug 22, 2011
45
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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."
 

Thread Starter

jp1390

Joined Aug 22, 2011
45
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
 

ftsolutions

Joined Nov 21, 2009
48
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.
 

ftsolutions

Joined Nov 21, 2009
48
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.
 

t06afre

Joined May 11, 2009
5,934
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
 

ErnieM

Joined Apr 24, 2011
8,377
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

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.
 
Top