Skip to main content
FEber.1
Associate II
July 2, 2025
Solved

do-while loop leads to false disassembly and hanging in release-mode

  • July 2, 2025
  • 5 replies
  • 828 views

STM32L052C8T3/ STM32CubeIDE:

In search of the causes of excessive power consumption, I had the firmware compiled for release mode. This caused the program to hang in the first while loop.
The cause is an incorrect compilation. I cannot resolve this and would like to know what I am doing wrong.

Written in C

...
intcontent=0;
do
{
intcontent=adcdone;
}
while (intcontent==0);
...

Disassambly in debug mode:
...
intcontent=adcdone;
08000eb2: ldr r3, [pc, #204] ; (0x8000f80 <MainMeasureVoltage+304>)
08000eb4: ldrb r3, [r3, #0]
121 while (intcontent==0);
08000eb6: cmp r3, #0
08000eb8: beq.n 0x8000eb2 <MainMeasureVoltage+98>
...

Disassambly in release mode:
...
08000e3e: ldr r3, [pc, #168] ; (0x8000ee8 <MainMeasureVoltage+264>)
08000e40: ldrb r3, [r3, #0]
08000e42: cmp r3, #0
08000e44: bne.n 0x8000e48 <MainMeasureVoltage+104>
08000e46: b.n 0x8000e46 <MainMeasureVoltage+102>
...

Result:
Programming is hanging in: 08000e46: b.n 0x8000e46 <MainMeasureVoltage+102>

Best answer by Ozone

> The cause is an incorrect compilation. I cannot resolve this and would like to know what I am doing wrong.

This is usually the wrong approach.
In 99,9% of cases, the problem is in front of the keyboard.

> In search of the causes of excessive power consumption, I had the firmware compiled for release mode. This caused the program to hang in the first while loop.

Your code fragment does not reveal much.
But first, "release mode" usually implies optimisation levels > 0.
I suppose your "adcdone" variable is set elsewhere, most probably in an interrupt routine. And interrupt routines are never detected as "called" by statical analysis, thus the compiler considers your loop pointless.
Unless you declare such variables as "volatile", the compiler will optimizing them away.

5 replies

Ozone
OzoneBest answer
Principal
July 2, 2025

> The cause is an incorrect compilation. I cannot resolve this and would like to know what I am doing wrong.

This is usually the wrong approach.
In 99,9% of cases, the problem is in front of the keyboard.

> In search of the causes of excessive power consumption, I had the firmware compiled for release mode. This caused the program to hang in the first while loop.

Your code fragment does not reveal much.
But first, "release mode" usually implies optimisation levels > 0.
I suppose your "adcdone" variable is set elsewhere, most probably in an interrupt routine. And interrupt routines are never detected as "called" by statical analysis, thus the compiler considers your loop pointless.
Unless you declare such variables as "volatile", the compiler will optimizing them away.

Tesla DeLorean
Guru
July 2, 2025

Make it volatile if it's changed in an interrupt or callback. That way the optimizer doesn't take the code too literally.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
FEber.1
FEber.1Author
Associate II
July 2, 2025

To "Ozone" & "Tesla DeLorean"

Thank you very much for your quick help.
Volatile was the magic word.

And another question for Ozone, where can I set the optimization level?

Andrew Neil
Super User
July 2, 2025

@FEber.1 wrote:

Volatile was the magic word


Good news! Now please mark that as the solution.

Whenever something breaks on increasing optimisation level, it is usually due to issues with the code - and lack of 'volatile' is a favourite.

 

Please also see: How to insert source code.

 


@FEber.1 wrote:

where can I set the optimization level?


In the Project Properties (right-click Project name):

AndrewNeil_0-1751450170852.png

 

The options are documented here: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
FEber.1
FEber.1Author
Associate II
July 2, 2025

@Tesla DeLorean: I have to postpone coffee due to missing cards.

FEber.1
FEber.1Author
Associate II
July 2, 2025

@Andrew Neil: Thank you very much. My surface looks a little different and I didn't find it straight away, although I was at this point at the beginning of the project.
This life is one of the hardest.