NO

Author Topic: how can make variable not in registers  (Read 5703 times)

shiva

  • Guest
how can make variable not in registers
« on: May 21, 2012, 06:02:02 PM »
how can make  variable not in registers?
« Last Edit: May 22, 2012, 05:20:55 PM by shiva »

CommonTater

  • Guest
Re: how can make variable not in registers
« Reply #1 on: May 21, 2012, 06:57:54 PM »
Don't declare it as register.


Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: how can make variable not in registers
« Reply #2 on: May 21, 2012, 07:09:04 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.
best regards
 Alex ;)

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: how can make variable not in registers
« Reply #3 on: May 21, 2012, 07:12:43 PM »
Registers are faster than memory locations. What's the purpose of avoiding the registers?
Code it... That's all...

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: how can make variable not in registers
« Reply #4 on: May 21, 2012, 07:15:44 PM »
Mmmmm.... What about your IT course?  ::)
Seems a shooting of questions....  ;D
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: how can make variable not in registers
« Reply #5 on: May 21, 2012, 09:27:42 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.
best regards
 Alex ;)

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: how can make variable not in registers
« Reply #6 on: May 21, 2012, 09:44:27 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?
Code it... That's all...

CommonTater

  • Guest
Re: how can make variable not in registers
« Reply #7 on: May 21, 2012, 10:33:29 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.



Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: how can make variable not in registers
« Reply #8 on: May 22, 2012, 07:32:43 AM »
Mmmmm.... What about your IT course?  ::)
Seems a shooting of questions....  ;D
Seems to me like I am tempted to quote Monty Python...  ;)

Ralf

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: how can make variable not in registers
« Reply #9 on: May 22, 2012, 08:40:18 AM »
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.
best regards
 Alex ;)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: how can make variable not in registers
« Reply #10 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.
« Last Edit: May 22, 2012, 09:53:13 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

shiva

  • Guest
Re: how can make variable not in registers
« Reply #11 on: May 22, 2012, 05:33:12 PM »
computer science engg... Btech
Mmmmm.... What about your IT course?  ::)
Seems a shooting of questions....  ;D

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: how can make variable not in registers
« Reply #12 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.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: how can make variable not in registers
« Reply #13 on: May 22, 2012, 07:21:47 PM »
OK, I understand, it's the case of other hardware register set.
Code it... That's all...