Howmay varaibles can be declared as register?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RANNA

    Howmay varaibles can be declared as register?

    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?
  • James Kuyper

    #2
    Re: Howmay varaibles can be declared as register?

    RANNA wrote:
    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.

    Comment

    • Eric Sosman

      #3
      Re: Howmay varaibles can be declared as register?

      RANNA wrote:
      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?
      Yes. Declare a hundred if you like, or a thousand.

      But it probably won't do you any good. Declaring a variable with
      the `register' storage class does two things: It prevents you from
      forming any pointers to the variable, and it suggests that access to
      the variable should be as fast as possible. Note that "suggests" is
      not the same as "commands," and the compiler may choose to ignore
      your suggestion. Nowadays, few compilers pay attention to it, so
      you're left with only the first effect.

      In the Bad Old Days, `register' could have a significant effect
      on the performance of a program. Note that "significan t" is not
      necessarily the same as "beneficial ;" sometimes adding `register'
      made the program slower ...

      --
      Eric.Sosman@sun .com

      Comment

      • Tim Rentsch

        #4
        Re: Howmay varaibles can be declared as register?

        James Kuyper <jameskuyper@ve rizon.netwrites :
        RANNA wrote:
        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.
        <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>
        [...]

        Comment

        • Kenny McCormack

          #5
          Re: How many varaibles can be declared as register?

          In article <kfnprl43pfb.fs f@alumnus.calte ch.edu>,
          Tim Rentsch <txr@alumnus.ca ltech.eduwrote:
          >James Kuyper <jameskuyper@ve rizon.netwrites :
          >
          >RANNA wrote:
          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.
          >
          ><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.

          Comment

          • Richard Tobin

            #6
            Re: How many varaibles can be declared as register?

            In article <gf99jo$cb$1@ne ws.xmission.com >,
            Kenny McCormack <gazelle@shell. xmission.comwro te:
            ><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
            --
            Please remember to mention me / in tapes you leave behind.

            Comment

            • CBFalconer

              #7
              Re: How many varaibles can be declared as register?

              Richard Tobin wrote:
              Kenny McCormack <gazelle@shell. xmission.comwro te:
              >
              ><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".
              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.

              --
              [mail]: Chuck F (cbfalconer at maineline dot net)
              [page]: <http://cbfalconer.home .att.net>
              Try the download section.

              Comment

              • Richard Heathfield

                #8
                Re: How many varaibles can be declared as register?

                CBFalconer said:
                Richard Tobin wrote:
                >Kenny McCormack <gazelle@shell. xmission.comwro te:
                >>
                >><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".
                >
                No, it isn't free to ignore 'register'.
                Under the conditions that Richard Tobin stated, it is.
                It is free to avoid the act of assigning a register.
                Yes.
                It is not free to allow access to the address of that variable.
                Wrong. If a program attempts to take the address of an object whose
                identifier is register-qualified, a constraint has been broken, and this
                requires a diagnostic message. If the implementation diagnoses *every*
                occurrence of an address being taken, then it will diagnose the taking of
                the address of an object whose identifier is register-qualified. It is
                then free to produce an executable program, or not - either is legal. The
                behaviour of such a program, if produced, would of course be undefined.

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                Working...