260 likes | 277 Views
Lecture 3 Flags Register & Jump Instruction. Outline The FLAGS Register Overflow How instruction affect the flags Jump Instruction. The FLAGS Register - Introduction. One important feature that distinguishes a computer from other
E N D
Lecture 3 Flags Register & Jump Instruction
Outline • The FLAGS Register • Overflow • How instruction affect the flags • Jump Instruction
The FLAGS Register - Introduction • One important feature that distinguishes a computer from other • machines is the computer’s ability to make decisions. • Decision are made based on the current state of the processor. • In the 8086 processor, the processor state is implemented as nine • individual bits called flags. • Each decision made by the 8086 is based on the values of these • flags. • The flags are placed in the FLAGS register.
The FLAGS Register • Two kinds of flags: • Status Flags: reflect the result of an instruction executed by the • processor (ex. ZF ,PF ,SF ,OF) • Control Flags: enable or disable certain operations of the • processor (ex. If the IF “Interrupt Flag” is set to 0, • inputs from the keyboard are ignored by the • processor).
The FLAGS Register 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 OF DF IF TF SF ZF AF PF CF • Status Flags: (0,2,4,6,7, and 11) • Carry Flag (CF) • Parity Flag (PF) • Auxiliary carry Flag (AF) • Zero Flag (ZF) • Sign Flag (SF) • Overflow Flag (OF) • Control Flags: (8,9,10) • Trap Flag (TF) • Interrupt Flag (IF) • Direction Flag (DF)
The FLAGS Register - Status Flags • Carry Flag (CF): • CF = 1 if there is a carry out from the msb on addition. • CF = 1 if there is a borrow into the msb on subtraction. • CF = 0 otherwise. • CF is also affected by shift and rotate instructions. • Parity Flag (PF): • PF = 1 if the low byte of a result has an even number of one bits • PF = 0 if the low byte has odd parity • (ex. if a result is FFFE PF = 0). • 1111 1111 1111 1101
The FLAGS Register - Status Flags • Auxiliary carry Flag (AF): • AF = 1 if there is a carry out from bit 3 on addition. • AF = 1 if there is a borrow into bit 3 on subtraction. • AF = 0 otherwise. • AF is used in BCD (binary coded decimal)operations. • Zero Flag (ZF): • ZF = 1 for a zero result. • ZF = 0 for nonzero result.
The FLAGS Register - Status Flags • Sign Flag (SF): • SF = 1 if the msb of a result is 1. • (i.e. Negative if signed interpretation is used). • SF = 0 if the msb is 0. • Overflow Flag (OF): • OF = 1 if signed overflow occurred. • OF = 0 otherwise.
Overflow • The phenomenon of overflow is associated with the fact that the range of numbers that can be represented in a computer is limited. • If the result of an operation falls outside the range, overflow occurs and the truncated result that is saved will be incorrect.
How the Processor Determines that Overflow Occurred? Signed Overflow • On addition of numbers with the same sign, signed overflow occurs when the sum has a different sign. • Subtraction of numbers with different signs is like adding numbers • of the same sign (ex. A - (-B) = A + B). Signed overflow occurs if • the result has a different sign than expected. • In addition of numbers with different signs, overflow is impossible. • Subtraction of numbers with the same sign cannot give overflow.
How Instructions Affect the Flags • In general, each time the processor executes an instruction, the flags are altered to reflect the result. • However, some instructions don’t affect any of the flags, affects • only some of them, or may leave them undefined. Instruction Affects flags MOV/XCHG none ADD/SUB all INC/DEC all except CF
How Instructions Affect the Flags Example: ADD AX, BX where AX contains FFFFh, BX contains FFFFh FFFFh (1111 1111 1111 1111) +FFFFh (1111 1111 1111 1111) 1 FFFEh = 1111 1111 1111 1110b SF = 1 because the msb is 1. PF = 0 because there are 7 (odd number) of 1 bits in the low byte. ZF = 0 because the result is nonzero. 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.
How Instructions Affect the Flags Example: ADD AL, BL where AL contains 80h, BL contains 80h 80h +80h 100h SF = 0 because the msb is 0. PF = 1 because all the bits in the result are zero. ZF = 1 because the result is 0. CF = 1 because there is a carry out of the msb on addition. OF = 1 because the numbers being added are both negative, but the result is 0.
How Instructions Affect the Flags Example: SUB AX, BX where AX contains 8000h, BX contains 0001h 8000h - 0001h 7FFFh = 0111 1111 1111 1111b SF = 0 because the msb is 0. PF = 1 because there are 8 (even number) one bits in the low byte. ZF = 0 because the result is nonzero. CF = 0 because a smaller unsigned number is being subtracted from a larger one. OF = 1 because a positive number is being subtracted from a negative one (like adding two negatives), and the result is positive.
How Instructions Affect the Flags Example: MOV AX, -5 None of the flags are affected by MOV.
The JMP Instruction - Unconditional Jumps • The JMP (jump) instruction causes an unconditional transfer of control (unconditional jump). • Syntax: JMP destination • Example JMP L10 …….. L10: INC CX
Short,near,and far addresses 1- A short address, limited to a distance of -128 to 127 bytes 2- A near address, limited to a distance of -32,768 to 32,767 bytes 3- A far address, which may be within the same segment at a distance over 32K or in other segment
The JMP Instruction - Unconditional Jumps • Backward and Forward jumps Backward: L10: ……. JMP L10 Forward: JMP L10 ……. L10:
The CMP Instruction • The jump condition is often provided by the CMP (compare) instruction • Syntax: CMP destination, source • Compares by computing destination contents minus source contents. • The result is not stored, but the flags are affected. • Destination may not be a constant. • CMP is just like SUB, except that destination is not changed.
Conditional Jumps • Syntax Jxxx destination_label • Example JNZ PRINT_LOOP • If the condition for the jump is true, the next instruction to be executed is the one at destinaltion_label (PRINT_LOOP), which may precede or follow the jump instruction itself. • If the condition is false, the instruction immediately following the jump is done next. • For JNZ, the condition is that the result of the previous operation is not zero.
Conditional Jumps • Signed Jumps: used for signed interpretations. Symbol Description Condition for Jumps JG/JNLE jump if grater than ZF = 0 & SF = OF jump if not less than or equal JGE/JNL jump if grater than or equal SF = OF jump if not less than JL/JNGE jump if less than SF <> OF jump if not greater than or equal JLE/JNG jump if less than or equal ZF = 1 or SF <> OF jump if not grater than
Conditional Jumps • Unsigned Jumps: used for unsigned interpretations. Symbol Description Condition for Jumps JA/JNBE jump if above CF = 0 & ZF = 0 jump if not below or equal JAE/JNB jump if above or equal CF = 0 jump if not below JB/JNAE jump if below CF = 1 jump if not above or equal JBE/JNA jump if below or equal CF = 1 or ZF = 1 jump if not above
Conditional Jumps • Single Flag Jumps: operates on settings of individual flags. Symbol Description Condition for Jumps JE/JZ jump if equal/ jump if equal to 0 ZF = 1 JNE/JNZ jump if not equal/ jump if not 0 ZF = 0 JC jump if carry CF = 1 JNC jump if no carry CF = 0 JO jump if overflow OF = 1 JNO jump if no overflow OF = 0 JS jump if sign negative SF = 1 JNS jump if nonnegative sign SF = 0 JP/JPE jump if parity even PF = 1 JNP/JPO jump if parity odd PF = 0
Examples In the following example, the JL instruction jumps to label L1 because AX is less than 6: mov ax,5cmp ax,6jl L1 ; jump if less In the following example, the jump is taken because AX is greater than 4: mov ax,5cmp ax,4jg L1 ; jump if greater