0% found this document useful (0 votes)
13 views21 pages

Understanding CPU Flags in Assembly Language

Coal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

Understanding CPU Flags in Assembly Language

Coal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Welcome to

Computer Organization &


Assembly Language
Mueed Ahmed
Week # 6 [Link]@[Link]

1
2-1

2
Course Outline

3
Reference Material

4
Grading Policy
Grading Criteria Score
Quizzes + Assignments 20
Projects Presentation for evaluation 10
Class Attendance & Behavior 10

Mid Term Exam 20


Final Exam 40

➢ Attendance Policy

▪ Attendance will be taken regularly.


▪ There will be a 0.5% grade deduction for every unexcused absence.
▪ Late students can attend, but every 3 late records will be assumed to hold marks as one absent.

Computer Organization & Assembly Language Lecture # 1 5


Today Agenda
In today's lecture, we will explore into the history of computer architecture, exploring
key concepts and advancements. Our primary discussion points will be:

➢ Flags
➢ Types of flags
➢ Instruction affects on flags

Computer Organization & Assembly Language Lecture # 1 6


The FLAG Register
• We used flags for
• control the operations of the CPU
• Handles the status of operations
• Nine individual bits called as flag are used to represent the 8086 processor state.
• Carry , Parity, Auxiliary, Zero, Sign, Trap, Interrupt, Direction, Overflow
• Flags are placed in FLAG Register.
• Is the register that contains the current state of the processor.
• Used only 9 bits
• 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.
7
Flags

Carry flag
Overflow
Direction Parity flag

Interrupt enable Auxiliary flag

Trap Zero
Sign
6 are status flags handle the result
3 are control flag control the operations
8
The Status Flags
1. Carry Flag: (CF)
• 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.
• 1: when there is last carry out
• 0: when there is not carry out
• Examples:
• 11111111b + 00000011b = 1 00000010b
1 1 1 1 1 11

11111111
+00000011
00000010

9
Contd..
2. Parity Flag: used for validity of data
• Set (PE), Unset (PO)
• PE (Even Parity): If the low byte of a result has an even number of on
bits. For Even parity, PF =1
• PO (Odd Parity): If the low byte of a result has odd number of on bits. For
Odd parity, PO = 0
• 1: when there is even number of bits
• 0: when there is not even number of bits
• Examples: 11111111b + 00000011b = 00000010b(Number of one’s in
result = 1, so PF = 0)
1 1 1 1 1 11

11111111
+00000011
00000010
10
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.
• 1: when 3rd bit carry exits
• 0: when 3rd bit carry not exits
• Examples: 11111111b + 00000011b = 00000010b
1 1 1 1 1 11

11111111
+00000011
00000010

11
Contd..
4. Zero Flag:
• Set (ZR), Unset (NZ)
• Zero Flag is set when the result is zero.
• Zero Flag is unset when result is non zero.
• Examples: 0FFh – 0FFh = 00h

12
Contd..
5. Sign Flag:
• Set when MSB of a result is 1; it means the result is negative (signed
interpretation)
1: when result is negative
0: when result is positive
• Examples: 3-7=-4
00000011b – 00000111b = 00000100b

13
Contd..
6. Overflow Flag:
• Set (OV), Unset (NV)
• Set if signed overflow occurred, otherwise it is 0.
1: when result is too big to fit in the destination
0: when result is not too big to fit in the destination
• 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.
• Four possible outcomes of an arithmetic operation:
• No Overflow
• Only Signed Overflow
• Only Unsigned Overflow
• Both Signed and Unsigned Overflow
How Instructions Affect the Flags
Instructions Affects Flags

MOV/XCHG None

ADD/SUB All

INC/DEC All except CF

NEG Multiply by -1

15
Example 5.1
• ADD AX, BX, where AX contains FFFFh, BX contains FFFFh
• Solution:
• Actual Result = 1FFFEh
• Result stored in AX = FFFEh
• Flags:
• SF = 0
• PF = 0 because there are 7 (odd number) of 1 bits in the low byte of
the result. (131070)10 (1 11111111 11111110) 2
• ZF = 0 because nonzero result
• CF = 1 because there is a carry out of the MSB on addition
• OF = 1

16
Example 5.2
• ADD AL, BL where AL contains 80h, BL contains 80h
• Solution:
• Actual Result = 100h (256)10 (1 00000000)2
• Result in AL = 00h
• Flags:
• SF = 0 because MSB is 0
• PF = 1 because all bits in result are 0
• ZF = 1 because result is 0
• CF = 1 because there is a carry out from MSB
• OF = 1

17
Example 5.3
• MOV AX, -5
• Solution:
• Result in AX = -5
• None of the flags are affected by MOV

18
Program
ORG 100h
.CODE
MOV AX, 4000h ; AX = 4000h
ADD AX, AX ; AX = 8000h
SUB AX, 0FFFFh ; AX = -7FFFh
NEG AX ; AX = 7FFFh
INC AX ; AX = 8000h
MOV AH, 4Ch
INT 21H

19
Control Flags
1. Direction Flag: Controls the assumed direction used by string processing
instructions. 1=Up, 0=Down
2. Interrupt Flag: Enable/Disable external interrupt.
3. Trap Flag: Determines whether or not CPU will be halted after each
instruction.

20
Home Work / Reading

William Stallings - Computer Organization and Architecture Designing for


Performance (8th Edition)

Chapter # 3:
(related this lecture)
Web Link: [Link]

21

You might also like