Accessing flags

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

    Accessing flags

    Hi

    What's the best way in C to access the processor's flags register? I'm
    thinking particularly of checking the carry or overflow flag after an
    arithmetic operation.

  • Walter Roberson

    #2
    Re: Accessing flags

    In article <pan.2008.06.06 .21.36.14.93633 5@spamtrap.inva lid>,
    kid joe <spamtrap@spamt rap.invalidwrot e:
    >What's the best way in C to access the processor's flags register? I'm
    >thinking particularly of checking the carry or overflow flag after an
    >arithmetic operation.
    There is no standard way of doing that in C; if you were able to
    do it at all in your implementation, it would be by some
    implementation-specific method.

    Furthermore, on some processors, the status bits that exist are
    not very persistant: if you had something like

    a = b + c;

    then after the addition of b + c then some processor status bit
    might be set, but the subsequent move of the result into a might
    end up changing the status bits.

    --
    "Whenever there is a hard job to be done I assign it to a lazy
    man; he is sure to find an easy way of doing it."
    -- Walter Chrysler

    Comment

    • Barry Schwarz

      #3
      Re: Accessing flags

      On Fri, 06 Jun 2008 22:36:14 +0100, kid joe
      <spamtrap@spamt rap.invalidwrot e:
      >Hi
      >
      >What's the best way in C to access the processor's flags register? I'm
      >thinking particularly of checking the carry or overflow flag after an
      >arithmetic operation.
      By reading the documentation for your system and learning the system
      specific method it provides for doing this. Alternately, you could
      ask in a newsgroup that discusses your system.


      Remove del for email

      Comment

      • Antoninus Twink

        #4
        Re: Accessing flags

        On 6 Jun 2008 at 21:53, Walter Roberson wrote:
        kid joe <spamtrap@spamt rap.invalidwrot e:
        >>What's the best way in C to access the processor's flags register? I'm
        >>thinking particularly of checking the carry or overflow flag after an
        >>arithmetic operation.
        >
        Furthermore, on some processors, the status bits that exist are
        not very persistant: if you had something like
        >
        a = b + c;
        >
        then after the addition of b + c then some processor status bit
        might be set, but the subsequent move of the result into a might
        end up changing the status bits.
        Is there really any architecture where a mov instruction alters the
        overflow or carry bit? It seems unlikely... On x86, moves don't modify
        any flags at all.

        Comment

        • Eric Sosman

          #5
          Re: Accessing flags

          kid joe wrote:
          Hi
          >
          What's the best way in C to access the processor's flags register? I'm
          thinking particularly of checking the carry or overflow flag after an
          arithmetic operation.
          There's no "best way" because as far as C is concerned
          there's no way at all. One tiny problem is that C can be
          (and has been) implemented on machines that have no flags.

          Unfortunately, this leaves the C programmer in a bit of a
          bind when it comes to overflow detection. For some operations
          on some data types you can inspect the results to determine
          whether there was an overflow. For example,

          unsigned int ua = ..., ub = ...;
          unsigned int sum = ua + ub;
          if (sum >= ua) {
          /* the mathematical sum was in representable range */
          }
          else {
          /* the sum was out of range and has been reduced */
          }

          However, this approach won't necessarily work for all types.
          For signed integers an overflow can produce fatal (or at least
          unwelcome) effects as soon as it occurs, so you can't wait until
          afterwards: You need to prevent it from happening in the first
          place. That leads to ugly code like

          int sa = ..., sb = ...;
          int sum;
          if (sa >= 0) {
          if (sb <= INT_MAX - sa)
          sub = sa + sb;
          else {
          /* sum would be too large */
          }
          }
          else {
          if (sb >= sa - INT_MIN)
          sum = sa + sb;
          else {
          /* sum would be too small */
          }
          }

          In actual practice it's rare to see code of this sort even
          in a few places, and you'll just never see it applied everywhere
          in a program! What people usually do, for better or worse, is
          choose data types that will (they think) be able to handle the
          expected range of data without overflowing, and then cross their
          fingers and pretend overflow can never happen. Often they're
          right, but the results can be spectacular when they're wrong ...

          Some C compilers on some machines provide non-portable
          extensions to get at such information (plus some promises on
          how overflows are handled, so the information can be useful).
          Note, though, that just "reading the flags register" is most
          likely not sufficient, since you'd need to check it after each
          potentially problematic operation:

          y = ((a * x + b) * x) + c;
          if (overflow_flag_ is_set()) /* sorry; too late */

          /* instead: */
          y = a * x;
          if (overflow_flag_ is_set()) ...
          y += b;
          if (overflow_flag_ is_set()) ...
          y *= x;
          if (overflow_flag_ is_set()) ...
          y += c;
          if (overflow_flag_ is_set()) ...

          What would be Really Nice would be a "latching flag" that would
          be set by any overflow and remain set until explicitly cleared:

          clear_overflow_ latch();
          y = ((a * x + b) * x) + c;
          if (overflow_latch _is_set()) ...

          However, there's no such thing -- not even a "volatile" flag --
          in C itself, presumably because it would be punishingly slow
          to provide on many machines. (C follows the long-established
          tradition that speed is more important than correctness. ;-)

          --
          Eric.Sosman@sun .com

          Comment

          • CBFalconer

            #6
            Re: Accessing flags

            kid joe wrote:
            >
            What's the best way in C to access the processor's flags register?
            I'm thinking particularly of checking the carry or overflow flag
            after an arithmetic operation.
            They are not available, and in fact may not even exist. In any
            standard C system.

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


            ** Posted from http://www.teranews.com **

            Comment

            • robertwessel2@yahoo.com

              #7
              Re: Accessing flags

              On Jun 6, 5:15 pm, Antoninus Twink <nos...@nospam. invalidwrote:
              On  6 Jun 2008 at 21:53, Walter Roberson wrote:
              >
              kid joe  <spamt...@spamt rap.invalidwrot e:
              >What's the best way in C to access the processor's flags register? I'm
              >thinking particularly of checking the carry or overflow flag after an
              >arithmetic operation.
              >
              Furthermore, on some processors, the status bits that exist are
              not very persistant: if you had something like
              >
                a = b + c;
              >
              then after the addition of b + c then some processor status bit
              might be set, but the subsequent move of the result into a might
              end up changing the status bits.
              >
              Is there really any architecture where a mov instruction alters the
              overflow or carry bit? It seems unlikely... On x86, moves don't modify
              any flags at all.

              The 6800 would clear (set to zero) overflow and set (to values correct
              for the data moved) the zero and sign flags on loads, stores and
              register to register moves, although it left the carry alone in those
              cases. The 6502 would set the zero and sign bits on loads and
              register to register moves (but leave overflow and carry alone, and
              altered no flags on stores).

              The other thing to remember is that if the program has to do any sort
              of address computation during the store, that might have to alter the
              flags as well. Nor is there any guarantee that the CPU in question
              has flags (most RISCs), or that the flag modifying instructions are
              actually used. For example, on x86 a compiler could use MMX/SSE
              instructions to add numbers, or use something like LEA to add. In
              fact the use of LEA is pretty common, especially if the expression is
              slightly more complex: say “a = b+2*c;”.

              Comment

              • rio

                #8
                Re: Accessing flags


                "Eric Sosman" <Eric.Sosman@su n.comha scritto nel messaggio
                news:1212790572 .671162@news1nw k...
                kid joe wrote:
                >Hi
                y = ((a * x + b) * x) + c;
                if (overflow_flag_ is_set()) /* sorry; too late */
                >
                /* instead: */
                y = a * x;
                if (overflow_flag_ is_set()) ...
                y += b;
                if (overflow_flag_ is_set()) ...
                y *= x;
                if (overflow_flag_ is_set()) ...
                y += c;
                if (overflow_flag_ is_set()) ...
                >
                What would be Really Nice would be a "latching flag" that would
                be set by any overflow and remain set until explicitly cleared:
                >
                clear_overflow_ latch();
                y = ((a * x + b) * x) + c;
                if (overflow_latch _is_set()) ...
                yes this can be the best

                there is anhother way: define
                a new integer type and an
                aritmetic +, *, -, \
                on that integer
                so if somehting during the operation goes wrong the result is the integer
                value OE [overflow error]
                in the way x_NEW*y_NEW=x*y <= x * y not overflow
                x_NEW*y_NEW=OE <=x*y overflow [or underflow?]
                where "*" can be each possible operation

                exaple
                NewIntegerType a, x, b, c;
                y = ((a * x + b) * x) + c;
                if (y==OE) ...
                However, there's no such thing -- not even a "volatile" flag --
                in C itself, presumably because it would be punishingly slow
                to provide on many machines. (C follows the long-established
                tradition that speed is more important than correctness. ;-)
                >
                --
                Eric.Sosman@sun .com

                Comment

                • Walter Banks

                  #9
                  Re: Accessing flags



                  kid joe wrote:
                  Hi
                  >
                  What's the best way in C to access the processor's flags register? I'm
                  thinking particularly of checking the carry or overflow flag after an
                  arithmetic operation.
                  C compilers that support ISO 18037 can directly access the
                  processor flags limited only by the processor access limits.

                  _register_cc cc;
                  or
                  register volatile _CC cc;

                  if (cc.v == 1) // test overflow

                  int a,b;

                  a = b + cc.c; // add with carry


                  Regards

                  --
                  Walter Banks
                  Byte Craft Limited
                  Tel. (519) 888-6911

                  walter@bytecraf t.com







                  Comment

                  • Walter Roberson

                    #10
                    Re: Accessing flags

                    In article <484A6D48.280FC 244@bytecraft.c om>,
                    Walter Banks <walter@bytecra ft.comwrote:
                    >C compilers that support ISO 18037 can directly access the
                    >processor flags limited only by the processor access limits.
                    I didn't recall what ISO 18037 was, so I checked briefly; it appears
                    to be C extensions for embedded processors. Potentially useful
                    if you work with embedded processors, but not applicable to other
                    situations.
                    --
                    "MAMA: Oh--So now it's life. Money is life. Once upon a time freedom
                    used to be life--now it's money. I guess the world really do change.
                    WALTER: No--it was always money, Mama. We just didn't know about it."
                    -- Lorraine Hansberry

                    Comment

                    Working...