130 likes | 313 Views
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
E N D
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)
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:
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
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:
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: …