130 likes | 332 Views
CMP Instruction. Compares 2 8-bit/16-bit operands Performs an implied subtraction (destination – source) Appropriate flags are set/cleared Neither operand changes One operand may be a memory Destination can not be an immediate operand or IP Neither operand may be a segment register.
E N D
CMP Instruction • Compares 2 8-bit/16-bit operands • Performs an implied subtraction (destination – source) • Appropriate flags are set/cleared • Neither operand changes • One operand may be a memory • Destination can not be an immediate operand or IP • Neither operand may be a segment register Lecture 26
Comparison and Test Instructions • Flags Affected • Dest < Source C=1 • Dest = Source Z=1 • Dest > Source C= 0 Z= 0 • mov al, 5 • cmp al, 10 C=1 • mov ax, 1000h • mov cx, 1000h • cmp cx, ax Z=1 • mov si, 105 • cmp si, 0 Z=0 and C=0 Lecture 26
TEST Instruction • Performs an implied AND operation on the destination operand using the source operand • Neither operand changes • If any matching bit positions are set in both operands Z is cleared Lecture 26
Comparison and Test Instructions • Input value 0010 0101 • Test input 0000 1001 Z = 0 • Input value 0010 0100 • Test input 0000 1001 Z = 1 • mov ah, 2 • int 17h ;read printer status • test al, 00100000b ; bit 5=1 indicates out of paper • jnz paper_error Lecture 26
LOOPNE/LOOPNZ • loop while not equal or not zero • Will branch to target address if cx is not zero and the flag is clear • no flags are affected • Use a je or jne to determine cause of termination • Loop range is (-128 to 127) bytes • Useful when need to search for a value in a set of values Lecture 26
LOOPNE/LOOPNZ • stop when a positive number is found • mov si, offset array-1 • mov cx, array_len • next: • inc si • test byte ptr [si], 80h • loopnz next • .data • array db -3, -6, -1, -10, 10, 30, 40, 4 • array_len equ $-array Lecture 26
HLL Structures • IF-THEN statement • If op1=op2 then • Statement1 • Statement 2 • …… • endif Lecture 26
IF-THEN statement • Inefficient Code cmp op1,op2 je then jmp endif then: statement1 statement2 ……… endif: Lecture 26
IF-THEN statement • Efficient Code cmp op1,op2 jne endif then: statement1 statement2 ……… endif: Lecture 26
IF with OR operator • If (al > op1) or (al >=op2) or (al=op3) or (al < op4) then Statement1 Statement2 ……….. end if Lecture 26
IF with OR operator cmp al, op1 jg then cmp al, op2 jge then cmp al, op3 je then cmp al, op4 jg endif then: Statement1 Statement2 ……….. end if: Lecture 26
IF with AND operator • If (al > op1) and (al >=op2) and (al=op3) and (al < op4) then Statement1 Statement2 ……….. endif Lecture 26
IF with AND operator cmp al, op1 jng endif cmp al, op2 jnge endif cmp al, op3 jne endif cmp al, op4 jnl endif then: Statement1 Statement2 ……….. endif: Lecture 26