Flags and Flow Control Structure

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Microprocessor and Assembly

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:

0FFh – 0FFh = 00h

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

INC/DEC All except CF

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 0 1

Destination > Source 0 0

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:

False True MOV AX, -128d


AX < 0 CMP AX, 0

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:

False True MOV AH, 1


INT 21H
AL <= BL
MOV BL, AL ;first input in BL
INT 21H ;second input in AL

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy