Register

Hello, I am investigating in depth the operation of one of the codes made by Nick Gammon, but I came across a variable that I had never seen, I cant find any explanation about the use of it on the internet. The variable is "register".

I dont think its necessary to pass all the code since I just want to know how this variable works, but just in case here it is: Gammon Forum : Electronics : Microprocessors : Arduino Uno output to VGA monitor

If it's not a bother, I would be grateful if someone explained to me what the "register" variable is for and what benefits it gives to using it (I also saw that it can be declared as "const register").

Thanks!!!

Please direct us to the variable in question. The link you posted has pages and pages of stuff to go through. Make it easier for us by at least quoting the section where it is used, and explain how it is used...

Well, this variable is in the first complete code posted on that forum...

// pre-load pointer for speed
  const register byte * linePtr = &screen_font [ (vLine >> 1) & 0x07 ] [0];
  register char * messagePtr =  & (message [messageLine] [0] );

The 'register' keyword is a hint to the compiler that you would like the variable to be kept in a register rather than having it fetch from and save to memory.

Register access is faster than accessing memory; the keyword is used when you need read/writes to be as quick as possible.

I don't know if there's any assurance the compiler will actually assign a register to a variable but that's the intent.

I did not understand, I think I had already heard that part that is a suggestion for the compiler. Sorry for what I’m going to say (I’m not an Arduino expert), as I remember there are like 32 registers inside the AVR, what you tell me is that by putting “register” to another variable, it allows me to access it more quickly ?

hiperdoo:
Hello, I am investigating in depth the operation of one of the codes made by Nick Gammon, but I came across a variable that I had never seen, I cant find any explanation about the use of it on the internet. The variable is “register”.

In your later quoted code, you’ll notice that “register” appears twice, thus “register” cannot be a variable name.
The variables are “linePointer” and “messagePointer”.

It’s a suggestion to the compiler that you’d like the fastest possible access to those variables.
The compiler can choose to ignore your suggestion.

With the information you said, I came to understand that "register" allows me to access variables more quickly but there is also some chance that the compiler will ignore this suggestion...

This raises two more questions for me:

  1. What is the difference in clock cycles between using and not using "register".
  2. What circumstances make the compiler ignore my "register" request?

From what I can find, register was deprecated in C++11, and removed in C++17 (but reserved as a keyword for possible future use), so should make no difference at all in the code produced by the compiler.

I doubt that this is the case, this code was created in 2012, at that time it must have had some use, also, this is an optimized code, so it must have a specific use.

hiperdoo:
This raises two more questions for me:

  1. What is the difference in clock cycles between using and not using “register”.
  2. What circumstances make the compiler ignore my “register” request?
  1. It depends on the size of the variable and the machine architecture.
  2. When there aren’t any available registers. (Or a bunch of other reasons).
    Your request for locking a variable into a register could spoil the compiler’s attempts at optimisation.

“Register” is historical at this point.

Standard compiler optimizations will put things in registers as much as possible, probably doing a better job than you would at picking which variables will yield the most improvement. With simple programs (like most Arduino sketches) and modern CPUs (with a significant number of registers), probably ALL simple local variables end up in registers. (the PDP-11 on which C was originally written had six registers...)

Well, I need the exact details of the operation of "register", someone know in which page of the official manual of ATmega328 this information is, or of some web page?

hiperdoo:
Well, I need the exact details of the operation of “register”, someone know in which page of the official manual of ATmega328 this information is, or of some web page?

I think it would be more relevant to refer to the compiler’s ABI.

But, as has already been pointed-out, you’re flogging a dead horse, or at least one that’s about to expire soon
.

It has nothing to do with the atmega328...

hiperdoo:
Well, I need the exact details of the operation of "register", someone know in which page of the official manual of ATmega328 this information is, or of some web page?

https://gcc.gnu.org/onlinedocs/gcc/Explicit-Register-Variables.html