Here is a simple example of code that makes use of the carry flag:
int main (void)
{
unsigned int smallnum;
unsigned int largenum;
unsigned int temp_num;
printf("Enter a number: ");
scanf("%d", &smallnum);
printf("Enter a bigger number: ");
scanf("%d", &largenum);
temp_num = smallnum - largenum;
if (smallnum < largenum)
{
printf("Carry Flag SET!");
}
else
{
printf("Carry Flag CLEAR!");
}
return(EXIT_SUCCESS);
}
If we look at the listing file we see the following:
48:carry.c **** if (smallnum < largenum)
82 .loc 1 48 0
83 0090 8B542418 movl 24(%esp), %edx
84 0094 8B442414 movl 20(%esp), %eax
85 0098 39C2 cmpl %eax, %edx
86 009a 730E jae L2
So the if
statement gets compiled to a compare of the two operands followed by a jae
or "Jump if Above or Equal". The test used in a jae
command is to check if the Carry Flag is equal to 0
. See this reference to see which flags get tested for which conditional jumps.
When you're writing code, generate a listing file and look at all the conditional jumps. Lots of them are testing to check the state of the Carry Flag.