The MASM Forum

General => The Campus => Topic started by: terabaaphoonmein on July 20, 2020, 12:44:45 AM

Title: Say Accumulator Has A Sum Of 2 Numbers,How Do You Decide An Overflow?
Post by: terabaaphoonmein on July 20, 2020, 12:44:45 AM
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?
Title: Re: Say Accumulator Has A Sum Of 2 Numbers,How Do You Decide An Overflow?
Post by: jj2007 on July 20, 2020, 01:28:50 AM
Read about the carry flag.
Title: Re: Say Accumulator Has A Sum Of 2 Numbers,How Do You Decide An Overflow?
Post by: felipe on July 20, 2020, 01:54:13 AM
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.
Title: Re: Say Accumulator Has A Sum Of 2 Numbers,How Do You Decide An Overflow?
Post by: TouEnMasm on July 20, 2020, 03:04:13 AM

https://en.wikipedia.org/wiki/Carry_flag (https://en.wikipedia.org/wiki/Carry_flag)
Title: Re: Say Accumulator Has A Sum Of 2 Numbers,How Do You Decide An Overflow?
Post by: jj2007 on July 20, 2020, 03:44:07 AM
More precisely: (http://teaching.idallen.com/dat2343/10f/notes/040_overflow.txt)
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.
Title: Re: Say Accumulator Has A Sum Of 2 Numbers,How Do You Decide An Overflow?
Post by: daydreamer on July 20, 2020, 05:17:08 AM
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