1 / 12

More on logical instruction and conditional jumps

More on logical instruction and conditional jumps. 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.

jett
Download Presentation

More on logical instruction and conditional jumps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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, 10110101b ; 5 bits = odd parity • Xor al, 0 ; parity flag clear (PO) • Mov al, 11001100b ; 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, 11110000b • NOT al ; AL = 00001111b • 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, 00001001b ; 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 • 0 0 1 0 0 1 0 1 input value • 0 0 0 0 1 0 0 1 test value • 0 0 0 0 0 0 0 1 result value • 0 0 1 0 0 1 0 0 input value • 0 0 0 0 1 0 0 1 test value • 0 0 0 0 0 0 0 0 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:

  7. CMP Results Flags destination < source SF = OF destination > source SF = OF destination = source ZF = 1 • 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.

  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, 10110000 • 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. 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. • Limitations: MASM requires the destination of the jump to be a label within -128 to 127 bytes from the jump instruction

More Related