Pelles C forum

C language => Beginner questions => Topic started by: shiva on May 21, 2012, 06:02:02 PM

Title: how can make variable not in registers
Post by: shiva on May 21, 2012, 06:02:02 PM
how can make  variable not in registers?
Title: Re: how can make variable not in registers
Post by: CommonTater on May 21, 2012, 06:57:54 PM
Don't declare it as register.

Title: Re: how can make variable not in registers
Post by: AlexN on May 21, 2012, 07:09:04 PM
Quote from: shiva on May 21, 2012, 06:02:02 PM
how can make variable not in registers....?
... with the keyword volatile, which means that a variable can be changed from outside of the program.
Title: Re: how can make variable not in registers
Post by: Vortex on May 21, 2012, 07:12:43 PM
Registers are faster than memory locations. What's the purpose of avoiding the registers?
Title: Re: how can make variable not in registers
Post by: frankie on May 21, 2012, 07:15:44 PM
Mmmmm.... What about your IT course?  ::)
Seems a shooting of questions....  ;D
Title: Re: how can make variable not in registers
Post by: AlexN on May 21, 2012, 09:27:42 PM
Quote from: Vortex on May 21, 2012, 07:12:43 PM
Registers are faster than memory locations. What's the purpose of avoiding the registers?
I use it (my favorites are static and volatile), if I found a probleme with the optimizer of a compiler . So I can change the generated code a little bit and perhaps this change helps - you see, I do not really need it.
But when you write code where you read register from real hardware (f.e. an A/D converter), it will be good to take the values from the hardware and not from registers where an old value may be stored.
Title: Re: how can make variable not in registers
Post by: Vortex on May 21, 2012, 09:44:27 PM
Quote from: AlexN on May 21, 2012, 09:27:42 PM
But when you write code where you read register from real hardware (f.e. an A/D converter), it will be good to take the values from the hardware and not from registers where an old value may be stored.

Register are stored in the CPU. Aren't we talking about CPU registers?
Title: Re: how can make variable not in registers
Post by: CommonTater on May 21, 2012, 10:33:29 PM
Quote from: Vortex on May 21, 2012, 09:44:27 PM
Register are stored in the CPU. Aren't we talking about CPU registers?

Yep... but, when you have a volitile value, such as a chip register that can change at any time, you want to force the system to re-read it every time, rather than storing it in a CPU register...

This is the only case where you actually do not want register variables, far as I know.


Title: Re: how can make variable not in registers
Post by: Bitbeisser on May 22, 2012, 07:32:43 AM
Quote from: frankie on May 21, 2012, 07:15:44 PM
Mmmmm.... What about your IT course?  ::)
Seems a shooting of questions....  ;D
Seems to me like I am tempted to quote Monty Python...  ;)

Ralf
Title: Re: how can make variable not in registers
Post by: AlexN on May 22, 2012, 08:40:18 AM
Quote from: Vortex on May 21, 2012, 09:44:27 PM
Register are stored in the CPU. Aren't we talking about CPU registers?
No, I mean the registers of other hardware which is mapped  in the memory of your CPU, like timer moduls, I/O-ports, A/D-converter.
OK, you can't use this at developing applications for a (32/64-bit) Windows application, but C is also used for embedded systems where you can speak directly to the ports. I have no experience with driver developing for Windows, but I think there you also will need this.
Title: Re: how can make variable not in registers
Post by: frankie on May 22, 2012, 09:15:42 AM
Well "volatile" is a memory variable's attribute, not referenced to chips registers. That are always read because are external to the CPU, and then, to the compiler control.
The "volatile" qualifier is used when a variable is accessed from more than one thread of execution, from interrupt routines, and whatever concurrent instruction flow outside of current context.
Immagine reading a memory variable holding ticks count, or concurrent access flags (that on OS level are accessed with specific machine instructions as test and set that take one cycle also locking the bus), in this case the compiler seeing that you read that variable many times can be tempted, for optimization reasons, to read it one time than cache the value. The result is that in all parts of your program you have the same value while the variable is continuosly changing.
This is also the reason for which you can solve an optimization bug problem, qualifying a variable as "volatile" the compiler don't try to 'cache' the value (i.e. in a register triggering the bug) and each time you use it the value is freshly read.
Title: Re: how can make variable not in registers
Post by: shiva on May 22, 2012, 05:33:12 PM
computer science engg... Btech
Quote from: frankie on May 21, 2012, 07:15:44 PM
Mmmmm.... What about your IT course?  ::)
Seems a shooting of questions....  ;D
Title: Re: how can make variable not in registers
Post by: frankie on May 22, 2012, 06:47:17 PM
Ok Shiva.
The easier way is to declare it 'extern' or 'static'. The compiler will automatically allocate it in memory, normally in the BSS section, and, in the first case, generates a global symble to make the variable shareble with other modules, in the latter case is generated a local symbol and the variable can be accessed only from the module where it is defined.

Alex you are write about some uP that have memory mapped I/O, in this case you define 'volatile' that address to perform I/O read/write toward the port each time the location is accessed. Anyway I/O memory mapping is generally slow (I/O port access add wait cycles to the bus transitions) so is not preferred as direct CPU access; it is used with DMA or dual-ported memory.
Title: Re: how can make variable not in registers
Post by: Vortex on May 22, 2012, 07:21:47 PM
OK, I understand, it's the case of other hardware register set.