Click here to Skip to main content
15,887,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello !

I've been learning how volatile works because I use microcontrollers I partly need to know how it works. I've learned how it works websites when it comes to pseudo assembler code. I'll add this information to show what I know and what I wanted to nibble.

So volatile stops the compiler to optimize a variable he thinks will not be updated in big summary or other processes from being optimized by compiler.

Something like this :

Non volatile :

move the value of variable m into internal register A
label:
if A != 1 goto done
goto label
done:
carry on


So the variable m is read once and it will never read the value m again even though the m can change it's value. It thinks it will never be changed

Volatile :

label:<br />
move the value of variable 'm' to internal register A<br />
if A != 1 then go to done<br />
go to label<br />
done:


Here with volatile it will read everytime what variable m has inside.

How it looks like with ISR (interrupt route) ?
This is an example I read from websites as well which tell me a lot how it works.

Quote:
* in main loop - just before ISR is executed - it has value "X" in register1
* ISR starts, ISR saves the value of register1 (X) into SRAM (stack?)
* ISR uses register 1 with it´s own values, loads with abc, changes to def ... anything,performs oeprations
* before returing to main() it restores value "X" into register1.


The variables value are saved in memory/RAM etc. in which operations are stored in registers. This made me clear as light that ISR only updates the variable value in memory not in register because the registers goes back to it's previous state before modyfication.

Cool

Now what I wanted to know is now how volatile works etc because I partly know how it works. I want to know how I will see it in debugger.

Let's use this example :

int A = 0;

int main()
{
	while(A != 1)
	{
	//Do something
	}
}

void ISR()

{
	A++;
}


Here is a simple code without volatile. Wanted to know how the debugger will behave.

int A = 0;

int main()
{
	while(A != 1)
	{
	//Do something
	}
}

void ISR()

{
	A++; <----- breakpoint
}


I set a breakpoint here.
The variable A is updated from 0 to 1. But the while loop won't read variable A, rather it is optimized and will read the value 0 from it's register like I said at the beginning.

Okey so variable A was updated from 0 to 1 and debugger will show the value of A to be 1 and not 0.

int A = 0;

int main()
{
	while(A != 1) <----- breakpoint
	{
	//Do something
	}
}

void ISR()

{
	A++;
}


So after ISR the debugger now jumps to the main and stops at while because this is my next breakpoint.

What value of A will be ? Will it still be "1" like I said at the beginning how it all works ? A should be still 1 and the while should let me pass through because even though A = 1 and while loop says A != 1 it should still pass because while reads the data from register and not from the variable A because it is not volatile.

Why I need this knowledge ? For the future if I forget and something weird will happen this will help me know if I forgot something like. Why the while loop started even though the condition didn't match.

A believe you understand what I am trying to say here.

What I have tried:

I didn't know how it would look like because my microcontroler didn't optimize the non volatile variables as if they were volatile so I seek rather knowledge without seeing it normally ;D
Posted

1 solution

I would image the optimization depends on the compiler. I'm very familiar with the gcc compiler and the code given here;

C++
int A = 0;

int main()
{
	while(A != 1) <----- breakpoint1
	{
	//Do something
	}
}

void ISR()

{
	A++; <----- breakpoint2
}


The value at breakpoint2 will be 0->1 and the value at breakpoint1 you will get an "the value was optimized out" because the value was in a register and the value is gone once the PC leaves the ISR.

C++
volatile int A = 0;

int main()
{
	while(A != 1) <----- breakpoint1
	{
	//Do something
	}
}

void ISR()

{
	A++; <----- breakpoint2
}


When using the volatile keyword the value at breakpoint2 will be n->n+1 and the value at breakpoint1 will be n and the value will show up in the debugger.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900