Re: twos complement and the CF (carry flag)



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.
.



Relevant Pages

  • Re: parenthesis
    ... > sub-expressions are evaluated. ... compiler had. ... > - subtract the second result from the first. ... > implementations from optimising that kind of expression too ...
    (comp.lang.c)
  • Re: A Degenerates Story
    ... but a half decent compiler would hfr the least ... worst object code for the architecture, ... integers), they should just do a subtract and check for a negative flag, ... "The life of a private citizen is infinitely preferable to the life of a Prince ...
    (uk.rec.sheds)
  • Re: parenthesis
    ... On Thu, 16 Oct 2003, Andreas Kahari wrote: ... the compiler can't decide to subtract w from ... , and then subtract x from the result, *UNLESS* the answer would ... sequence points. ...
    (comp.lang.c)
  • Re: Comparing two numbers
    ... means that it's a pretty pointless exercise if this isn't specified. ... means code in a file with a *.c extension, compiled with a C compiler. ... >> Or subtract one from the other and the test the sign bit. ...
    (comp.lang.c)
  • Re: [Clax86list] Negative numbers
    ... numbers that you want to subtract, ... subtract the borrow, resulting in 1, hence the result is 18. ... chuck of the number instead of a single decimal digit. ... overflow flag instead. ...
    (comp.lang.asm.x86)