gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Strange register storage class specifer behaviour

#871 - I.C.E - Sat Jan 11, 2003 6:51 pm

I was initially just interested in what assember code the compiler turns code like this:
Code:

while(1)
   test++;


I have seen that because of storing test in memory it must be loaded to register and then 1 is added and then stored again into memory.

Then I remembered that there is the posibility to use the storage class register to advice the compiler to store the variable in question in a register. (by the way, i thought that the compiler usualy does this in such a construct, without explicity tell him to store it in register). I wrote this code instead of the above one:
Code:

{
   register u32 test;
   while(1)
      test++;
}


I now used gdb to see what happend and to my astonishment a ASM dump of the loop looks like this:
Code:

0x8000cfc <initStage+776>:   b   0x8000cfc <initStage+776>


Can anyone explain me why the compiler just done an endless loop?

#876 - Costis - Sat Jan 11, 2003 7:41 pm

Hi,

The compiler probably has just optimized out the incrementing add as it loops forever not doing anything else. This may be the case if you're using a high optimization level. I don't believe that it will do that if you compile it with -O0.

Costis

#879 - I.C.E - Sat Jan 11, 2003 7:51 pm

I was not using any specific otimazion switch. I have now explicit tell the compiler -O0 and the same happens. Maybe the compiler implicit optimize the loop because of using the register storage class?

#1168 - tom - Wed Jan 15, 2003 10:42 am

try something like that:

[code]
volatile int test = 0;

while(1) test++;
[/code]

the volatile keyword will tell the compiler not to do any optimizations regarding the variable test, that is, its content will always be read from memory and written immediatley back to memory.

#1184 - Vortex - Wed Jan 15, 2003 4:37 pm

There is one more thing - the register keyword is just a hint for the compiler - it can be ignored if for example there are no avaiable registers to be allocated. In that case the declaration will be just a regular auto variable.

#1192 - Touchstone - Wed Jan 15, 2003 5:29 pm

Since you've done an infinte loop it's possible that the compiler just discards the rest of the code in the funktion so therefore it's useless.

Exactly what are you interested in knowing? Maybe there are other ways to find out.
_________________
You can't beat our meat