carry and overflow flag

4

Click here to load reader

Upload: surendrapdaceg

Post on 14-Jan-2016

224 views

Category:

Documents


0 download

DESCRIPTION

Carry and Overflow Flag, Computer Architecture, intel

TRANSCRIPT

Page 1: Carry and Overflow Flag

7/18/2019 Carry and Overflow Flag

http://slidepdf.com/reader/full/carry-and-overflow-flag 1/4

=====================================================The CARRY flag and OVERFLOW flag in binary arithmetic=====================================================- Ian! Allen - idallen"idallenca - ###idallenc$m

$ n$t c$nf%&e the 'carry' flag #ith the '$(erfl$#' flag in integerarithmetic Each flag can $cc%r $n it& $#n) $r b$th t$gether The C*+,&AL+ d$e&n,t care $r n$# #hether y$% are d$ing &igned $r %n&ignedmathematic&. the AL+ al#ay& &et& b$th flag& a//r$/riately #hen d$ing anyinteger math The AL+ d$e&n,t n$# ab$%t &igned0%n&igned. the AL+ 1%&td$e& the binary math and &et& the flag& a//r$/riately It,& %/ t$ y$%)the /r$grammer) t$ n$# #hich flag t$ chec after the math i& d$ne

If y$%r /r$gram treat& the bit& in a #$rd a& %n&igned n%mber&) y$%m%&t #atch t$ &ee if y$%r arithmetic &et& the carry flag $n) indicatingthe re&%lt i& #r$ng Y$% d$n,t care ab$%t the $(erfl$# flag #hen d$ing%n&igned math 2The $(erfl$# flag i& $nly rele(ant t$ &igned n%mber&) n$t%n&igned3

If y$%r /r$gram treat& the bit& in a #$rd a& t#$,& c$m/lement &igned(al%e&) y$% m%&t #atch t$ &ee if y$%r arithmetic &et& the $(erfl$# flag

$n) indicating the re&%lt i& #r$ng Y$% d$n,t care ab$%t the carryflag #hen d$ing &igned) t#$,& c$m/lement math 2The carry flag i& $nlyrele(ant t$ %n&igned n%mber&) n$t &igned3

In %n&igned arithmetic) #atch the carry flag t$ detect err$r&In %n&igned arithmetic) the $(erfl$# flag tell& y$% n$thing intere&ting

In &igned arithmetic) #atch the $(erfl$# flag t$ detect err$r&In &igned arithmetic) the carry flag tell& y$% n$thing intere&ting

Engli&h-------

$ n$t c$nf%&e the Engli&h (erb 't$ $(erfl$#' #ith the '$(erfl$# flag'in the AL+ The (erb 't$ $(erfl$#' i& %&ed ca&%ally t$ indicate that&$me math re&%lt d$e&n,t fit in the n%mber $f bit& a(ailable. it c$%ld beinteger math) $r fl$ating-/$int math) $r #hate(er The '$(erfl$# flag'i& &et &/ecifically by the AL+ a& de&cribed bel$#) and it i&n,t the &amea& the ca&%al Engli&h (erb 't$ $(erfl$#'

In Engli&h) #e may &ay 'the binary0integer math $(erfl$#ed the n%mber$f bit& a(ailable f$r the re&%lt) ca%&ing the carry flag t$ c$me $n'4$te h$# thi& Engli&h %&age $f the (erb 't$ $(erfl$#' i& 5n$t5 the &ame a&&aying 'the $(erfl$# flag i& $n' A math re&%lt can $(erfl$# 2the (erb3the n%mber $f bit& a(ailable #ith$%t t%rning $n the AL+ '$(erfl$#' flag

Carry Flag----------

The r%le& f$r t%rning $n the carry flag in binary0integer math are t#$6

7 The carry flag i& &et if the additi$n $f t#$ n%mber& ca%&e& a carry  $%t $f the m$&t &ignificant 2leftm$&t3 bit& added

  7777 8 9997 = 9999 2carry flag i& t%rned $n3

: The carry 2b$rr$#3 flag i& al&$ &et if the &%btracti$n $f t#$ n%mber&  re;%ire& a b$rr$# int$ the m$&t &ignificant 2leftm$&t3 bit& &%btracted

  9999 - 9997 = 7777 2carry flag i& t%rned $n3

Other#i&e) the carry flag i& t%rned $ff 2<er$3 5 9777 8 9997 = 7999 2carry flag i& t%rned $ff <er$>3

Page 2: Carry and Overflow Flag

7/18/2019 Carry and Overflow Flag

http://slidepdf.com/reader/full/carry-and-overflow-flag 2/4

 5 7999 - 9997 = 9777 2carry flag i& t%rned $ff <er$>3

In %n&igned arithmetic) #atch the carry flag t$ detect err$r&In &igned arithmetic) the carry flag tell& y$% n$thing intere&ting

O(erfl$# Flag-------------

The r%le& f$r t%rning $n the $(erfl$# flag in binary0integer math are t#$6

7 If the &%m $f t#$ n%mber& #ith the &ign bit& $ff yield& a re&%lt n%mber  #ith the &ign bit $n) the '$(erfl$#' flag i& t%rned $n

  9799 8 9799 = 7999 2$(erfl$# flag i& t%rned $n3

: If the &%m $f t#$ n%mber& #ith the &ign bit& $n yield& a re&%lt n%mber  #ith the &ign bit $ff) the '$(erfl$#' flag i& t%rned $n

  7999 8 7999 = 9999 2$(erfl$# flag i& t%rned $n3

Other#i&e) the $(erfl$# flag i& t%rned $ff

 5 9799 8 9997 = 9797 2$(erfl$# flag i& t%rned $ff3 5 9779 8 7997 = 7777 2$(erfl$# flag i& t%rned $ff3 5 7999 8 9997 = 7997 2$(erfl$# flag i& t%rned $ff3 5 7799 8 7799 = 7999 2$(erfl$# flag i& t%rned $ff3

4$te that y$% $nly need t$ l$$ at the &ign bit& 2leftm$&t3 $f the threen%mber& t$ decide if the $(erfl$# flag i& t%rned $n $r $ff

If y$% are d$ing t#$,& c$m/lement 2&igned3 arithmetic) $(erfl$# flag $nmean& the an&#er i& #r$ng - y$% added t#$ /$&iti(e n%mber& and g$t anegati(e) $r y$% added t#$ negati(e n%mber& and g$t a /$&iti(e

If y$% are d$ing %n&igned arithmetic) the $(erfl$# flag mean& n$thing

and &h$%ld be ign$red

The r%le& f$r t#$,& c$m/lement detect err$r& by e?amining the &ign $fthe re&%lt A negati(e and /$&iti(e added t$gether cann$t be #r$ng)beca%&e the &%m i& bet#een the addend& @ince b$th $f the addend& fit#ithin the all$#able range $f n%mber&) and their &%m i& bet#een them) itm%&t fit a& #ell i?ed-&ign additi$n ne(er t%rn& $n the $(erfl$# flag

In &igned arithmetic) #atch the $(erfl$# flag t$ detect err$r&In %n&igned arithmetic) the $(erfl$# flag tell& y$% n$thing intere&ting

B$# the AL+ calc%late& the O(erfl$# Flag----------------------------------------

Thi& material i& $/ti$nal reading

There are &e(eral a%t$mated #ay& $f detecting $(erfl$# err$r& in t#$,&c$m/lement binary arithmetic 2f$r th$&e $f y$% #h$ d$n,t lie the man%alin&/ecti$n meth$d3 Bere are t#$6

Calc%lating O(erfl$# Flag6 eth$d 7-----------------------------------

O(erfl$# can $nly ha//en #hen adding t#$ n%mber& $f the &ame &ign andgetting a different &ign @$) t$ detect $(erfl$# #e d$n,t care ab$%tany bit& e?ce/t the &ign bit& Ign$re the $ther bit&

With t#$ $/erand& and $ne re&%lt) #e ha(e three &ign bit& 2each 7 $r93 t$ c$n&ider) &$ #e ha(e e?actly :55=D /$&&ible c$mbinati$n& $f thethree bit& Only t#$ $f th$&e D /$&&ible ca&e& are c$n&idered $(erfl$#

Page 3: Carry and Overflow Flag

7/18/2019 Carry and Overflow Flag

http://slidepdf.com/reader/full/carry-and-overflow-flag 3/4

el$# are 1%&t the &ign bit& $f the t#$ additi$n $/erand& and re&%lt6

  AITIO4 @I4 IT@  n%m7&ign n%m:&ign &%m&ign  ---------------------------  9 9 9 5OVER5 9 9 7 2adding t#$ /$&iti(e& &h$%ld be /$&iti(e3  9 7 9  9 7 7  7 9 9  7 9 7 5OVER5 7 7 9 2adding t#$ negati(e& &h$%ld be negati(e3  7 7 7

We can re/eat the &ame table f$r &%btracti$n 4$te that &%btractinga /$&iti(e n%mber i& the &ame a& adding a negati(e) &$ the c$nditi$n& thattrigger the $(erfl$# flag are6

  @+TRACTIO4 @I4 IT@  n%m7&ign n%m:&ign &%m&ign  ---------------------------

  9 9 9  9 9 7  9 7 9 5OVER5 9 7 7 2&%btracting a negati(e i& the &ame a& adding a /$&iti(e3 5OVER5 7 9 9 2&%btracting a /$&iti(e i& the &ame a& adding a negati(e3  7 9 7  7 7 9  7 7 7

A c$m/%ter might c$ntain a &mall l$gic gate array that &et& the $(erfl$#flag t$ '7' iff any $ne $f the ab$(e f$%r OV c$nditi$n& i& met

A h%man need $nly remember that) #hen d$ing &igned math) adding

t#$ n%mber& $f the &ame &ign m%&t /r$d%ce a re&%lt $f the &ame &ign)$ther#i&e $(erfl$# ha//ened

Calc%lating O(erfl$# Flag6 eth$d :-----------------------------------

When adding t#$ binary (al%e&) c$n&ider the binary carry c$ming int$the leftm$&t /lace 2int$ the &ign bit3 and the binary carry g$ing $%t$f that leftm$&t /lace 2Carry g$ing $%t $f the leftm$&t &ign> bitbec$me& the CARRY flag in the AL+3

O(erfl$# in t#$,& c$m/lement may $cc%r) n$t #hen a bit i& carried $%t$%t $f the left c$l%mn) b%t #hen $ne i& carried int$ it and n$ matching

carry $%t $cc%r& That i&) $(erfl$# ha//en& #hen there i& a carry int$the &ign bit b%t n$ carry $%t $f the &ign bit

The OVERFLOW flag i& the GOR $f the carry c$ming int$ the &ign bit 2ifany3 #ith the carry g$ing $%t $f the &ign bit 2if any3 O(erfl$# ha//en&if the carry in d$e& n$t e;%al the carry $%t

E?am/le& 2:-bit &igned :,& c$m/lement binary n%mber&36

  77  897  ===  99

  - carry in i& 7  - carry $%t i& 7  - 7 GOR 7 = 4O OVERFLOW

Page 4: Carry and Overflow Flag

7/18/2019 Carry and Overflow Flag

http://slidepdf.com/reader/full/carry-and-overflow-flag 4/4

  97  897  ===  79

  - carry in i& 7  - carry $%t i& 9  - 7 GOR 9 = OVERFLOW!

  77  879  ===  97

  - carry in i& 9  - carry $%t i& 7  - 9 GOR 7 = OVERFLOW!

  79  897  ===  77

  - carry in i& 9  - carry $%t i& 9  - 9 GOR 9 = 4O OVERFLOW

4$te that thi& GOR meth$d $nly #$r& #ith the 5binary5 carry that g$e&int$ the &ign 5bit5 If y$% are #$ring #ith he?adecimal n%mber&) $rdecimal n%mber&) $r $ctal n%mber&) y$% al&$ ha(e carry. b%t) the carry

d$e&n,t g$ int$ the &ign 5bit5 and y$% can,t GOR that n$n-binary carry#ith the $%tg$ing carry

Be?adecimal additi$n e?am/le 2&h$#ing that GOR d$e&n,t #$r f$r he? carry36

  DAh  8DAh  ====  7Hh

  The he?adecimal carry $f 7 re&%lting fr$m A8A d$e& n$t affect the  &ign bit If y$% d$ the math in binary) y$%,ll &ee that there i&  5n$5 carry 5int$5 the &ign bit. b%t) there i& carry $%t $f the &ign

  bit Theref$re) the ab$(e e?am/le &et& OVERFLOW $n 2The e?am/le  add& t#$ negati(e n%mber& and get& a /$&iti(e n%mber3

-- Ian! Allen - idallen"idallenca - Otta#a) Ontari$) Canada B$me *age6 htt/600idallenc$m0 C$ntact Im/r$(6 htt/600c$ntactim/r$(ca0 C$llege /r$fe&&$r 2Free0Libre 4+8Lin%?3 at6 htt/600teachingidallenc$m0 efend digital freed$m6 htt/600eff$rg0 and ha(e f%n6 htt/600f$$l&ca0