overflow
home

C U : Carry Unsigned       O S : Overflow Signed

Status Flags
ALU has status flags that reflect the outcome of arithmetic operations based on the contents of the destination operand Flags: Zero flag - destination equals zero Sign flag - destination is negative Carry flag - UNSIGNED value out of range (CU) Overflow flag - SIGNED value out of range (OS)

A flag is SET when it equals 1. A flag is CLEAR when it equals 0.


The 'mov' instruction NEVER affects ANY flag. The 'inc' & 'dec' instructions NEVER affect CF.
ADD and SUB affect the status flags according to result of the operation ZF (zero flag) = 1 iff the result is zero SF (sign flag) = 1 iff the msb of the result is one OF (overflow flag) = 1 iff there is a signed overflow CF (carry flag) = 1 iff there is an unsigned overflow

Carry Flag (unsigned)
Carry Flag (CU) set when result of operation generates an ========== UNSIGNED value is out of range for the given number of bits. 8 BIT ----- mov al, 255 ; 255 is ff hex add al, 1 ; CF=1, AL = 00 mov al, -1 ; -1 is FF hex IDENTICAL to above example add al, 1 ; CF=1, AL = 00 mov al, 0ffh ; IDENTICAL to above examples add al, 1 ; CF=1, AL = 00 mov al, 11111111b ; IDENTICAL to above examples add al, 1 ; CF=1, AL = 00 mov al, 377o ; IDENTICAL to above examples add al, 1 ; CF=1, AL = 00 WHY IS THIS DIFFERENT?? mov ah, 0ffh ; 255? -1? inc ah ; ah becomes 00h, BUT CF ?? => NOT CHANGED! 16 BIT ------ ; WHY no overflow here? mov ax, 0ffh add ax, 1 ; CF=0, AX 100h ; overflow here mov bx, 65535 ; equals FFFF hex add bx, 1 ; CF = 1, BX = 0000 ; same as above mov bx, 177777o ; equals FFFF hex add bx, 1 ; CF = 1, BX = 0000 IDEA ---- If the result will not "fit" in the provided space, then you have a problem. If the result (255 + 1 = 0) does not "make sense", then you have a problem. MECHANICS --------- If there is a carry out of the high bit, the carry flag is set.

Overflow Flag (signed)
Overflow Flag (OS) set when SIGNED result of an operation is out of range for the given number of bits. ============= ; Example mov al, +127 add al, 1 ; OF = 1, AL = 80h ; you need to understand that in signed (two's complement) notation, 80h is -128 ; Example - same mov al, 7Fh ; OF = 1, AL = 80h add al, 1 The examples are identical at the binary level because 7Fh equals +127. To determine the value of the destination operand, it is often easier to calculate in hex. ; Example mov al, -128 ; 80h add al, -1 ; OF = 1 , AL = 7fh ; you need to understand that in signed (two's complement) notation, 80h is -128 ; you need to understand that in signed (two's complement) notation, 7fh is 127
Both types (SIGNED UNSIGNED) of overflow occur independently and are signaled separately by CF (unsigned) and OF (signed) mov al, 01h ; (add 1 to 1) add al, 01h ; AL=02h, OF=0, CF=0 mov al, 0FFh ; (add 1 to 255? or to -1?) (ffh) add al, 1 ; AL=00h, OF=0, CF=1 mov al, 7Fh ; (add 1 to 127 = 128? or -128) (80h) add al, 1 ; AL=80h, OF=1, CF=0 mov al, 80h ; (add what to 128 or to -128?) add al, 80h ; AL=00h, OF=1, CF=1 IDEA ---- If the result will not "fit" in the provided space, then you have a problem. If the result (127 + 1 = -128) does not "make sense", then you have a problem. MECHANICS --------- When adding two integers, remember that the Overflow (signed)flag is only set when: Two positive operands are added and their sum is negative Two negative operands are added and their sum is positive If the signs of the operands are different (hi bit(), then there is never overflow. One way to detect overflow is to look at the sign bit (high bit) of the sum. If the sign bit of the sum is different than the sign bit of the operands, then there's overflow. Looking at the high bit (leftmost): if you have carryin and carry out, then there is NO overflow. You can simply XOR the carryin and carryout of the hi bit and set the flag to that result. So the overflow flag is only sety when the carryin and carryout to the high bit is different.