Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on logical instruction and

Similar presentations


Presentation on theme: "More on logical instruction and"— Presentation transcript:

1 More on logical instruction and
conditional jumps

2 parity flag The parity flag indicates whether the lowest byte of the result of a bitwise arithmetic or logical operation has an even or odd number of 1 bits. The flag is set when the parity is even and it is clear when the parity is odd. One way to check the parity of a number without changing its value is to XOR the number with all Zeros: Mov al, b ; 5 bits = odd parity Xor al, 0 ; parity flag clear (PO) Mov al, b ; 4 bits = even parity Xor al, 0 ; parity flag set (PE)

3 NOT instruction The NOT instruction toggles all bits in an operand. The result is called 1’s complement. The following operand types are permitted NOT reg NOT mem For example, the 1’s complement of F0h is 0Fh Mov al, b NOT al ; AL = b Flag: No flags are affected by NOT instruction

4 TEST instruction The TEST instruction performs an implied AND operation between each pair of matching bits in 2 operands and set the flag accordingly. The difference between TEST and AND operation is that TEST does not modify the destination operand. The TEST instruction permits the same operand combinations as the AND instructions. TEST particularly is valuable for finding out if individual bits in an operand is set. Flags : The TEST instruction always clears the overflow and carry flag. It modifies the sign, zero, and parity flag in the same way as the AND instruction

5 Example Testing multiple bits The test instruction can check several bits at once. Suppose we want to know if either bit 0 or bit 3 is set in AL register We can use the following instruction to find this out TEST al, b ; test bits 0 and 3 From the following example, data set we can infer that the zero flag is set only when all tested bits are clear input value test value result value input value result value

6 CMP Instruction The CMP instruction performs an implied subtraction of a source operand from a destination operand. Neither operand is modified: CMP destination, source CMP uses the same operand combinations as the AND instruction. Flags: the CMP instruction changes the overflow, sign , zero, carry, auxiliary carry, and parity flags according to the destination operand would have had if the Sub instruction were used. For example, as shown, below two operands are compared, the zero and carry flags indicate the relation between operands: CMP Results ZF CF destination < source 1 destination > source destination = source

7 If the two operands being compared are assumed to be signed, the sign, Zero, and overflow flags indicate the following relations between operands: CMP is voluble because it provides the basic for most conditional logic structures. When you follow a CMP with conditional jump instruction, the result is the assembly language equivalent of an IF statement. ZF = 1 destination = source SF = OF destination > source destination < source Flags CMP Results

8 Examples: Lets look at three code fragments that show how the flags are affected by the CMP instruction. When we put 5 in AX and compare it to 10, the carry flag is set because subtracting 10 from 5 requires a borrow: Mov ax,5 Cmp ax,10 ; CF = 1 Comparing 1000 to 1000 sets the zero flag because subtracting the source from the destination produces zero: Mov ax, 1000 Mov cx, 1000 Cmp cx,ax ;ZF = 1 Comparing 105 to 0 clears both the zero and carry flags because 105 is greater than 0: Mov si,105 Cmp si,0 ZF = 0 and CF = 0

9 Conditional Jumps Two steps involve in any conditional jumps.
First, an operation such as CMP , AND or SUB modifies the CPU flags. Second, a conditional jump instruction tests the flags and causes a branch to a new address. Example 1: The CMP instruction campers AL to Zero. The JZ ( jumps if Zero) instruction jumps to label L1 if Zero flags was set by the CMP instruction: Cmp a1,0 Jz L1 ;Jump if ZF=1 : L1

10 Example 2 : The AND instruction performs a bitwise AND on the register, affecting the Zero flag. The JNZ (jump if not Zero) instruction jumps if the Zero flag is clear: And d1, Jnz L2 : ; jump if ZF = 0 L2:

11 Jcond instruction A condition jump instruction branches to a destination label when a flag condition is true. If the flag condition is false, the instruction immediately following the conditional jump is executed. The syntax is: Jcond destination Cond refers to a flag condition, identifying the state of one or more flags. For example: Jc jump if carry=1 Jnc jump if not carry Jz jump if zero=1 Jnz jump if not zero

12 Jump if not parity (odd)
Each conditional jump instruction checks one or more flags, returning a result of true or false. If the result is true, the jump is taken; otherwise the program skips the jump and conditions to the next instruction. Mnemonic Description Flags JZ Jump if zero ZF = 1 JNZ Jump if not zreo ZF = 0 JC Jump if carry CF = 1 JNC Jump if not carry CF = 0 JO Jump if overflow OF = 1 JNO Jump if not overflow OF = 0 JS Jump if signed SF = 1 JNS Jump if not signed SF = 0 JP Jump if parity (even) PF = 1 JNP Jump if not parity (odd) PF = 0 Limitations: MASM requires the destination of the jump to be a label within -128 to 127 bytes from the jump instruction


Download ppt "More on logical instruction and"

Similar presentations


Ads by Google