Accessing flags

K

kid joe

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.
 
W

Walter Roberson

kid joe said:
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.
 
B

Barry Schwarz

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
 
A

Antoninus Twink

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.
 
R

robertwessel2

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;”.
 
W

Walter Banks

kid said:
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
http://www.bytecraft.com
(e-mail address removed)
 
W

Walter Roberson

Walter Banks said:
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.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top