Download - Flag control
![Page 1: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/1.jpg)
Flag Control
Objective:• Identify the different flag control instruction• Apply the different flag control instructions
![Page 2: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/2.jpg)
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
![Page 3: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/3.jpg)
The LAHF Instruction
The LAHF (Load AH from flags)instruction copies the SF, ZF, AF, PF, andCF into bits b7, b6, b4, b2, and b0respectively of register AH.
Format:
Action:
AH
LAHF
SF x ZF x AF x PF x CF
![Page 4: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/4.jpg)
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, 25HSAHF
![Page 5: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/5.jpg)
The CLC Instruction
The CLC (Clear Carry Flag) instructionclears the carry flag.
Format: CLC Action: CF 0
![Page 6: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/6.jpg)
The STC Instruction
The STC (Set Carry Flag) instruction setsthe carry flag to 1.
Format: STCAction: CF 1
![Page 7: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/7.jpg)
The CMC Instruction
The CMC (Complement Carry Flag)instruction complements the carry flag.
Format: CMC Action: CF CF’
![Page 8: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/8.jpg)
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
![Page 9: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/9.jpg)
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
![Page 10: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/10.jpg)
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]
![Page 11: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/11.jpg)
The POPF Instruction
The POPF (Pop Flags off the Stack)instruction transfers the word pointed to bySP to the status register, thereby alteringthe value of the flags.
POPF
Format:
Action:
status register
SP
[SP]
SP + 2
![Page 12: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/12.jpg)
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
![Page 13: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/13.jpg)
The CLI Instruction
The CLI (Clear Interrupt Flag) instructionclears the interrupt flag, thereby disablinginterrupt request. The 8086/8088 willtherefore not recognize any externalinterrupt request with the exception of thenon-maskable interrupt.
Format: CLI Action: IF 0
![Page 14: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/14.jpg)
The STI Instruction
The STI (Set Interrupt Flag) instructionsets the interrupt flag to 1, thereby enablingthe 8086/8088 to recognize any interruptrequest.
Format: STI Action: IF 1
![Page 15: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/15.jpg)
The CLD Instruction
The CLD (Clear Direction Flag) instructionclears the direction flag, thereby causingthe string instruction to auto-increment theindex registers (SI or DI).
Format: CLD Action: DF 0
![Page 16: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/16.jpg)
The STD Instruction
The STD (Set Direction Flag) instructionsets the direction flag to 1, thereby causingthe string instruction to auto-decrement theindex registers (SI or DI).
Format: STD Action: DF 1
![Page 17: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/17.jpg)
Flow Control Instruction
![Page 18: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/18.jpg)
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
![Page 19: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/19.jpg)
Flow Control Instructions
![Page 20: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/20.jpg)
Flow Control Instructions
![Page 21: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/21.jpg)
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.
![Page 22: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/22.jpg)
Flow Control Instructions
![Page 23: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/23.jpg)
Branching Structures: IF-Then
• Example:If AX < 0 Then
Replace AX by –AX
ENDIF
; if AX < 0CMP AX, 0JNL END_IF
;thenNEG AX
END_IF:
![Page 24: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/24.jpg)
IF-Then-Else• Example:
If AL <= BL ThenDisplay character in AL
Else Display character in BL
ENDIF
MOV AH, 2; if AL<=BL
CMP AL, BLJNBE ELSE_
;thenMOV DL, ALJMP DISPLAY
ELSE_:MOV DL, BL
DISPLAY:INT 21H
END_IF:
![Page 25: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/25.jpg)
CASE• Example:
CASE AX<0: put –1 in BX=0: put 0 in BX>0: put 1 in BX
END_CASE
; case AXCMP AX, 0JL NEGATIVEJE ZEROJG POSITIVE
NEGATIVE: MOV BX, -1JMP END_CASE
ZERO: MOV BX, 0JMP END_CASE
POSITIVE: MOV BX, 1END_CASE:
![Page 26: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/26.jpg)
CASE – Cont.• Example:
CASE AL1,3: display ‘o’2,4: display ‘e’
END_CASE; case AL
CMP AL, 1 ; 1, 3:JE ODDCMP AL, 3JE ODDCMP AL, 2 ; 2, 4:JE EVENCMP AL, 4JE EVENJMP END_CASE
ODD: MOV DL, ‘o’JMP DISPLAY
EVEN: MOV DL, ‘e’DISPLAY: MOV AH, 2
INT 21HEND_CASE:
![Page 27: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/27.jpg)
Branches with Compound Conditions
• Example:If (‘A’ <= character) and (character <= ‘Z’) Then
Display characterEND_IF
; read a characterMOV AH, 1INT 21H
; If (‘A’ <= character) and (character <= ‘Z’) Then CMP AL, ‘A’JNGE END_IFCMP AL, ‘Z’JNLE END_IF
; display characterMOV DL, ALMOV AH, 2INT 21H
END_IF:
![Page 28: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/28.jpg)
Branches with Compound Conditions
• Example:If (character=‘y’) OR (character <= ‘Y’) Then
Display character
Else terminate programEND_IF; read a character
MOV AH, 1INT 21H
; If (character=‘y’) OR (character = ‘Y’) Then CMP AL, ‘y’JE ThenCMP AL, ‘Y’JE ThenJMP ELSE_
Then: MOV AH, 2MOV DL, ALINT 21HJMP END_IF
ELSE: MOV AH, 4CHINT 21H
END_IF:
![Page 29: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/29.jpg)
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
![Page 30: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/30.jpg)
FOR LOOP
• Example:For 80 times DO
Display ‘*’
END_IFMOV CX, 80MOV AH, 2MOV DL, ‘*’
Next: INT 21HLoop Next
![Page 31: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/31.jpg)
While Loop• Example:
Initialize count to 0Read a characterWhile character <> Carriage Return DO
Count = Count + 1Read a character
END_While
MOV DX, 0MOV AH, 1INT 21H
While_: CMP AL, 0DHJE End_WhileINC DXINT 21HJMP While_
End_While:
![Page 32: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/32.jpg)
Repeat Loop
• Example:Repeat
Read a characterUntil character is blank
MOV AH, 1Repeat:
INT 21H; until
CMP AL, ‘ ‘JNE Repeat
![Page 33: Flag control](https://reader031.vdocument.in/reader031/viewer/2022032012/55c3f6c6bb61eb3a438b4646/html5/thumbnails/33.jpg)
Application of Loope
• Example: Search for a number in a Table
Table DB 1,2,3,4,5,6,7,8,9
XOR SI, SIMOV CX, 9
Next: INC SICMP Table[SI-1], 7Loopne Next