c++ - Checking overflow for addc function -


i coding c++ program simulate 16 bit virtual machine. have function 'addc' in have check overflow. i'm little lost on conditions overflow. know overflow happens when add 2 positive numbers , negative number, or when add 2 negative numbers , positive number. however, in function addc, have 3 numbers have add. approaching correctly?

my function supposed this:

register destination = register destination + register source + carry bit

(rd = rd + rs + c)

so far checking overflow, have code..

//check when adding positive numbers gives negative result //my carry bit positive (either 0 or 1) if(rd >= 0 , rs >= 0 , c >= 0) , ((rd + rs + c) < 0)){     //set overflow bit } else if( rd < 0 , rs < 0 , c < 0) , (rd + rs + c) > 0) ){     //set overflow bit } 

my confusion else if condition. c 0 or 1, it'll never go else if loop, there's no reason have else if loop. doing right? or have add carry bit (c) 1 of operands (either rd or rs) have 2 operands compare. this?

else if( rd < 0 , ((rs + c) < 0)) , (rd + (rs + c)) > 0) ){     //set overflow bit } 

the fact second if statement seems operating under impression carry bit possibly negative should first clue doesn't add (as foghorn leghorn say, "hey, think made funny!").

the right way simplify case of single add operation of 3 values 2 separate add operations. first, add rd , rs, , check overflow using conditions you've defined. then, take result of first addition, , add carry bit, , check overflow here. second overflow check can simple, given carry bit either 0, or 1.

the final result set overflow bit if either first or second add operation determined have overflowed.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -