Labsheet 05
Labsheet 05
2
The FLAGS Register
3
The FLAGS Register
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
OF DF IF TF SF ZF AF PF CF
• Carry Flag(CF)
• CF=1 if there is a carry out in the MSB on addition, if there is a borrow into
the MSB on subtraction
• Otherwise CF=0
• It is also affected by shift and rotate instructions
• Parity Flag(PF)
• PF=1 if the low byte of a result has even parity
• PF=0 if the low byte of a result has odd parity
5
The Status Flags
• Zero Flag(ZF)
• ZF=1 for a zero result
• ZF=0 for a non-zero result
• Sign Flag(SF)
• SF=1 if the MSB of a result is 1 that means the result is negative
• SF=0 if the MSB of a result is 0 that means the result is positive
• Overflow Flag(OF)
• OF=1 if signed overflow occured
• Otherwise OF=0
6
Overflow
• If the result of an operation falls out of these ranges then overflow occurs
and the truncated result that is saved will be incorrect
• When we perform an arithmetic operation such as addition there are four
possible outcomes:
• No overflow
• Signed overflow only
• Unsigned overflow only
• Both signed and unsigned overflows
7
Example of Unsigned
Overflow Only
+ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
8
Example of Signed
Overflow Only
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
+ 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
9
Overflow Indicates by
Processor
10
Overflow Occur
Determination by Processor
• Unsigned overflow
• On addition when there is a carry out in the MSB. This means the result
is larger than the biggest unsigned number
• On subtraction when there is a borrow in the MSB. This means the
correct answer is smaller than 0
• Signed overflow
• On addition the numbers with the same sign produces result of
different sign
• On subtraction the result has a different sign than expected
• Subtraction of numbers with different signs means addition of number
with same sign.
11
How Instructions Affect
the Flags
12
How Instructions Affect the
Flags
• ADD AX,BX where AX contains FFFFh and BX contains FFFFh
• Solution
F F F F h
+ F F F F h
1 F F F E h
13
Assignment 05
• For each of the following instructions, give the new
destination contents and the new settings of CF,SF,ZF,PF and
OF. Suppose that the flags are initially 0 in each part of this
question
• ADD AX, BX where AX contains 7FFFh and BX contains 0001h
• DEC AL where Al contains 00h
• NEG AL where AL contains 7Fh
• XCHG AX, BX where AX contains 1ABCh and BX contains 712Ah
14