Re: twos complement and the CF (carry flag)
- From: Herbert Kleebauer <klee@xxxxxxxxx>
- Date: Mon, 29 Jan 2007 19:18:52 +0200
LuB wrote:
I am a newbie ... self-studying Kip Irvine's 4th edition assembly
book. I may have mistyped an example program into my compiler - but
assuming not ...
I've got a basic handle on binary numbers and twos complement notation
- but I have a question about the CARRY FLAG.
Since the CF is really only relevant for unsigned operations ??? I
assume it looks at each transaction as though it were being carried
out on unsigned values.
So, since "sub" is implemented as adding the twos complement, why does
the CF=1 (carry flag get set to 1) when subtracting 90h from 80h and
not when adding 70h to 80h?
Assuming byte size,
the valid range of numbers is
for unsigned numbers: 0 - 255
for signed numbers: -128 - +127 (2th complement)
If you add or subtract unsigned numbers and the result is outside 0 - 255
the Carry is set. If you add or subtract signed numbers and the result is
outside -128 - +127 the Overflow flag is set.
Aren't they the same from an unsigned perspective? Nothing gets
carried ... what is the CPU doing when subtracting 90h from 80h that
it knows to mark the CF=1?
80h + 70h = F0h, CF=0
that is
1000 0000 + 0111 0000 = 1111 0000
128 + 112 = 240 is within 0-255 so Carry is not set
-128 + 112 = -16 is within -128 - +127 so Overflow is not set
but
80h - 90h = F0h, CF=1
that is
90h => 1001 0000 => 0110 1111 + 0000 0001 => 0111 0000
so
80h - 90h = 80h + 70h = F0h.
128 - 144 = -16 is outside 0 - 255 so Carry is set
-128 - (-112) = -16 is within -128 - +127 so Overflow is not set
Why does the CARRY FLAG = 1?
Or do mean the generating hardware? For a subtraction the Carry
generated by the adder has to be inverted to produce the correct Carry.
.
- Follow-Ups:
- References:
- twos complement and the CF (carry flag)
- From: LuB
- twos complement and the CF (carry flag)
- Prev by Date: Re: Problems posting to CLAX...
- Next by Date: Re: my little implementation of malloc
- Previous by thread: twos complement and the CF (carry flag)
- Next by thread: Re: twos complement and the CF (carry flag)
- Index(es):
Relevant Pages
|