cancel
Showing results for 
Search instead for 
Did you mean: 

Different optimization level and different assembly code

lre.1
Associate II

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

 

1 ACCEPTED SOLUTION

Accepted Solutions

Make value volatile, otherwise optimizer optimizes it away entirely.

JW

View solution in original post

2 REPLIES 2

Make value volatile, otherwise optimizer optimizes it away entirely.

JW

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 Venmo
Up vote any posts that you find helpful, it shows what's working..