The concept of register variables (data type: register) in C?

I just want to get an idea of ​​how register variables are processed in C program executables. I.e. in which place (or register) is it exactly stored in the case of the embedded system and on the X86 machine (executable program on a PC on a desktop PC)?

How about this view? (correct me if I am wrong)

Suppose we have declared / initialized one variable inside a function as the data type is 'int'. Usually it goes to the stack segment, and it will only be in this section at run time when the caller calls the caller group containing the local variable. But if we declare the local variable above as "register int", then it will go to the stack segment. But at runtime, the processor puts this local variable from the stack into its general-purpose registers (due to the addition of an additional compiler due to the "register" keyword) and quick access from it.

This is the only difference between them is access at run time, and there is no difference in memory load between them.

__ Kanu

+4
source share
5 answers

The register keyword in C (rarely ever seen) is just a hint for the compiler, which may be useful to keep the variable in the register for faster access.

The compiler can ignore the hint and optimize as it sees best.

Since modern compilers are much better than people in understanding usage and speed, the register keyword is usually ignored by modern compilers, and in some cases can actually slow down the execution speed.

+10
source

From K & RC:

A register variable tells the compiler that the variable in question will be heavily used. The idea is that register variables should be placed in machine registers, which can lead to smaller and faster programs. But compilers can ignore this advice.

It is not possible to accept the address of a register variable regardless of whether the variable is actually placed in the register.

Hence,

 register int x; int *y = &x; // is illegal 

So, you have to weigh the minuses due to the inability to get the address of the register variable.

+7
source

In addition to the crypto answer (which my vote has), just look at the register name for the keyword as a historical incorrect name. It has no special relation to registers, since you study it in a class, for example, for the von Neumann processor model, but this is just a hint to the compiler that this variable does not need an address.

On modern machines, an unlimited variable can be implemented in various ways (for example, by the direct assembler operator) or fully optimized. Marking a variable as register can be a useful optimization hint for the compiler, as well as a useful discipline for the programmer.

+4
source

When the compiler takes its internal code and the backend turns it into a machine / assembler for the target processor, it keeps track of the registers that it generates for the command as it creates the code. When he needs to allocate a register for loading or tracking a variable, if there is an unused working variable, then it marks it as used and generates instructions using this register. But if all working registers have something in them, then it usually crowds out the contents of one of these registers somewhere, often spreads, for example, global memory or a stack if this variable has a home. The compiler may or may not be sensible with respect to this solution, and may evict a variable that is heavily used. Using the register keyword, depending on the compiler, you can influence this decision, it may want to save the register keyword variables in the registries and supplant the keyword variables without registers if necessary.

+1
source

what location (or register) is it exactly saved in the case of the embedded system and on the machine> X86 (executable program on PC to PC)?

You do not know without opening the output of the assembly, which can be changed depending on the choice of compiler. It is a good idea to check the assembly for educational purposes only.

If you need to accurately and accurately write and write specific registers, you must write a built-in assembly or a link in the assembly module.

Generally, when using the standard C compiler for x86 / amd64 (gcc, icc, cl), you can reasonably assume that the compiler will be reasonably well optimized for most purposes.

If, however, you use a non-standard compiler, for example, one ready for the new embedded system, it is recommended to consider manual optimization. If the architecture is new, it might also be a good idea to consider manual optimization.

0
source

All Articles