Re: signed unsiged add instruction
- From: Herbert Kleebauer <klee@xxxxxxxxx>
- Date: Thu, 29 Mar 2007 13:16:13 +0200
Gianmaria wrote:
There is no difference in signed/unsigned addition if you
use 2's complement representation for signed numbers.
that's clear.
If that's clear, then I don't understand your question below.
Suppose (just for simplicity) to work with 3 bit long word (000, 001
etc. range -4..3) and you want to add two signed number: 100 (-4) and
111 (-1). The result -5 is to big to be encoded in the 3 bit signed
word.
I was thinking that in case you use signed add instruction this will
preserve the sign bit (the leftmost bit) and give the result 111. On
the contrary if you use a unsigned add I was thinking the result would
be 011. So, if I understand correctly what I'm saying here is
perfectly incorrect :) Isn't it?
You want to add the binary numbers 111 + 011. (NOTE: add/sub is
always done modulo 2^n, in this case modulo 8)
The ALU doesn't know whether this bit sequences are signed
or unsigned numbers so it has to add them without this information:
100
+111
-----
011
It's up to the user to interpret this binary numbers. If you
want them to be unsigned numbers this means: 4 + 7 = 3
Or for signed numbers this means: -4 + -1 = 3
Both interpretations are correct (don't forget, we do modulo 8
arithmetic).
Processors which uses flags, signals an overflow (wrapping around
modulo 8) by setting one of the flags. Because the CPU doesn't know
whether the binary number should represent a signed or unsigned
number, it has to use two different flags to signal an overflow.
The C flag signals on overflow if the binary numbers represent
an unsigned number and the V flag if the number is a signed number.
So in the example above C=1 and V=1.
.
- Follow-Ups:
- Re: signed unsiged add instruction
- From: Gianmaria
- Re: signed unsiged add instruction
- References:
- signed unsiged add instruction
- From: Gianmaria
- Re: signed unsiged add instruction
- From: Herbert Kleebauer
- Re: signed unsiged add instruction
- From: Gianmaria
- signed unsiged add instruction
- Prev by Date: Re: signed unsiged add instruction
- Next by Date: Re: OT: Re: Ten years later
- Previous by thread: Re: signed unsiged add instruction
- Next by thread: Re: signed unsiged add instruction
- Index(es):
Relevant Pages
|