the flags register

24
06/10/22 CAP 221 1 The FLAGS Register An x bit means an unidentified value

Upload: louie

Post on 06-Feb-2016

73 views

Category:

Documents


3 download

DESCRIPTION

The FLAGS Register. An x bit means an unidentified value. Status Flags. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The FLAGS Register

04/22/23 CAP 221 1

The FLAGS Register

An x bit means an unidentified value

Page 2: The FLAGS Register

04/22/23 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.

Page 3: The FLAGS Register

04/22/23 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.

Page 4: The FLAGS Register

04/22/23 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.

Page 5: The FLAGS Register

04/22/23 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.

Page 6: The FLAGS Register

04/22/23 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.

Page 7: The FLAGS Register

04/22/23 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.

Page 8: The FLAGS Register

04/22/23 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.

Page 9: The FLAGS Register

04/22/23 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.

Page 10: The FLAGS Register

04/22/23 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.

Page 11: The FLAGS Register

04/22/23 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

Page 12: The FLAGS Register

04/22/23 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

Page 13: The FLAGS Register

04/22/23 CAP 221 13

Example

ADD AL,BL

AL = 0Fh 00001111

BL = 08h 00001000

AL=AL+BL=17h 000010111

CF=0, OF=0,Both operands and the result are positive. PF=1, ZF=0, SF=0, AF=1

Page 14: The FLAGS Register

04/22/23 CAP 221 14

Example

ADD AL,BL AL = 0Fh 00001111

BL = F8h 11111000AL=AL+BL= 07h 100000111

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).

Page 15: The FLAGS Register

04/22/23 CAP 221 15

Example

ADD AL,BL

AL = 4Fh 01001111BL = 40h 01000000

AL=AL+BL=8Fh 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).

Page 16: The FLAGS Register

04/22/23 CAP 221 16

Example

ADD AL,BL

AL = F8h 11111000 BL = 81h 10000001

AL=AL+BL=79h 101111001

CF=1, OF=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).

Page 17: The FLAGS Register

04/22/23 CAP 221 17

Example

ADD AX,BX

AX = FFFFh 1111111111111111 BX = 0001h 0000000000000001

AX=AX+BX=0000h 1 0000000000000000CF=1, OF=0,Operands have different sign bits.

PF=1, ZF=1, SF=0, AF=1As a signed operation, FFFFh+ 0001h = 10000h

(out-of-range). As an unsigned operation, FFFFh+0001h=-1+1=0

(ok).

Page 18: The FLAGS Register

04/22/23 CAP 221 18

Example

ADD AX,BX

AX = 7FFFh 0111111111111111 BX = 7FFFh 0111111111111111

AX=AX+BX=FFFEh 0 1111111111111110CF=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 anunsigned operation, 7FFFh+7FFFh =32767 +

32767 = 65534 (ok).

Page 19: The FLAGS Register

04/22/23 CAP 221 19

How instructions affect the flag

Instruction Affects flagsMOV/XCHG noneADD/SUB allINC/DEC all except CFNEG all (CF=1 unless result is 0, OF=1 if word operand is 8000h, or byte operand is 80h)

Page 20: The FLAGS Register

04/22/23 CAP 221 20

Example

ADD AX,BX

AX = FFFFh

BX = + FFFFh

1 FFFEh= 1111 1111 1111 1110

CF=1, OF=0, PF=0, ZF=0, SF=1

Page 21: The FLAGS Register

04/22/23 CAP 221 21

Example

ADD AL,BL

AL = 80h

BL = + 80h

1 00h

CF=1, OF=1, PF=1, ZF=1, SF=0

Page 22: The FLAGS Register

04/22/23 CAP 221 22

Example

SUB AX,BX

AX = 8000h

BX = - 0001h

7FFFh = 0111 1111 1111 1111

CF=0, OF=1, PF=1, ZF=0, SF=0

Page 23: The FLAGS Register

04/22/23 CAP 221 23

Example

• INC AL, AL=FFh

FFh

+ 1h

100h

OF=0, PF=1, ZF=1, SF=0

CF unaffected

Page 24: The FLAGS Register

04/22/23 CAP 221 24

Example

• MOV AX, -5

AX= -5= FFFBh, none of the flags are affected

• NEG AX, AX=8000h

8000h = 1000 0000 0000 0000 2’s complement = 1000 0000 0000 0000 = 8000h CF=1, OF=1, PF=1, ZF=0, SF=1