Flags and Flow Control Structure
Flags and Flow Control Structure
Flags and Flow Control Structure
Language
Lab 4 Flags and Flow control introduction CSC 321
Mughees Ahmad
mugheesahmad@cuilahore.edu.pk
The FLAG Register
Nine individual bits called as flag are used to represent the
8086 processor state.
Flags are placed in FLAG Register.
Two types of flags:
Status Flags: Reflects the result of a computation. Located in
bits: 0, 2, 4, 6, 7 and 11.
Control Flags: Used to enable/disable certain operations of the
processor. Located in bits 8, 9 and 10.
2
The Status Flags
1. Carry Flag:
Set (CY), Unset (NC)
The Carry Flag is set to 1 when there is a carry out from MSB on
addition or there is a borrow into the MSB on subtraction. Also affected
by shift and rotate instructions.
Examples:
128 + 128 = 256 (1 byte = 0 - 255)
0FFh + 11h = 110h (If a register can store only 1 byte, then where to
store the carry generated by MSB?)
1000 0001b – 1000 0010b = 11111111b (How processor would know a
borrow is required to perform subtraction?)
3
Contd..
2. Parity Flag:
Set (PE), Unset (PO)
PE (Even Parity): If the low byte of a result has an even number of one
bits. For Even parity, PF =1
PO (Odd Parity): If the low byte of a result has odd number of one bits.
For Even parity, PF = 0
Examples:
1000 0001b – 1000 0010b = 11111111b (Number of one’s in result = 8,
so PF = 1)
4
Contd..
3. Auxiliary Carry Flag:
Set (AC), Unset (NA)
The Auxiliary Carry Flag is set to 1 if there is a carry out from bit 3 on
addition, or a borrow into bit 3 on subtraction.
Used in Binary Coded Decimal (BCD) Operations.
Examples:
1000 0001b
– 0000 0010b
= 01111111b (Borrow from bit 4 to bit 3)
5
Contd..
4. Zero Flag:
Set (ZR), Unset (NZ)
Zero Flag is 1 when the result is zero.
Zero Flag is 0 when result is non zero.
Examples:
6
Contd..
5. Sign Flag:
Set when MSB of a result is 1; it means the result is negative (signed
interpretation)
Unset/0 when MSB is 0 i.e. result is positive.
Examples:
0FFh – 0FFh = 00h (MSB = 0, PF = 0)
7
Contd..
6. Overflow Flag:
Set if signed overflow occurred, otherwise it is 0.
Overflow:
Range of numbers that can be represented in a computer is limited.
If the result of an operation falls outside the defined range, Overflow
occurs and the truncated result will be incorrect.
1 BYTE SIGNED = -128 to 127
1 BYTE UNSIGNED = 0 to 255
8
How Instructions Affect the Flags
Instructions Affects Flags
MOV/XCHG None
ADD/SUB All
NEG All
9
Example 5.1
ADD AX, BX, where AX contains FFFFh, BX contains FFFFh
Solution:
Actual Result = 1FFFEh
Result stored in AX = FFFEh
Flags:
SF = 1 because the MSB is 1
PF = 0 because there are 7 (odd number) of 1 bits in the low byte of the
result.
ZF = 0 because nonzero result
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 (in binary addition, there is a carry into the MSB and
carry out from MSB also)
10
Example 5.5
MOV AX, -5
Solution:
Result in AX = -5 = FFFBh
11
High – level operators 8086 conditional jumps
> JG / JNLE
< JL / JNGE
>= JGE / JNL
<= JLE /JNG
== JE / JZ
!= JNE / JNZ
12
Unconditional Jump
Syntax:
JMP destination_label
Purpose: To Transfer control to another part of the program.
Example:
MOV AX, 2
MOV BX, 2
JMP LABEL_SUB
ADD AX, BX ;this instruction will never execute
LABEL_SUB:
SUB AX, BX
13
Conditional Jump
Syntax:
Jxxx destination_label
where xxx represents the condition
If condition is true, the next instruction to be executed is the one at
destination_label.
If condition is false, the instruction immediately following the jump is done
next.
Note: The destination_label must precede the jump instruction by no more
than 126 bytes.
To implement a conditional jump, the CPU looks at the FLAG register (set
by last instruction executed by the processor).
JUMP instructions themselves do not affect flags.
14
CMP Instruction
CMP (compare) instruction performs an implied subtraction of a source
operand from destination operand. Neither operand is modified.
CMP destination, source
FLAGS
CMP Result ZF CF
Destination = Source 1 0
15
CMP Instruction Examples
Destination < Source:
mov ax, 5
cmp ax, 10 ;CF = 1, ZF = 0
Destination = Source
mov ax, 1000
mov cx, 1000
cmp cx, ax ; ZF = 1, CF = 0
Destination > Source
mov ax, 105
cmp ax, 0 ; ZF = 0 and CF = 0
16
High Level Language Structures
Branching Structure
IF-THEN
IF-THEN-ELSE
CASE
Branching with Compound Conditions
AND CONDITIONS
OR CONDITIONS
Looping Structures
FOR LOOP
WHILE LOOP
17
IF-THEN
;Code in Assembly Language:
JGE END_IF
NEG AX
Replace AX
END_IF:
by –AX
MOV AH, 4CH
INT 21H
End
18
IF-THEN-ELSE
;Code in Assembly Language:
MOV AH, 2
CMP AL, BL ;If AL <= BL
Display the Display the JLE ELSE_
character in BL character in AL MOV DL, AL ;then
JMP DISPLAY
ELSE_:
MOV DL, BL
DISPLAY:
End INT 21H
19