Lecture05-06
Lecture05-06
Remember...
• A flag is set when it equals 1.
• A flag is clear when it equals 0.
Sign Flag (SF)
• The Sign flag is set when the destination operand is negative. The flag
is clear when the destination is positive.
mov cx,0
sub cx,1 ; CX = -1, SF = 1
add cx,2 ; CX = 1, SF = 0
mov al,0FFh
add al,1 ; CF = 1, AL = 00
mov al,0
sub al,1 ; CF = 1, AL = FF
Overflow
Overflow occurs when the result cannot fit into the destination field after some
arithmetic operation
Types of Arithmetic Overflows
– No overflow
– Signed overflow only (OF = 1)
– Unsigned overflow only (CF = 1)
– Both signed and unsigned overflows (OF=1 and CF=1)
Programmers must interpret the results
– Use the appropriate FLAG to interpret the correct result
– For example, if a signed interpretation needs to be used, only the OF is of interest!
Your turn . . .
For each of the following marked entries, show the values of the
destination operand and the Sign, Zero, and Carry flags:
mov ax,00FFh
add ax,1 ; AX=0100h SF=0 ZF=0 CF=0
sub ax,1 ; AX=00FFh SF=0 ZF=0 CF=0
add al,1 ; AL=00h SF=0 ZF=1 CF=1
mov bh,6Ch
add bh,95h ; BH=01 SF=0 ZF=0 CF=1
mov al,2
sub al,3 ; AL=FFh SF=1 ZF=0 CF=1
Parity Flag (PF)
The parity flag is set when the least significant byte of the
destination has an even number of 1 bits.
; Example
mov al, 10001100b
add al, 00000010b ; AL = 10001110, PF = 1
sub al, 10000000b ; AL = 00001110, PF = 0
Auxiliary Carry Flag
The Auxiliary carry flag indicates a carry or borrow out of bit 3 in the
destination operand. It is primarily used in binary coded decimal
(BCD) arithmetic, but can be used in other contexts.
; Example
mov al, 0Fh
add al, 1 ; AF = 1
Here is the arithmetic:
00001111
+ 00000001
------------------------
00010000
More Examples from Text
• Determine how each instruction sets 5 of the STATUS FLAGS (We are not
interested in the AF, the Auxiliary Carry Flag)
• Example 1
AX: FFFF BX: FFFF
ADD AX,BX
AX:FFFE
AL: 80 BL: 80
ADD AL,BL
AL:00
SUB AX,BX
AX:7FFF