How Instructions Affect the Flags

advertisement
Lecture 4
( Assembly Language)
1
Lecture Outline
• Introduction
• The FLAGS Register
• Overflow
• How instruction affect the flags
2
Introduction
• One important feature that distinguishes a computer from other
machines is the computer’s ability to make decisions.
• Decision are made based on the current state of the processor.
• In the 8086 processor, the processor state is implemented as nine
individual bits called flags.
• Each decision made by the 8086 is based on the values of these
flags.
• The flags are placed in the FLAGS register.
3
The FLAGS Register
• Two kinds of flags:
• Status flags: reflect the result of an instruction executed by the
processor (ex. ZF ,PF ,SF ,OF)
• Control Flag: enable or disable certain operations of the
processor (ex. If the IF “Interrupt Flag” is set to 0,
inputs from the keyboard are ignored by the
processor).
4
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
• Status Flags
• Carry Flag (CF)
• Parity Flag (PF)
• Auxiliary carry Flag (AF)
• Zero Flag (ZF)
• Sign Flag (SF)
• Overflow Flag (OF)
• Control Flags
• Trap Flag (TF)
• Interrupt Flag (IF)
• Direction Flag (DF)
5
The FLAGS Register - Status Flags
• Carry Flag (CF):
• CF = 1 if there is a carry out from the msb on addition.
• CF = 1 if there is a borrow into the msb on subtraction.
• CF = 0 otherwise.
• CF is also affected by shift and rotate instructions.
• Parity Flag (PF):
• PF = 1 if the low byte of a result has an even number of one bits
• PF = 0 if the low byte has odd parity
(ex. if a result is FFFE
PF = 0).
6
The FLAGS Register - Status Flags
• Auxiliary carry Flag (AF):
• AF = 1 if there is a carry out from bit 3 on addition.
• AF = 1 if there is a borrow into bit 3 on subtraction.
• AF = 0 otherwise.
• AF is used in BCD operations.
• Zero Flag (ZF):
• ZF = 1 for a zero result.
• ZF = 0 for nonzero result.
7
The FLAGS Register - Status Flags
• Sign Flag (SF):
• SF = 1 if the msb of a result is 1.
(i.e. Negative if signed interpretation is used).
• SF = 0 if the msb is 0.
• Overflow Flag (OF):
• OF = 1 if signed overflow occurred.
• OF = 0 otherwise.
8
Overflow
• The phenomenon of overflow is associated with the fact that the
range of numbers that can be represented in a computer is limited.
• If the result of an operation falls outside the range, overflow occurs
and the truncated result that is saved will be incorrect.
9
How the Processor Determines that Overflow Occurred?
Signed Overflow
• On addition of numbers with the same sign, signed overflow occurs
when the sum has a different sign.
• Subtraction of numbers with different signs is like adding numbers
of the same sign (ex. A - (-B) = A + B). Signed overflow occurs if
the result has a different sign than expected.
• In addition of numbers with different signs, overflow is impossible.
• Subtraction of numbers with the same sign cannot give overflow.
10
How Instructions Affect the Flags
• In general, each time the processor executes an instruction, the flags
are altered to reflect the result.
• However, some instructions don’t affect any of the flags, affects
only some of them, or may leave them undefined.
Instruction
MOV/XCHG
ADD/SUB
INC/DEC
NEG
Affects flags
none
all
all except CF
all (CF = 1 unless result is 0,
OF = 1 if word operand is 8000h
or byte operand is 80h)
11
How Instructions Affect the Flags
Example: ADD AX, BX
where AX contains FFFFh, BX contains FFFFh
FFFFh
+FFFFh
1FFFEh = 1111 1111 1111 1110b
SF = 1 because the msb is 1.
PF = 0 because there are 7 (odd number) of 1 bits in the low byte.
ZF = 0 because the result is nonzero.
CF = 1 because there is a carry out of the msb on addition.
OF = 0 because the sign of the stored result is the same as that of the
numbers being added.
12
How Instructions Affect the Flags
Example: ADD AL, BL
where AL contains 80h, BL contains 80h
80h
+80h
100h
SF = 0 because the msb is 0.
PF = 1 because all the bits in the result are zero.
ZF = 1 because the result is 0.
CF = 1 because there is a carry out of the msb on addition.
OF = 1 because the numbers being added are both negative, but the
result is 0.
13
How Instructions Affect the Flags
Example: SUB AX, BX
where AX contains 8000h, BX contains 0001h
8000h
- 0001h
7FFFh = 0111 1111 1111 1111b
SF = 0 because the msb is 0.
PF = 1 because there are 8 (even number) one bits in the low byte.
ZF = 0 because the result is nonzero.
CF = 0 because a smaller unsigned number is being subtracted from a
larger one.
OF = 1 because a positive number is being subtracted from a negative
one (like adding two negatives), and the result is positive.
14
How Instructions Affect the Flags
Example: MOV AX, -5
None of the flags are affected by MOV.
15
Download