|
From: Eric S. <er...@br...> - 2004-06-11 05:38:24
|
In searching the archives for messages regarding LCDs, I see that there was some discussion of the overflow flag not too long ago. Perhaps this has already been resolved, but in case it hasn't... I don't know how Microchip has implemented this, and have not tried writing a test program, but I do know what most if not all other microprocessors do. Suppose you're doing an eight-bit add, so your operands are a7..a0 (MSB through LSB), b7..b0, and c0 (carry in). Your result is r7..r0 and c8 (carry out). Inside the adder, there is a carry from each stage to the next. (Assume a ripple carry adder; the actual implementation may be different but the principle is the same.) So the carry from stage 6 to stage 7 is c7. The overflow is c7 xor c8. In C this can be done with two additions, some masking and shifting, and an xor: sum = a + b + c0; r = sum & 0xff; c8 = sum >> 8; v = (c8 ^ ((((a & 0x7f) + (b & 0x7f) + c0) & 0x80) >> 7)) & 1; This can be extended in the obvious way for longer (or shorter) word lengths. Replace 8, 7, 0x80, and 0x7f with wsize, (wsize-1), (1<<(wsize-1)), and ((1<<(wsize-1))-1). For subtraction, the same thing applies. The carry in and out are considered to be "not borrow". The b operand is inverted (ones complement), and an add is performed. For subtract without a borrow input, set the carry input of the adder to one (no borrow). Some microprocessors implement a "true borrow"; this is done by having the hardware invert the carry in and carry out during a subtract instruction. The PIC does not do this, because it would take two extra exclusive OR gates, and provide no real benefit. Eric |