473,378 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.

Jun 27 '08 #1
9 5196
In article <pa****************************@spamtrap.invalid >,
kid joe <sp******@spamtrap.invalidwrote:
>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
Jun 27 '08 #2
On Fri, 06 Jun 2008 22:36:14 +0100, kid joe
<sp******@spamtrap.invalidwrote:
>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
Jun 27 '08 #3
On 6 Jun 2008 at 21:53, Walter Roberson wrote:
kid joe <sp******@spamtrap.invalidwrote:
>>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.

Jun 27 '08 #4
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. ;-)

--
Er*********@sun.com
Jun 27 '08 #5
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 **
Jun 27 '08 #6
On Jun 6, 5:15*pm, Antoninus Twink <nos...@nospam.invalidwrote:
On *6 Jun 2008 at 21:53, Walter Roberson wrote:
kid joe *<spamt...@spamtrap.invalidwrote:
>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;”.
Jun 27 '08 #7
rio

"Eric Sosman" <Er*********@sun.comha scritto nel messaggio
news:1212790572.671162@news1nwk...
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. ;-)

--
Er*********@sun.com

Jun 27 '08 #8


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
http://www.bytecraft.com
wa****@bytecraft.com



Jun 27 '08 #9
In article <48***************@bytecraft.com>,
Walter Banks <wa****@bytecraft.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
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Xah Lee | last post by:
Python re module has methods flags and pattern. How to use these exactly? e.g. i tried print patternObj.flags() and the error is some "int object is not callable". ...
7
by: loquak | last post by:
Greets. I wonder which way would be the best for holding some state flags in terms of memory use and efficiency. a) struct FLAGS { bool flag1 : 1; bool flag2 : 1;
13
by: Mark A. Odell | last post by:
I write a lot of drivers and there is inevitably some hardware register or buffer descriptor field called 'flags'. The flags are defined, typically, as bit positions. I was thinking I could get...
6
by: Alfred B. Thordarson | last post by:
I have a problem accessing a DLL using C#. I'm using C/FRONT with Navision's CFRONT.DLL, which contains the method: DBL_S32* DBL_NextKey(DBL_HTABLE hTable, DBL_S32* Key); typedef signed long...
3
by: Ignacio X. Domínguez | last post by:
Hi. I'm using the | operator to use multiple flags in a variable. For example: const int flag1 = 0x00000001; const int flag2 = 0x00000002; const int flag3 = 0x00000004; int Flags = flag2 |...
1
by: Dwight.Dexter | last post by:
I have an enum variable. Is there some way to cast an int or BitArray and get my enum value? I have to communicate with an old C-style sever so I get an array of bits("0","1"). But I'm using an...
1
by: Stephan Zaubzer | last post by:
Hi I relatively new to C# and at the moment I am having troubles accessing com objects within C#. I am working in VS.net. I add the com library I want to access to my references. Accessing...
1
by: Alexander Korsunsky | last post by:
Hi! Is it possible to extract the mode flags of a stream (FILE* stream or fstream - object), without looking at how the stream was opened? What I mean by mode flags is wether the stream is opened...
4
by: Ralf | last post by:
Hallo, I'm trying to call a COM function from C#. The function has a parameter from the type array of IStream* (IStream**). The COM component (out-proc-server (.exe)) has the following code: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.