Howmay varaibles can be declared as register?

R

RANNA

Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
want to declare 10 register variables in my code, is it possible to do
it?
 
J

James Kuyper

RANNA said:
Say the CPU has only AL, BL, CL, DL or eight 8 bit registers, and if
want to declare 10 register variables in my code, is it possible to do
it?

You can declare as many register variables as you want. The compiler is
free to ignore any of those declarations; it's even free to ignore all
of them.

If all 10 variables must exist at the same time, the compiler can't
possibly implement them all as registers on such a machine. However,
keep in mind that the compiler has a lot of freedom to rearrange your
code, so long as the rearranged code has the same behavior as the
original. If it finds a rearrangement such that two of your variables
are never storing a value as the same time, it might implement both of
them as using the same register. If it finds two such pairs, it might be
able to put all ten variables into 8 registers. However, it's more
likely to ignore some or all of your 'register' declarations, and make
up it's own mind about which variables should be stored in registers.
When doing so, it will probably make a better choice than the one that
you made.

If it's really important to you to control such things, the C language
is the wrong one to use. You probably should use some sort of assembly
language.
 
T

Tim Rentsch

James Kuyper said:
You can declare as many register variables as you want. The compiler is
free to ignore any of those declarations; it's even free to ignore all
of them.

<pardon-my-pedantry>
Of course what is meant is that the compiler may ignore the 'register'
storage class specifiers, not that it may ignore the declarations.
 
K

Kenny McCormack

<pardon-my-pedantry>
Of course what is meant is that the compiler may ignore the 'register'
storage class specifiers, not that it may ignore the declarations.
</pardon-my-pedantry>

<super-pedant-mode>

Someone will come along and point out that the other key thing about
register variables is that you can't take their address.

So, the compiler is not free to ignore the 'register' keyword (as if you
had done: #define register)
in the sense that if you later try to apply the & to such a variable, it
has to flag it.
 
R

Richard Tobin

Kenny McCormack said:
<super-pedant-mode>
So, the compiler is not free to ignore the 'register' keyword (as if you
had done: #define register)
in the sense that if you later try to apply the & to such a variable, it
has to flag it.

Ha, call that super-pedantry?

A compiler is free to issue any diagnostics it likes for legal code.
So it could warn every time the address of *any* variable is taken,
and it would then be free to ignore "register".

-- Richard
 
C

CBFalconer

Richard said:
Ha, call that super-pedantry?

A compiler is free to issue any diagnostics it likes for legal
code. So it could warn every time the address of *any* variable
is taken, and it would then be free to ignore "register".

No, it isn't free to ignore 'register'. It is free to avoid the
act of assigning a register. It is not free to allow access to the
address of that variable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top