News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Say Accumulator Has A Sum Of 2 Numbers,How Do You Decide An Overflow?

Started by terabaaphoonmein, July 20, 2020, 12:44:45 AM

Previous topic - Next topic

terabaaphoonmein

Say we have an accumulator AC.
And that accumulator has added value.

Say the signed bit of accumulator is either 0 or 1.

Then, how do we decide, in which case is overflow?

eg-:

a) is it overflow when signed bit=1? if yes, why?

B ) is it overflow, when signed bit=0? if yes, why?


felipe

For simplicity this example would use an accumulator of 4 bits. Now for case A:


AC = 1000 (after the sum of the numbers 0111 and 0001)


So in this case you added 7 and 1 and the result is 8, but this 8 is using the sign bit, so when using "signed numbers representation" you would get an overflow here. i.e. -8.

For case B:

AC = 0000 (after the sum of the numers 1000 and 1000)


In this case (using "signed numbers representation" again), you would get -8 + -8 = 0. This is also an overflow.

So this gives you an idea when you get an overflow when the sign bit change. So "we can decide" when there's an overflow when adding. But how a machine decide this, is a little bit different...You would use bit flags for that.


jj2007

More precisely:
QuoteIn unsigned arithmetic, watch the carry flag to detect errors.
In unsigned arithmetic, the overflow flag tells you nothing interesting.

In signed arithmetic, watch the overflow flag to detect errors.
In signed arithmetic, the carry flag tells you nothing interesting.

daydreamer

you could install a debugger and singlestep thru some arithmetic code to watch the different flags go on/off and include:
add
adc
sub
sbb
cmp (a subtraction that affects flags same way as sub,but throws away result
test
sar
sal
ror
rol
mul
div
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding