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, b ;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)
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:
Using Compare (CMP)
Comparison Instructions CMP Instruction For Signed operand comparisons:
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
Jumps Based on General Comparisons
Jumps Based on Unsigned Comparisons
Jumps Based on Signed Comparisons
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 Jznext;jump if ZF=1 … Next:
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 Movbx,2 L2: …
Do While Loop