flag control

Post on 07-Aug-2015

53 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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, 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

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

The CLC Instruction

The CLC (Clear Carry Flag) instructionclears the carry flag.

Format: CLC Action: CF 0

The STC Instruction

The STC (Set Carry Flag) instruction setsthe carry flag to 1.

Format: STCAction: 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 bySP to the status register, thereby alteringthe 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) 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

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

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

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

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 < 0CMP AX, 0JNL END_IF

;thenNEG AX

END_IF:

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:

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:

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:

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:

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:

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_IFMOV CX, 80MOV AH, 2MOV DL, ‘*’

Next: INT 21HLoop Next

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:

Repeat Loop

• Example:Repeat

Read a characterUntil character is blank

MOV AH, 1Repeat:

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, SIMOV CX, 9

Next: INC SICMP Table[SI-1], 7Loopne Next

top related