1 / 13

CMP Instruction

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.

amity-ramos
Download Presentation

CMP Instruction

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. 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

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. HLL Structures • IF-THEN statement • If op1=op2 then • Statement1 • Statement 2 • …… • endif Lecture 26

  8. IF-THEN statement • Inefficient Code cmp op1,op2 je then jmp endif then: statement1 statement2 ……… endif: Lecture 26

  9. IF-THEN statement • Efficient Code cmp op1,op2 jne endif then: statement1 statement2 ……… endif: Lecture 26

  10. IF with OR operator • If (al > op1) or (al >=op2) or (al=op3) or (al < op4) then Statement1 Statement2 ……….. end if Lecture 26

  11. 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

  12. IF with AND operator • If (al > op1) and (al >=op2) and (al=op3) and (al < op4) then Statement1 Statement2 ……….. endif Lecture 26

  13. 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

More Related