The FLAGS Register An x bit means an
The FLAGS Register An x bit means an unidentified value 9/15/2020 CAP 221 1
Status Flags • The Carry Flag (C): This flag is set when the result of an unsigned arithmetic operation is too large to fit in the destination register. CF=1 if a carry from most significant bit (msb) on addition, or a borrow into msb on subtraction; otherwise CF=0. 9/15/2020 CAP 221 2
Status Flags • The Overflow Flag (O): This flag is set when the result of a signed arithmetic operation is too large to fit in the destination register (i. e. when an overflow occurs). Overflow can occur when adding two numbers with the same sign (i. e. both positive or both negative). A value of 1 = overflow and 0 = no overflow. 9/15/2020 CAP 221 3
Status Flags • The Sign Flag (S): This flag is set when the result of an arithmetic or logic operation is negative. This flag is a copy of the MSB of the result (i. e. the sign bit). A value of 1 means negative and 0 = positive. 9/15/2020 CAP 221 4
Status Flags • The Zero Flag (Z): This flag is set when the result of an arithmetic or logic operation is equal to zero. A value of 1 means the result is zero and a value of 0 means the result is not zero. 9/15/2020 CAP 221 5
Status Flags • The Auxiliary Carry Flag (A): This flag is set when an operation causes a carry from bit 3 to bit 4 (or a borrow from bit 4 to bit 3) of an operand. A value of 1 = carry and 0 = no carry. 9/15/2020 CAP 221 6
Status Flags • The Parity Flag (P): This flags reflects the number of 1 s in the low byte of a result of an operation. If the number of 1 s is even its value = 1 and if the number of 1 s is odd then its value = 0. 9/15/2020 CAP 221 7
Overflow Flag and Carry Flag • Both are indicators of out-of-range condition. • Overflow flag is used to evaluate an out-ofrange condition of signed number operations. • Carry flag is used in unsigned number operations. 9/15/2020 CAP 221 8
Overflow Flag and Carry Flag • Since a binary number can represent an unsigned number or signed number, the processor computes both flags and the user checks the appropriate flag to check if the result is out of range or not. 9/15/2020 CAP 221 9
Unsigned Overflow CF • Carry Flag CF is set to 1 if there is an end carry in an addition operation or there an end borrow in a subtraction operation. A value of 1 = carry and 0 = no carry. 9/15/2020 CAP 221 10
Signed Overflow OF • Overflow Flag is set to 1 when adding two numbers the same sign and the result has a different sign. Otherwise, OF is reset to 0. • Subtraction operation A - B can be performed as operation A + (-B). OF=1 if the result has a different sign than expected • OF=1 if the carries into and out of the msb don’t match 9/15/2020 CAP 221 11
Overflow Flag and Carry Flag • There are four possible conditions happen due to arithmetic operations. • OF=0, CF=0 • OF=0, CF=1 • OF=1, CF=0 • OF=1, CF=1 9/15/2020 CAP 221 12
Example ADD AL, BL AL = 0 Fh BL = 08 h AL=AL+BL=17 h 00001111 00001000 000010111 CF=0, OF=0, Both operands and the result are positive. PF=1, ZF=0, SF=0, AF=1 9/15/2020 CAP 221 13
Example ADD AL, BL AL = 0 Fh BL = F 8 h AL=AL+BL= 07 h 00001111100000111 CF=1, OF=0, Operands have different sign bits. PF=0, ZF=0, SF=0, AF=1 As a signed operation, 15 + (-8) = 7 (ok). As an unsigned operation, 15 + 248 = 263 > 255 (out-of -range). 9/15/2020 CAP 221 14
Example ADD AL, BL AL = 4 Fh BL = 40 h AL=AL+BL=8 Fh 01001111 01000000 010001111 CF=0, OF=1, The result and operands have different sign bits. PF=0, ZF=0, SF=1, AF=0 As a signed operation, 79 + 64 = 143 > 127 (out-of -range). As an unsigned operation, 143 < 255 (ok). 9/15/2020 CAP 221 15
Example ADD AL, BL AL = F 8 h BL = 81 h AL=AL+BL=79 h 111110000001 101111001 CF=1, Operands are negatives, the result is positive. PF=0, ZF=0, SF=0, AF=0 As a signed operation, -8 + -127 = -135 < -128 (out-of-range). As an unsigned operation, 248 + 129 = 377 > 255 (out-of-range). 9/15/2020 CAP 221 16
Example ADD AX, BX AX = FFFFh 11111111 BX = 0001 h 000000001 AX=AX+BX=0000 h 1 00000000 CF=1, OF=0, Operands have different sign bits. PF=1, ZF=1, SF=0, AF=1 As a signed operation, FFFFh+ 0001 h = 10000 h (out-of-range). As an unsigned operation, FFFFh+0001 h=-1+1=0 (ok). 9/15/2020 CAP 221 17
Example ADD AX, BX AX = 7 FFFh 011111111 BX = 7 FFFh 011111111 AX=AX+BX=FFFEh 0 111111110 CF=0, OF=1, The result and operands have different sign bits. PF=0, ZF=0, SF=1, AF=1 As a signed operation, 7 FFFh+ 7 FFFh = =32767 + 32767 = 65534 = FFFEh (out-of-range). As an unsigned operation, 7 FFFh+7 FFFh =32767 + 32767 = 65534 (ok). 9/15/2020 CAP 221 18
How the processor indicates Overflow • The processor sets: OF=1 for signed overflow CF =1 for unsigned overflow • The processor does not interpret the result as signed or unsigned • It is the programmer who interprets the results and take the appropriate action: If signed interpretation OF is of interest If unsigned interpretation CF is important 9/15/2020 CAP 221 19
How the processor determines that Overflow occurred Unsigned overflow: Ø For Addition: CF=1 if there is a carry out of msb Ø For subtraction: CF=1 if there is a borrow into msb Signed overflow: Ø OF=1 if the carries into and out of the msb don’t match 9/15/2020 CAP 221 20
Signed overflow • On addition of numbers with the same sign, or subtraction of numbers with different signs overflow can occur • Two conditions under which overflow can occur are: (i) positive add positive gives negative (ii) negative add negative gives positive 9/15/2020 CAP 221 21
How instructions affect the flag Instruction MOV/XCHG ADD/SUB INC/DEC NEG 9/15/2020 Affects flags none all except CF all (CF=1 unless result is 0, OF=1 if word operand is 8000 h, or byte operand is 80 h) CAP 221 22
Example ADD AX, BX AX = FFFFh BX = + FFFFh 1 FFFEh= 1111 1110 CF=1, OF=0, PF=0, ZF=0, SF=1 9/15/2020 CAP 221 23
Example ADD AL, BL AL = BL = 80 h + 80 h 1 00 h CF=1, OF=1, PF=1, ZF=1, SF=0 9/15/2020 CAP 221 24
Example SUB AX, BX AX = BX = 8000 h - 0001 h 7 FFFh = 0111 1111 CF=0, OF=1, PF=1, ZF=0, SF=0 9/15/2020 CAP 221 25
Example • INC AL, AL=FFh + 1 h 100 h OF=0, PF=1, ZF=1, SF=0 CF unaffected 9/15/2020 CAP 221 26
Example • MOV AX, -5 AX= -5= FFFBh, none of the flags are affected • NEG AX, AX=8000 h = 1000 0000 2’s complement = 1000 0000 = 8000 h CF=1, OF=1, PF=1, ZF=0, SF=1 9/15/2020 CAP 221 27
- Slides: 27