views:

231

answers:

3

Is it possible to add two signed 8-bit numbers together and set both the carry and overflow bits?

+1  A: 

You don't have access to the flags in C, even if you could get the compiler to generate code that set them, you have have no way to use them.

John Knoeller
i don't think that he asked how to change the flags. he asked if both of the flags can be changed while doing a single operation
jojo
That is what I am asking, can both be changed in a single operation.
freezie
And since C has no concept of carry/overflow flags, you can't do that - unless there's an extension to your particular C compiler that tells you otherwise.
nos
Not to mention that not all processors that have C compilers have 8 bit ADD instructions. But if the question is _in general_ can an add set both carry and overflow, the answer is yes.
John Knoeller
@nos if the processor has the flags, you can cause them to be set. Whether c can access the flags has nothing to do with it.
Justin Smith
To "get the compiler to generate code that set them", all you need to do is add two numbers and have the result carry and overflow. The processor sets the flags automatically (on all the processors I know of that have the flags, at the very least).
Justin Smith
@Justin: Just because the CPU has an 8 bit addition op doesn't mean the C implementation uses it when you add together two 8 bit numbers as in the question. C arithmetic operations are never "logically" performed on types smaller than `int`, so you're relying on particular code transformations/optimisations. Anyway, I've removed the reference to C from the title and tags, since the actual question isn't about C.
Steve Jessop
@Steve Jessop: I'm glad you changed the tags, I wanted to, but I wasn't sure whether that would be seen as stepping over a line.
John Knoeller
The site philosophy is that it's like a Wiki - if the OP or anyone else with rep-based edit privileges wants to change it back, I can't stop them, so I don't think of it as interfering very much. I certainly won't get into an edit war over it, it just seems to me that "what can the C language guarantee about the state of CPU flags not mentioned in the C standard" probably isn't the important part of the question ;-)
Steve Jessop
A: 

You can write your own add routine in C that will return carry and overflow flags for signed 8-bit operands. If you're referring to the hardware carry and overflow bits inside the processor, no, that cannot be done portably in C.

Jim Lewis
You simply don't know that in the general case.
dmckee
@dmckee: I'm not sure what you're getting at here. One can certainly write an 'add' routine that returns carry and overflow flags in pure, portable C. It'll be dog-slow compared to inline assembly or other platform-specific hacks, but that wasn't the question...
Jim Lewis
Ah...I didn't read you "portable" weasel word. I think we are on the same page. Certainly you can't do anything useful with it baring non-standard extensions.
dmckee
@dmckee: As an aspiring language lawyer, I appreciate the compliment on my talent for weasel-wording!
Jim Lewis
+3  A: 

Per your comments, your question seems to be "is it possible to have both carry and overflow set for a two's complement add involving signed number?" It is. The typical implementation is to take the exclusive-OR of the carry-in for the last adder with the carry-out at the end of the chain -- hence, an overflowing addition of negative numbers will cause the carry-out bit to be set and the overflow bit to be set.

Here's an example, add -1 to -128:

Carry 10000 0000 
       1000 0000  (-128)
       1111 1111  (-1)
       ---------
       0111 1111 (oops, this is 127!)

Carry will be set, since the last add resulted in a carry -- and overflow will be set based on the rule above (also, note that -128 added to -1 is obviously not 127)

Arthur Shipkowski
Thanks a lot for this. It make so much more sense now with your eloquent explanation.
freezie