Click to See Complete Forum and Search --> : Carry Flag


_Dave_
November 11th, 2004, 07:22 AM
Hello CodeGurus.

I am reading "Assembly Language for Intel-Based Computers" by Kip R. Irvine. Section 4.2.6.2 is about the Carry Flag (CF). It says if we subtract an integer from a smaller integer, the CF will be set. This is true when I run, for example...

.data
val1 BYTE 1
val2 BYTE 2

.code
mov al, val1
sub al, val2 ; CF = 1 SF = 1

But when I do the arithmetic on paper...

Carry
0000 0001 1
1111 1110 -2

1111 1111 which is -1 in 2's complement form.

Where is the carry out??? It says earlier in the book that the CF is set when the result of an unsigned arithmetic operation is too large to fit into the destination operand. But -1 fits into AL.

Thanks,
Dave

kahlinor
November 27th, 2004, 03:43 AM
Hi, Dave.

Your doing a 1+ ( NEG 2 ) operation on 'paper' which will only set the sign flag in computer flags terms, even though you get the exact same result as SUB 1, 2


I have written a little utility which gives you a visual look at what happens to registers and flags during logical and arithmatic operations.

If you're interested, take a look at my cCalc program, attached.

-Sevag K.

kahlinor
November 27th, 2004, 07:43 PM
Let me clarify the previous post a little bit.

Rules for Binary Subtraction:

0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
0 - 1 = 1 w/borrow

0000 0001
0000 0010
--------------
1111 1111