SlideShare a Scribd company logo
1 of 33
Flag Control
Objective:
• Identify the different flag control instruction
• Apply the different flag control instructions
Flag Control Instructions
The 8086/8088 provides a group of instructions
that directly affects the state of the flags.
• LAHF Instruction
• SAHF Instruction
• CLC Instruction
• STC Instruction
• CMC Instruction
• PUSHF Instruction
• POPF Instruction
The LAHF Instruction

The LAHF (Load AH from flags)
instruction copies the SF, ZF, AF, PF, and
CF into bits b7, b6, b4, b2, and b0
respectively of register AH.
Format:
Action:
AH
LAHF
SF x ZF x AF x PF x CF
The SAHF Instruction
The SAHF (Store AH into flags)
instruction transfers bits b7, b6, b4, b2,
and b0 from register AH into SF, ZF, AF,
PF, and CF, respectively.
SAHFFormat:
Action:
SF ZF x AF x PF x CF AH
Example:
MOV AH, 25H
SAHF
The CLC Instruction
 The CLC (Clear Carry Flag) instruction
clears the carry flag.
 Format: CLC
 Action: CF 0
The STC Instruction
The STC (Set Carry Flag) instruction sets
the carry flag to 1.
Format: STC
Action: CF 1
The CMC Instruction
 The CMC (Complement Carry Flag)
instruction complements the carry flag.
 Format: CMC
 Action: CF CF’
Status & Flags Register
• Carry flag (CF): CF=1 if there is
– a carry out from most significant bit (msb) on addition
– a borrow into msb on subtraction
– CF also affected differently by shift and rotate instructions
• Parity flag (PF): PF=1 if
– low byte of result has an even number of one bits (even
parity)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
CFPFAFZFSFOF IFDF
Status & Flags Register - Cont.
• Auxiliary carry flag (AF): AF=1 if there is
– a carry out from bit 3 on addition
– a borrow into bit 3 on subtraction
• Zero flag (ZF): ZF=1
– if the result is zero
• Sign flag (SF): SF=1 if
– msb of result is 1 indicating that the result is negative
for signed number interpretation
• Overflow flag (OF): OF=1
– if signed overflow occurs
The PUSHF Instruction
 The PUSHF (Push Status Register onto Stack)
instruction pushes the flag register onto the stack.
PUSHF decrements SP by 2 and then moves the
flag register to the top of the stack.
 Format: PUSHF
 Action: SP SP - 2
SP [flag register]
The POPF Instruction
 The POPF (Pop Flags off the Stack)
instruction transfers the word pointed to by
SP to the status register, thereby altering
the value of the flags.
POPF

Format:
Action:
status register
SP
[SP]
SP + 2
Stack Instructions
• SP points at the the top of the stack
• .STACK 100H
– SP is initialized to 100H
• PUSH operand
– SP  SP - 2
– [SP+1:SP]  operand
• POP operand
– Operand  [SP+1:SP]
– SP  SP + 2
• PUSHF
– SP  SP - 2
– [SP+1:SP]  flags register
• POPF
– Flags register  [SP+1:SP]
– SP  SP + 2
The CLI Instruction
 The CLI (Clear Interrupt Flag) instruction
clears the interrupt flag, thereby disabling
interrupt request. The 8086/8088 will
therefore not recognize any external
interrupt request with the exception of the
non-maskable interrupt.
 Format: CLI
 Action: IF 0
The STI Instruction
 The STI (Set Interrupt Flag) instruction
sets the interrupt flag to 1, thereby enabling
the 8086/8088 to recognize any interrupt
request.
 Format: STI
 Action: IF 1
The CLD Instruction
 The CLD (Clear Direction Flag) instruction
clears the direction flag, thereby causing
the string instruction to auto-increment the
index registers (SI or DI).
 Format: CLD
 Action: DF 0
The STD Instruction
 The STD (Set Direction Flag) instruction
sets the direction flag to 1, thereby causing
the string instruction to auto-decrement the
index registers (SI or DI).
 Format: STD
 Action: DF 1
Flow Control Instruction
Flow Control Instructions
• Unconditional jump
– JMP label ; IP  label
• Conditional jump
– Signed jumps
– Unsigned jumps
– Common jumps
• Signed jumps
– JG/JNLE jump if greater than, or jump if not less than or equal
– JGE/JNL jump if greater than or equal, or jump if not less than
– JL/JNGE jump if less than, or jump if not greater than or equal
– JLE/JNG jump if less than or equal, or jump if not greater than
Flow Control Instructions
Flow Control Instructions
Flow Control Instructions
• Single-flag jumps
– JS jump if sign negative
– JNS jump if nonnegative sign
– JP/JPE jump if parity even
– JNP/JPO jump if parity odd
• Jump based on CX
– JCXZ if CX = 0, then jump to the address specified by
the operand
• Loop Instructions
– Loop short-label -loop decrement CX
– Loopnz/Loopne -loop while not zero/loop while not equal
– Loopz/Loope -loop while zero/loop while equal
• All jump instructions have no effect on the flags.
Flow Control Instructions
Branching Structures: IF-Then
• Example:
If AX < 0 Then
Replace AX by –AX
ENDIF
; if AX < 0
CMP AX, 0
JNL END_IF
;then
NEG AX
END_IF:
IF-Then-Else
• Example:
If AL <= BL Then
Display character in AL
Else
Display character in BL
ENDIF
MOV AH, 2
; if AL<=BL
CMP AL, BL
JNBE ELSE_
;then
MOV DL, AL
JMP DISPLAY
ELSE_:
MOV DL, BL
DISPLAY:
INT 21H
END_IF:
CASE
• Example:
CASE AX
<0: put –1 in BX
=0: put 0 in BX
>0: put 1 in BX
END_CASE
; case AX
CMP AX, 0
JL NEGATIVE
JE ZERO
JG POSITIVE
NEGATIVE: MOV BX, -1
JMP END_CASE
ZERO: MOV BX, 0
JMP END_CASE
POSITIVE: MOV BX, 1
END_CASE:
CASE – Cont.
• Example:
CASE AL
1,3: display ‘o’
2,4: display ‘e’
END_CASE
; case AL
CMP AL, 1 ; 1, 3:
JE ODD
CMP AL, 3
JE ODD
CMP AL, 2 ; 2, 4:
JE EVEN
CMP AL, 4
JE EVEN
JMP END_CASE
ODD: MOV DL, ‘o’
JMP DISPLAY
EVEN: MOV DL, ‘e’
DISPLAY: MOV AH, 2
INT 21H
END_CASE:
Branches with Compound
Conditions
• Example:
If (‘A’ <= character) and (character <= ‘Z’) Then
Display character
END_IF
; read a character
MOV AH, 1
INT 21H
; If (‘A’ <= character) and (character <= ‘Z’) Then
CMP AL, ‘A’
JNGE END_IF
CMP AL, ‘Z’
JNLE END_IF
; display character
MOV DL, AL
MOV AH, 2
INT 21H
END_IF:
Branches with Compound Conditions
• Example:
If (character=‘y’) OR (character <= ‘Y’) Then
Display character
Else terminate program
END_IF
; read a character
MOV AH, 1
INT 21H
; If (character=‘y’) OR (character = ‘Y’) Then
CMP AL, ‘y’
JE Then
CMP AL, ‘Y’
JE Then
JMP ELSE_
Then: MOV AH, 2
MOV DL, AL
INT 21H
JMP END_IF
ELSE:MOV AH, 4CH
INT 21H
END_IF:
Loop Instructions
• Loop Next
– Dec Cx
– If CX<>0 JMP Next
• Loopz/loope Next
– Dec Cx
– If (CX<>0) AND (ZF=1) JMP Next
• Loopnz/loopne Next
– Dec Cx
– If (CX<>0) AND (ZF=0) JMP Next
FOR LOOP
• Example:
For 80 times DO
Display ‘*’
END_IF
MOV CX, 80
MOV AH, 2
MOV DL, ‘*’
Next: INT 21H
Loop Next
While Loop
• Example:
Initialize count to 0
Read a character
While character <> Carriage Return DO
Count = Count + 1
Read a character
END_While
MOV DX, 0
MOV AH, 1
INT 21H
While_: CMP AL, 0DH
JE End_While
INC DX
INT 21H
JMP While_
End_While:
Repeat Loop
• Example:
Repeat
Read a character
Until character is blank
MOV AH, 1
Repeat:
INT 21H
; until
CMP AL, ‘ ‘
JNE Repeat
Application of Loope
• Example: Search for a number in a Table
Table DB 1,2,3,4,5,6,7,8,9
XOR SI, SI
MOV CX, 9
Next: INC SI
CMP Table[SI-1], 7
Loopne Next

More Related Content

What's hot

Complex Programmable Logic Devices(CPLD) & Field Programmable Logic Devices (...
Complex Programmable Logic Devices(CPLD) & Field Programmable Logic Devices (...Complex Programmable Logic Devices(CPLD) & Field Programmable Logic Devices (...
Complex Programmable Logic Devices(CPLD) & Field Programmable Logic Devices (...Revathi Subramaniam
 
I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)Varun Mahajan
 
Synchronous counters
Synchronous countersSynchronous counters
Synchronous countersLee Diaz
 
Serial Peripheral Interface
Serial Peripheral InterfaceSerial Peripheral Interface
Serial Peripheral InterfaceAnurag Tomar
 
SPI introduction(Serial Peripheral Interface)
SPI introduction(Serial Peripheral Interface)SPI introduction(Serial Peripheral Interface)
SPI introduction(Serial Peripheral Interface)SUNODH GARLAPATI
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4Motaz Saad
 
Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor  Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor Mustapha Fatty
 
Dot Matrix LED Interfacing using 8255 PPI
Dot Matrix LED Interfacing using 8255 PPIDot Matrix LED Interfacing using 8255 PPI
Dot Matrix LED Interfacing using 8255 PPIAmit Kumer Podder
 
Microprocessor 8086 instructions
Microprocessor 8086 instructionsMicroprocessor 8086 instructions
Microprocessor 8086 instructionsRavi Anand
 
Addressing modes of 8086
Addressing modes of 8086Addressing modes of 8086
Addressing modes of 8086Dr. AISHWARYA N
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Bilal Amjad
 
Verilog Test Bench
Verilog Test BenchVerilog Test Bench
Verilog Test BenchDr.YNM
 
Lecture #3 Flag Register.pptx
Lecture #3 Flag Register.pptxLecture #3 Flag Register.pptx
Lecture #3 Flag Register.pptxImranBhatti58
 

What's hot (20)

Complex Programmable Logic Devices(CPLD) & Field Programmable Logic Devices (...
Complex Programmable Logic Devices(CPLD) & Field Programmable Logic Devices (...Complex Programmable Logic Devices(CPLD) & Field Programmable Logic Devices (...
Complex Programmable Logic Devices(CPLD) & Field Programmable Logic Devices (...
 
I2C
I2CI2C
I2C
 
8086
80868086
8086
 
I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)I2C Bus (Inter-Integrated Circuit)
I2C Bus (Inter-Integrated Circuit)
 
Synchronous counters
Synchronous countersSynchronous counters
Synchronous counters
 
Instruction formats-in-8086
Instruction formats-in-8086Instruction formats-in-8086
Instruction formats-in-8086
 
Serial Peripheral Interface
Serial Peripheral InterfaceSerial Peripheral Interface
Serial Peripheral Interface
 
8086 String Instructions.pdf
8086 String Instructions.pdf8086 String Instructions.pdf
8086 String Instructions.pdf
 
I o ports.ppt
I o ports.pptI o ports.ppt
I o ports.ppt
 
I2C Protocol
I2C ProtocolI2C Protocol
I2C Protocol
 
SPI introduction(Serial Peripheral Interface)
SPI introduction(Serial Peripheral Interface)SPI introduction(Serial Peripheral Interface)
SPI introduction(Serial Peripheral Interface)
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4
 
Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor  Architecture of 8086 Microprocessor
Architecture of 8086 Microprocessor
 
Dot Matrix LED Interfacing using 8255 PPI
Dot Matrix LED Interfacing using 8255 PPIDot Matrix LED Interfacing using 8255 PPI
Dot Matrix LED Interfacing using 8255 PPI
 
Microprocessor 8086 instructions
Microprocessor 8086 instructionsMicroprocessor 8086 instructions
Microprocessor 8086 instructions
 
Addressing modes of 8086
Addressing modes of 8086Addressing modes of 8086
Addressing modes of 8086
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
 
Synchronous sequential Circuits
Synchronous sequential CircuitsSynchronous sequential Circuits
Synchronous sequential Circuits
 
Verilog Test Bench
Verilog Test BenchVerilog Test Bench
Verilog Test Bench
 
Lecture #3 Flag Register.pptx
Lecture #3 Flag Register.pptxLecture #3 Flag Register.pptx
Lecture #3 Flag Register.pptx
 

Similar to Flag control

Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...Bilal Amjad
 
Instructionsetof8086 by Alwani
Instructionsetof8086 by AlwaniInstructionsetof8086 by Alwani
Instructionsetof8086 by AlwaniHimanshu Alwani
 
Jumps in Assembly Language.
Jumps in Assembly Language.Jumps in Assembly Language.
Jumps in Assembly Language.NA000000
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamDr. Girish GS
 
Arithmetic and logical instructions set
Arithmetic and logical instructions setArithmetic and logical instructions set
Arithmetic and logical instructions setRobert Almazan
 
instruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.pptinstruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.pptssuser2b759d
 
Microprocessor:stack,shifting,looping
Microprocessor:stack,shifting,loopingMicroprocessor:stack,shifting,looping
Microprocessor:stack,shifting,loopingSamiul Ehsan
 
Intel codetable
Intel codetableIntel codetable
Intel codetableJ R7
 
Flags
FlagsFlags
FlagsCME
 
unit1.pdf
unit1.pdfunit1.pdf
unit1.pdfSaruM1
 
Microprocessor.pptx
Microprocessor.pptxMicroprocessor.pptx
Microprocessor.pptxNishatNishu5
 
Chap3 program flow control instructions
Chap3 program flow control instructionsChap3 program flow control instructions
Chap3 program flow control instructionsHarshitParkar6677
 
Chapter3 program flow control instructions
Chapter3 program flow control instructionsChapter3 program flow control instructions
Chapter3 program flow control instructionsHarshitParkar6677
 

Similar to Flag control (20)

Assignment on alp
Assignment on alpAssignment on alp
Assignment on alp
 
Assignment on alp
Assignment on alpAssignment on alp
Assignment on alp
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
 
Al2ed chapter8
Al2ed chapter8Al2ed chapter8
Al2ed chapter8
 
Instructionsetof8086 by Alwani
Instructionsetof8086 by AlwaniInstructionsetof8086 by Alwani
Instructionsetof8086 by Alwani
 
Jumps in Assembly Language.
Jumps in Assembly Language.Jumps in Assembly Language.
Jumps in Assembly Language.
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progam
 
Arithmetic and logical instructions set
Arithmetic and logical instructions setArithmetic and logical instructions set
Arithmetic and logical instructions set
 
instruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.pptinstruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.ppt
 
assembly flag resister
assembly flag resisterassembly flag resister
assembly flag resister
 
Expt 5 matrix.docx
Expt 5 matrix.docxExpt 5 matrix.docx
Expt 5 matrix.docx
 
Microprocessor:stack,shifting,looping
Microprocessor:stack,shifting,loopingMicroprocessor:stack,shifting,looping
Microprocessor:stack,shifting,looping
 
Intel codetable
Intel codetableIntel codetable
Intel codetable
 
Flags
FlagsFlags
Flags
 
Intel 8086
Intel 8086Intel 8086
Intel 8086
 
unit1.pdf
unit1.pdfunit1.pdf
unit1.pdf
 
Microprocessor.pptx
Microprocessor.pptxMicroprocessor.pptx
Microprocessor.pptx
 
Chapter2c
Chapter2cChapter2c
Chapter2c
 
Chap3 program flow control instructions
Chap3 program flow control instructionsChap3 program flow control instructions
Chap3 program flow control instructions
 
Chapter3 program flow control instructions
Chapter3 program flow control instructionsChapter3 program flow control instructions
Chapter3 program flow control instructions
 

More from Robert Almazan

Distruct week 15 graphs theory (updated)
Distruct week 15 graphs theory (updated)Distruct week 15 graphs theory (updated)
Distruct week 15 graphs theory (updated)Robert Almazan
 
Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructionsRobert Almazan
 
Week 5 lan topology part 2
Week 5 lan topology part 2Week 5 lan topology part 2
Week 5 lan topology part 2Robert Almazan
 
Week 4 introducing network standards
Week 4 introducing network standardsWeek 4 introducing network standards
Week 4 introducing network standardsRobert Almazan
 
Week 3 basic network media
Week 3 basic network mediaWeek 3 basic network media
Week 3 basic network mediaRobert Almazan
 
Week 2 network configurartion
Week 2 network configurartionWeek 2 network configurartion
Week 2 network configurartionRobert Almazan
 
introduction to networking
introduction to networkingintroduction to networking
introduction to networkingRobert Almazan
 

More from Robert Almazan (8)

Distruct week 15 graphs theory (updated)
Distruct week 15 graphs theory (updated)Distruct week 15 graphs theory (updated)
Distruct week 15 graphs theory (updated)
 
Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructions
 
Week 5 lan topology
Week 5 lan topologyWeek 5 lan topology
Week 5 lan topology
 
Week 5 lan topology part 2
Week 5 lan topology part 2Week 5 lan topology part 2
Week 5 lan topology part 2
 
Week 4 introducing network standards
Week 4 introducing network standardsWeek 4 introducing network standards
Week 4 introducing network standards
 
Week 3 basic network media
Week 3 basic network mediaWeek 3 basic network media
Week 3 basic network media
 
Week 2 network configurartion
Week 2 network configurartionWeek 2 network configurartion
Week 2 network configurartion
 
introduction to networking
introduction to networkingintroduction to networking
introduction to networking
 

Recently uploaded

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 

Recently uploaded (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 

Flag control

  • 1. Flag Control Objective: • Identify the different flag control instruction • Apply the different flag control instructions
  • 2. Flag Control Instructions The 8086/8088 provides a group of instructions that directly affects the state of the flags. • LAHF Instruction • SAHF Instruction • CLC Instruction • STC Instruction • CMC Instruction • PUSHF Instruction • POPF Instruction
  • 3. The LAHF Instruction  The LAHF (Load AH from flags) instruction copies the SF, ZF, AF, PF, and CF into bits b7, b6, b4, b2, and b0 respectively of register AH. Format: Action: AH LAHF SF x ZF x AF x PF x CF
  • 4. The SAHF Instruction The SAHF (Store AH into flags) instruction transfers bits b7, b6, b4, b2, and b0 from register AH into SF, ZF, AF, PF, and CF, respectively. SAHFFormat: Action: SF ZF x AF x PF x CF AH Example: MOV AH, 25H SAHF
  • 5. The CLC Instruction  The CLC (Clear Carry Flag) instruction clears the carry flag.  Format: CLC  Action: CF 0
  • 6. The STC Instruction The STC (Set Carry Flag) instruction sets the carry flag to 1. Format: STC Action: CF 1
  • 7. The CMC Instruction  The CMC (Complement Carry Flag) instruction complements the carry flag.  Format: CMC  Action: CF CF’
  • 8. Status & Flags Register • Carry flag (CF): CF=1 if there is – a carry out from most significant bit (msb) on addition – a borrow into msb on subtraction – CF also affected differently by shift and rotate instructions • Parity flag (PF): PF=1 if – low byte of result has an even number of one bits (even parity) 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 CFPFAFZFSFOF IFDF
  • 9. Status & Flags Register - Cont. • Auxiliary carry flag (AF): AF=1 if there is – a carry out from bit 3 on addition – a borrow into bit 3 on subtraction • Zero flag (ZF): ZF=1 – if the result is zero • Sign flag (SF): SF=1 if – msb of result is 1 indicating that the result is negative for signed number interpretation • Overflow flag (OF): OF=1 – if signed overflow occurs
  • 10. The PUSHF Instruction  The PUSHF (Push Status Register onto Stack) instruction pushes the flag register onto the stack. PUSHF decrements SP by 2 and then moves the flag register to the top of the stack.  Format: PUSHF  Action: SP SP - 2 SP [flag register]
  • 11. The POPF Instruction  The POPF (Pop Flags off the Stack) instruction transfers the word pointed to by SP to the status register, thereby altering the value of the flags. POPF  Format: Action: status register SP [SP] SP + 2
  • 12. Stack Instructions • SP points at the the top of the stack • .STACK 100H – SP is initialized to 100H • PUSH operand – SP  SP - 2 – [SP+1:SP]  operand • POP operand – Operand  [SP+1:SP] – SP  SP + 2 • PUSHF – SP  SP - 2 – [SP+1:SP]  flags register • POPF – Flags register  [SP+1:SP] – SP  SP + 2
  • 13. The CLI Instruction  The CLI (Clear Interrupt Flag) instruction clears the interrupt flag, thereby disabling interrupt request. The 8086/8088 will therefore not recognize any external interrupt request with the exception of the non-maskable interrupt.  Format: CLI  Action: IF 0
  • 14. The STI Instruction  The STI (Set Interrupt Flag) instruction sets the interrupt flag to 1, thereby enabling the 8086/8088 to recognize any interrupt request.  Format: STI  Action: IF 1
  • 15. The CLD Instruction  The CLD (Clear Direction Flag) instruction clears the direction flag, thereby causing the string instruction to auto-increment the index registers (SI or DI).  Format: CLD  Action: DF 0
  • 16. The STD Instruction  The STD (Set Direction Flag) instruction sets the direction flag to 1, thereby causing the string instruction to auto-decrement the index registers (SI or DI).  Format: STD  Action: DF 1
  • 18. Flow Control Instructions • Unconditional jump – JMP label ; IP  label • Conditional jump – Signed jumps – Unsigned jumps – Common jumps • Signed jumps – JG/JNLE jump if greater than, or jump if not less than or equal – JGE/JNL jump if greater than or equal, or jump if not less than – JL/JNGE jump if less than, or jump if not greater than or equal – JLE/JNG jump if less than or equal, or jump if not greater than
  • 21. Flow Control Instructions • Single-flag jumps – JS jump if sign negative – JNS jump if nonnegative sign – JP/JPE jump if parity even – JNP/JPO jump if parity odd • Jump based on CX – JCXZ if CX = 0, then jump to the address specified by the operand • Loop Instructions – Loop short-label -loop decrement CX – Loopnz/Loopne -loop while not zero/loop while not equal – Loopz/Loope -loop while zero/loop while equal • All jump instructions have no effect on the flags.
  • 23. Branching Structures: IF-Then • Example: If AX < 0 Then Replace AX by –AX ENDIF ; if AX < 0 CMP AX, 0 JNL END_IF ;then NEG AX END_IF:
  • 24. IF-Then-Else • Example: If AL <= BL Then Display character in AL Else Display character in BL ENDIF MOV AH, 2 ; if AL<=BL CMP AL, BL JNBE ELSE_ ;then MOV DL, AL JMP DISPLAY ELSE_: MOV DL, BL DISPLAY: INT 21H END_IF:
  • 25. CASE • Example: CASE AX <0: put –1 in BX =0: put 0 in BX >0: put 1 in BX END_CASE ; case AX CMP AX, 0 JL NEGATIVE JE ZERO JG POSITIVE NEGATIVE: MOV BX, -1 JMP END_CASE ZERO: MOV BX, 0 JMP END_CASE POSITIVE: MOV BX, 1 END_CASE:
  • 26. CASE – Cont. • Example: CASE AL 1,3: display ‘o’ 2,4: display ‘e’ END_CASE ; case AL CMP AL, 1 ; 1, 3: JE ODD CMP AL, 3 JE ODD CMP AL, 2 ; 2, 4: JE EVEN CMP AL, 4 JE EVEN JMP END_CASE ODD: MOV DL, ‘o’ JMP DISPLAY EVEN: MOV DL, ‘e’ DISPLAY: MOV AH, 2 INT 21H END_CASE:
  • 27. Branches with Compound Conditions • Example: If (‘A’ <= character) and (character <= ‘Z’) Then Display character END_IF ; read a character MOV AH, 1 INT 21H ; If (‘A’ <= character) and (character <= ‘Z’) Then CMP AL, ‘A’ JNGE END_IF CMP AL, ‘Z’ JNLE END_IF ; display character MOV DL, AL MOV AH, 2 INT 21H END_IF:
  • 28. Branches with Compound Conditions • Example: If (character=‘y’) OR (character <= ‘Y’) Then Display character Else terminate program END_IF ; read a character MOV AH, 1 INT 21H ; If (character=‘y’) OR (character = ‘Y’) Then CMP AL, ‘y’ JE Then CMP AL, ‘Y’ JE Then JMP ELSE_ Then: MOV AH, 2 MOV DL, AL INT 21H JMP END_IF ELSE:MOV AH, 4CH INT 21H END_IF:
  • 29. Loop Instructions • Loop Next – Dec Cx – If CX<>0 JMP Next • Loopz/loope Next – Dec Cx – If (CX<>0) AND (ZF=1) JMP Next • Loopnz/loopne Next – Dec Cx – If (CX<>0) AND (ZF=0) JMP Next
  • 30. FOR LOOP • Example: For 80 times DO Display ‘*’ END_IF MOV CX, 80 MOV AH, 2 MOV DL, ‘*’ Next: INT 21H Loop Next
  • 31. While Loop • Example: Initialize count to 0 Read a character While character <> Carriage Return DO Count = Count + 1 Read a character END_While MOV DX, 0 MOV AH, 1 INT 21H While_: CMP AL, 0DH JE End_While INC DX INT 21H JMP While_ End_While:
  • 32. Repeat Loop • Example: Repeat Read a character Until character is blank MOV AH, 1 Repeat: INT 21H ; until CMP AL, ‘ ‘ JNE Repeat
  • 33. Application of Loope • Example: Search for a number in a Table Table DB 1,2,3,4,5,6,7,8,9 XOR SI, SI MOV CX, 9 Next: INC SI CMP Table[SI-1], 7 Loopne Next