Download presentation
Presentation is loading. Please wait.
Published byColin Byrd Modified over 6 years ago
1
The FLAGS Register An x bit means an unidentified value 9/12/2018
CAP 221
2
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/12/2018 CAP 221
3
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/12/2018 CAP 221
4
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/12/2018 CAP 221
5
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/12/2018 CAP 221
6
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/12/2018 CAP 221
7
Status Flags The Parity Flag (P): This flags reflects the number of 1s in the low byte of a result of an operation. If the number of 1s is even its value = 1 and if the number of 1s is odd then its value = 0. 9/12/2018 CAP 221
8
Overflow Flag and Carry Flag
Both are indicators of out-of-range condition. Overflow flag is used to evaluate an out-of-range condition of signed number operations . Carry flag is used in unsigned number operations. 9/12/2018 CAP 221
9
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/12/2018 CAP 221
10
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/12/2018 CAP 221
11
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/12/2018 CAP 221
12
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/12/2018 CAP 221
13
Example ADD AL,BL AL = 0Fh 00001111 BL = 08h 00001000
AL=AL+BL=17h CF=0, OF=0,Both operands and the result are positive. PF=1, ZF=0, SF=0, AF=1 9/12/2018 CAP 221
14
Example ADD AL,BL AL = 0Fh 00001111 BL = F8h 11111000
AL=AL+BL= 07h 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, = 263 > 255 (out-of-range). 9/12/2018 CAP 221
15
Example ADD AL,BL AL = 4Fh 01001111 BL = 40h 01000000
AL=AL+BL=8Fh CF=0, OF=1, The result and operands have different sign bits. PF=0, ZF=0, SF=1, AF=0 As a signed operation, = 143 > 127 (out-of-range). As an unsigned operation, 143 < 255 (ok). 9/12/2018 CAP 221
16
Example ADD AL,BL AL = F8h 11111000 BL = 81h 10000001
AL=AL+BL=79h CF=1, OF=1, Operands are negatives, the result is positive. PF=0, ZF=0, SF=0, AF=0 As a signed operation, = -135 < -128 (out-of-range). As an unsigned operation, = 377 > 255 (out-of-range). 9/12/2018 CAP 221
17
Example BX = 0001h 0000000000000001 AX=AX+BX=0000h 1 0000000000000000
ADD AX,BX AX = FFFFh BX = 0001h AX=AX+BX=0000h CF=1, OF=0,Operands have different sign bits. PF=1, ZF=1, SF=0, AF=1 As a signed operation, FFFFh+ 0001h = 10000h (out-of-range). As an unsigned operation, FFFFh+0001h=-1+1=0 (ok). 9/12/2018 CAP 221
18
Example BX = 7FFFh 0111111111111111 AX=AX+BX=FFFEh 0 1111111111111110
ADD AX,BX AX = 7FFFh BX = 7FFFh AX=AX+BX=FFFEh CF=0, OF=1, The result and operands have different sign bits. PF=0, ZF=0, SF=1, AF=1 As a signed operation, 7FFFh+ 7FFFh = = = = FFFEh (out-of-range). As an unsigned operation, 7FFFh+7FFFh = = (ok). 9/12/2018 CAP 221
19
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/12/2018 CAP 221
20
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/12/2018 CAP 221
21
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/12/2018 CAP 221
22
How instructions affect the flag
Instruction Affects flags MOV/XCHG none ADD/SUB all INC/DEC all except CF NEG all (CF=1 unless result is 0, OF=1 if word operand is 8000h, or byte operand is 80h) 9/12/2018 CAP 221
23
Example BX = + FFFFh 1 FFFEh= 1111 1111 1111 1110
ADD AX,BX AX = FFFFh BX = + FFFFh 1 FFFEh= CF=1, OF=0, PF=0, ZF=0, SF=1 9/12/2018 CAP 221
24
Example ADD AL,BL AL = 80h BL = + 80h 1 00h
CF=1, OF=1, PF=1, ZF=1, SF=0 9/12/2018 CAP 221
25
Example SUB AX,BX AX = h BX = h 7FFFh = CF=0, OF=1, PF=1, ZF=0, SF=0 9/12/2018 CAP 221
26
Example INC AL, AL=FFh FFh + 1h 100h OF=0, PF=1, ZF=1, SF=0
CF unaffected 9/12/2018 CAP 221
27
Example MOV AX, -5 AX= -5= FFFBh, none of the flags are affected
NEG AX, AX=8000h 8000h = 2’s complement = = 8000h CF=1, OF=1, PF=1, ZF=0, SF=1 9/12/2018 CAP 221
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.