Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flags Register & Jump Instruction

Similar presentations


Presentation on theme: "Flags Register & Jump Instruction"— Presentation transcript:

1 Flags Register & Jump Instruction
Lecture 3 Flags Register & Jump Instruction

2 Outline The FLAGS Register Overflow How instruction affect the flags
Jump Instruction

3 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.

4 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).

5 The FLAGS Register 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 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)

6 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).

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

8 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.

9 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.

10 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.

11 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

12 How Instructions Affect the Flags
Example: ADD AX, BX where AX contains FFFFh, BX contains FFFFh FFFFh ( ) +FFFFh ( ) 1 FFFEh = b 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.

13 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.

14 How Instructions Affect the Flags
Example: SUB AX, BX where AX contains 8000h, BX contains 0001h 8000h - 0001h 7FFFh = b 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.

15 How Instructions Affect the Flags
Example: MOV AX, -5 None of the flags are affected by MOV.

16 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/FAR address JMP

17 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

18 The JMP Instruction - Unconditional Jumps
Backward and Forward jumps Backward: L10: ……. JMP L10 Forward:

19 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.

20 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.

21 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

22 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

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

24 Examples In the following example, the JL instruction jumps to label L1 because AX is less than 6: mov ax,5 cmp ax,6 jl L1 ; jump if less In the following example, the jump is taken because AX is greater than 4: mov ax,5 cmp ax,4 jg L1 ; jump if greater


Download ppt "Flags Register & Jump Instruction"

Similar presentations


Ads by Google