How to know if a binary sum of unsigneds has overflowed?

Asked

Viewed 430 times

4

If I have a sum of two unsigned numbers (1 byte each) in an Assembly without carry flag, how do I know if I have overflow?

Edit: the architecture in question is Neander

Solution: I found a way to do this. First check the most significant bits (msb) of the numbers to be summed.

Se ambos forem 1, teremos overflow.
Senão, se apenas um deles for 1 e o msb do resultado for 0, teremos overflow

  • It gets hard without knowing the CPU.

  • So, it’s Neander architecture

3 answers

1

If the sum of two unsigned numbers results in a number less than the smallest number then an overflow has occurred. Example:

  1111  
+ 0001  
1 0000
  • I thought about it, but how to compare the numbers without resorting to two complement?

  • I was thinking about it yesterday. But I didn’t find anything.

  • Take a look at Edit, @Fabio

  • Truth: 1111 + 1111 = 1 1110 (first case) 1111 + 0011 = 1 0010 (second case) .

  • All right, I’ll do it

1

The commands "JO" (jump on overflow) or "JNO" (jump on not overflow) make it possible to take this test and follow as appropriate.

  • It’s the Neander architecture, there’s only JZ, JNZ and JN. I’d need to implement the carry itself

0


Solution: I found a way to do this. First check the most significant bits (msb) of the numbers to be summed.

If both are 1, we will overflow.

Otherwise, if only one of them is 1 and the msb of the result is 0, we will overflow

Browser other questions tagged

You are not signed in. Login or sign up in order to post.