0

Since processors follow the convention of representing numbers as 2's complement how do they know whether the number resulted from an addition of two positive numbers is still positive and not negative.

For example if I add two 32bit numbers:

Let r2 contains the value- 0x50192E32

Sample Code:

add r1, r2, #0x6F06410C
str r1, [r3]

Here an overflow flag is set. Now if I want to use the stored result from memory in later instructions(somewhere in the code...and by now due to different instructions let the processors cpsr has been changed) as shown below:

ldr r5, [r3]
add r7, r5

As the result of the first add instruction has 1 in it's MSB i.e.now r5 has 1 in it's MSB how do the processor interpret the value. Since the correct result on adding two positive numbers is positive. Is it just because the MSB has 1, it interprets as negative number? In that case we get different results from expected one.

Let for example in a 4 bit machine:

2's complement: 4=0100 and 5=0101; -4=1100 and -5=1011

now 4+5=9 and if it is stored in a register/memory as 1001, and later if it is being accessed by another instruction and given the processor stores numbers in 2's complement format and checks the MSB and thinks that it is a negative 7.

If it all depends upon a programmer then how do one store the correct results in reg/mem. Is there anyway that we can do to our code to store the correct results?

Arvind
  • 198
  • 2
  • 11
  • 2
    I don't quite get what you're asking - two's complement means that signedness is irrelevant to arithmetic operations, so the `add` just adds the numbers. Nothing's being interpreted as signed _or_ unsigned here, because no code is testing the flags in any way (note also that nothing's even setting them; that would be `adds`). – Notlikethat Sep 02 '16 at 16:03
  • I agree it is unclear. I think between notlikethat's comment and jasonharper's answer it just about covers all interpretations. – rjp Sep 02 '16 at 16:49
  • depending on what syntax neither of those touch the flags often you need to add the S, adds vs add. – old_timer Sep 02 '16 at 17:23

2 Answers2

2

If you care about overflow conditions, then you'd need to check the overflow flag before the status register is overwritten by some other operation - depending on the language involved, this may result in an exception being generated, or the operation being retried using a longer integer type. However, many languages (C, for example) DON'T care about overflow conditions - if the result is out of range of the type, you simply get an incorrect result. If a program written in such a language needs to detect overflow, it would have to implement the check itself - for example, in the case of addition, if the operands have the same sign, but the result is different, there was an overflow.

jasonharper
  • 9,450
  • 2
  • 18
  • 42
2

I know I have covered this many times as have others.

The carry flag can be considered the unsigned overflow flag for addition it is also the borrow flag or not borrow flag for subtraction depending on your architecture. The v flag is the signed overflow flag for addition (subtraction). YOU are the only one who knows or cares whether or not the addition is signed or unsigned as for addition/subtraction it doesnt matter.

It doesnt matter what flag it is, or what architecture, YOU have to make sure that if you care about the result (be it the result or a flag) that you preserve that information for as long as you have to until you need to use it, it is not the processors job to do that nor the instruction set nor the architecture in general. It goes for the answers in the registers as it does for the flags, it is all on you the programmer. Just preserve the state if you care. This question is like saying how do you solve this:

if(a==b)
{
}
stuff;
stuff;
I want to do the if a == b thing now.

It is all on you the programmer to make that work do the compare at the time you need to use it instead of at some other time, save the result of the compare at the time of the compare and then check the condition at the time you need to use it.

old_timer
  • 69,149
  • 8
  • 89
  • 168