Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.postscript > #1148
| From | vojta@math.berkeley.edu (Paul Vojta) |
|---|---|
| Newsgroups | comp.lang.postscript |
| Subject | Re: Calculate a 16-bit Carry Flag? |
| Date | 2012-12-05 02:57 +0000 |
| Organization | U.C. Berkeley Math. Department |
| Message-ID | <k9md6l$m41$1@dont-email.me> (permalink) |
| References | <k9a31h$ngc$1@dont-email.me> <yNGdnRpPq9tZeyXNnZ2dnUVZ_tSdnZ2d@posted.nuvoxcommunications> <k9bbcb$l24$1@dont-email.me> <JcCdnf5qr67SbCfNnZ2dnUVZ_sSdnZ2d@posted.nuvoxcommunications> |
In article <JcCdnf5qr67SbCfNnZ2dnUVZ_sSdnZ2d@posted.nuvoxcommunications>,
Robert Bonomi <bonomi@host122.r-bonomi.com> wrote:
>define what *you* mean by 'carry' and 'overflow' :)
>
>from my _distant_ memories, overflow is set on mul/div -- you can't get
>meaningful (even partial) results there.
>'carry' is set for add/sub -- only one excess/'lost' bit is possible,
>and you do multiple-precision is done by adding the 'carry' bit as a LSB
>to the 'next more significant' chunk of th operand.
>
>*dim* recollection of implementation details:
> a) 'carry' set on =signed= result that carries into sign bit (only).
> Or unsigned carry into next higher bit.
> b) 'overflow' set if anything propagates -past- the carry position
> c) 'overflow' set implies 'carry' set.
Dim, indeed.
On the 8086 family, arithmetic is two's complement, and there are no
separate "signed" or "unsigned" addition instructions.
Carry is set if the operation carries past the most significant bit.
(In other words, interpreting the bits as unsigned integers, carry for
16-bit addition is set if and only if the sum is >= 2^{16}. For 16-bit
subtraction a-b, carry is set if and only if b>a as unsigned quantities.)
This way, you can do addition of (say) 32-bit integers on a 16-bit machine
by doing a 16-bit add on the lowest significant word, and then a 16-bit ADC
(add with carry) on the corresponding higher-significant words.
Overflow is set if the operation leads to an overflow when considering the
operands as signed quantities. So, for addition, if you add two words with
the same sign bit and the sum has a different sign bit, then you get overflow;
otherwise you don't. For subtraction, it's a bit more complicated:
for a-b, if a>0, b<0, and the sign bit of the result is set, then you get
overflow; if a<0, b>0, and the sign bit of the result is not set, then you
get overflow; otherwise you don't.
Overflow does not imply carry set. For example, 0x4000+0x4000 gives 0x8000
with the overflow bit set but no carry bit.
>Aside -- beware of gotcha on 'complement and add'. can't complement a
>'maximum negative' operand. If 1st operand not 'maximum negative', then
>complement it, else if 2nd operand not 'maximum negative', then complement it,
>else (both are 'maximum negative'), SHORTCUT: result is =zero=.
Complement and add only works in one's complement arithmetic. For two's
complement (as in most modern processors, including the 8086), you need
to complement and increment to turn a (signed) word w into -w. That will
not simplify things if you are emulating a processor.
--Paul
Back to comp.lang.postscript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Calculate a 16-bit Carry Flag? "luser.droog" <luser.droog@gmail.com> - 2012-11-30 04:50 -0600
Re: Calculate a 16-bit Carry Flag? bonomi@host122.r-bonomi.com (Robert Bonomi) - 2012-11-30 11:03 -0600
Re: Calculate a 16-bit Carry Flag? "luser.droog" <luser.droog@gmail.com> - 2012-11-30 16:18 -0600
Re: Calculate a 16-bit Carry Flag? bonomi@host122.r-bonomi.com (Robert Bonomi) - 2012-12-02 00:12 -0600
Re: Calculate a 16-bit Carry Flag? "luser.droog" <luser.droog@gmail.com> - 2012-12-03 00:06 -0600
Re: Calculate a 16-bit Carry Flag? vojta@math.berkeley.edu (Paul Vojta) - 2012-12-05 02:57 +0000
Re: Calculate a 16-bit Carry Flag? "luser.droog" <luser.droog@gmail.com> - 2012-12-05 23:34 -0600
Re: Calculate a 16-bit Carry Flag? Bogus@Embarq.com (Steve) - 2012-12-07 15:59 +0000
csiph-web