Skip to main content
lre.1
Associate
January 16, 2024
Solved

Different optimization level and different assembly code

  • January 16, 2024
  • 2 replies
  • 1384 views

I have following code snippet, when I compile it with -O0 optimization level it gives below assembly code and I can observe value address on Expression window. But when I compile it with -O3 optimization level I couldn't observe address of value. 

Where is the storing location of value when compiled it with -O3 ?

#define SRAM_ADDR 0x20000004U
int main(void) {
	uint32_t value = 41;
	uint32_t *ptr = (uint32_t *)SRAM_ADDR;

	while(1) {
		value = *ptr;
		if(value) break;
	}
	while(1);
	return 0;
}

Disassembly :

lre1_0-1705427650708.png

-O0 optimization :

lre1_3-1705427765575.png

-O3 optimization :

lre1_5-1705427949908.png

lre1_4-1705427909358.png

 

    Best answer by waclawek.jan

    Make value volatile, otherwise optimizer optimizes it away entirely.

    JW

    2 replies

    waclawek.jan
    waclawek.janBest answer
    Super User
    January 16, 2024

    Make value volatile, otherwise optimizer optimizes it away entirely.

    JW

    Tesla DeLorean
    Guru
    January 16, 2024

    value is simply held in register r3, and stored within the local stack frame [r7+4] or [sp+4]

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..