1 / 13

Comparison Instructions

Comparison Instructions. Test instruction Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags affected: OF=CF=0, SF, ZF,AF,PF) TEST reg, reg -TEST mem, reg TEST reg,mem -TEST mem, immed TEST reg, immed

drake-ayers
Download Presentation

Comparison Instructions

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. Comparison Instructions • Test instruction • Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags affected: OF=CF=0, SF, ZF,AF,PF) • TEST reg, reg -TEST mem, reg • TEST reg,mem -TEST mem, immed • TEST reg, immed • Example: Checking printer status • Mov ah,2 ;function: read printer status • Int 17h ;call BIOS • Test al,00100000b ;ZF=0 if out of paper • Can check if either bit 0 or bit 3 is set (ZF=1 only if both bits are clear)

  2. Comparison Instructions • CMP Instruction • Performs an implied subtraction of a source operand from a destination operand. Neither operand is modified. Segment registers cannot be used. • CMP reg, reg - CMP mem, reg • CMP reg,mem - CMP mem, immed • CMP reg, immed • For Unsigned operand comparisons:

  3. Using Compare (CMP)

  4. Comparison Instructions • CMP Instruction • For Signed operand comparisons:

  5. Conditional Jumps • Jcond Instructions • Transfers control to a destination address when a flag condition is true. • Destination address must be –128 to +127 bytes from the current location, which can sometimes be a problem. • Three types of conditional jump instructions: • general comparisons • unsigned comparisons • signed comparisons

  6. Jumps Based on General Comparisons

  7. Jumps Based on Unsigned Comparisons

  8. Jumps Based on Signed Comparisons

  9. Steps to executing IF statements • Use comparison and arithmetic statements (CPU sets individual flags based on result) • Use conditional jump instructions (CPU takes action based on flags) cmp al, 0 Jz next ;jump if ZF=1 … Next:

  10. Use SHORT operator to improve machine code • Use SHORT before jump label to eliminate NOP instructions after your jump instruction. Cmp ax,0 Jne short L2 Mov bx,2 L2: …

  11. Do While Loop

More Related