Question

I am taking an Introduction to Embedded Systems Class. As I was reading, I encountered an interesting question on the implementations of the carry bit and overflow bit.

I know what a carry bit and overflow bit is, however I cannot think of a situation in where someone would use a carry bit. One reason i thought was to align memory. Can someone shed light on this issue please?

Was it helpful?

Solution

The carry flag is useful for efficiently performing arithmetic and logical operations on data that is wider than the processor's accumulator or registers. This may not be a concern on a modern 64-bit processor, but early microprocessors and some current microcontrollers may still have only an 8-bit or 16-bit accumulator. The carry bit would permit add/subtract and shift/rotate of any multi-word length with the single accumulator. Besides the basic add, subtract, shift and rotate instructions (to begin the operation on multi-word data), there would be add-with-carry, subtract-with-borrow, shift-with-carry and rotate-with-carry instructions (to operate on subsequent words). And to facilitate such code sequences, the INC reg and DEC reg instructions (for pointer and loop counter modification) would not modify (and therefore preserve) the carry flag, even though they were arithmetic instructions.

Some microcontrollers (e.g. Intel 8051) also use the carry flag as a read destination or write source for its single-bit port I/O operations.

The carry and overflow flags (and perhaps some other flags, e.g. half-carry, sign or zero flags, depending on the processor architecture) are set or cleared on various arithmetic and logical operations. The processor's instruction set should be consulted for what flags exist and the conditions for which they are modified by instructions.

OTHER TIPS

Carry bit is used in machine instructions to test less_than/greater_than conditions for unsigned variables for example, or loop termination condition, as subtracting 1 from 0 leads to setting carry bit.

Also in assembly language the carry of arithmetic operations such as addition can be used to easily implement arbitrary precision arithmetic.

Carry bit is not often visible to the programmer in higher level languages.

Overflow bit is rarely used -- but as indicated by the semantics, it can and should be used by scientific algorithms, whose correctness must be assured over the specified signed integer range. add reg1, reg2; JO exception_handler allows efficient testing if the algorithm working on signed integers produces correct results. Same applies for subtraction and integer multiplication (IMUL), but not integer division, after which all (arithmetic) flags are undefined.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top