The CMP instruction
CMP <opd>, <ops>
- The CMP instruction computes a fictional substraction between the numerical values of the two operands opd-ops.
CMP d,s | compare the value of the operands (without modifying them) (fictive execution d - s affecting only flags) | OF,SF,ZF,AF,PF şi CF |
- CMP decrements the value of the source operand from the destination operand, but unlike the instruction SUB, the result is not retained, it does not affect any of the operands' initial values. The effect of this statement is only to change the value of some flags in accordance with the opd-ops operation. The instruction CMP is most often used in combination with conditional jump instructions.
- Although its name is CMP, it is important to underline that in reality this instruction DOES NOT COMPARE anything, without having any criteria to compare and in fact it does not take any decision, but it only PREPARES the decision with the flag set, the actual comparison and the corresponding decision being taken concretely by the conditional jump instruction that will be used after the CMP instruction! If we do not use any decision-making instructions later, the CMP does not have any concrete role in any comparison, it is just a simple fictive subtraction with the role of changing flags, and it will not represent the CMP (compare) name.
- The destination operator may be a register or variable in memory.
- The source operand may be a register, a variable in memory or a constant.
- Both operands of the CMP instruction must be of the same size.
Example 1:
cmp eax, ebx ; ”compares” the values stored in the two registers (fictional subtraction eax-ebx)
jle done ;Depending on the conditional jump instruction used (here JLE), the comparison criteria is established.
;In this case: If the EAX content in the signed interpretation is less than or equal to the content in EBX then JUMP to the Done label.
;Otherwise continue with the following instruction (the flag being tested here is ZF).
done:
;instructions after the label 'done'
Example 2:
mov al,200 ; AL = C8h
mov bl,100
cmp al, bl ; fictive subtraction al-bl and set flangs
; accordingly (in our case this means SF=0, OF=1, CF=0 şi ZF=0)
JB et2 ;the conditional jump statement establishes the comparison criterion, in this case Jump if Below - comparison for unsigned numbers (is 200 below 100?) and test CF content: if CF = 1 the jump will be performed, if CF = 0 NO jump will be done.
;In our case CF=0, so the jump will not be performed.
;............. ;instructions set
et2:
;............. ;instructions set after this label
Example 3:
mov al,-56 ; AL = C8h = 200 in unsigned interpretation
mov bl,100
cmp al, bl ;fictive subtraction al-bl which sets the flags (for our case, we will have SF = 0, OF = 1, CF = 0 and ZF = 0)
JNGE et2 ;verify condition JNGE - Jump if not greater or equal
;(SIGNED comparison -56 versus 100)
;verifies in fact if the two flags, SF and OF, have different values
; Considering that in our case SF=0 and OF=1, so SF <> OF, the condition is fullfilled (and truly -56 is „NOT GREATER OR ;EQUAL” to 100) so the jump to label et2 will be performed
mov dx,1
et2:
mov cx,1
Example 4:
mov al,-56 ; AL = C8h = 200 in unsigned interpretation
mov bl,100
cmp al, bl ;fictive subtraction al-bl is performed, setting flags accordingly (in our case SF=0, OF=1, CF=0 and ZF=0)
JNBE et2 ;verify condition JNBE - Jump if not below or equal
;(UNSIGNED comparison 200 versus 100)
;verifies in fact if CF=0 and ZF=0
;Considering that in our case CF=0 and ZF=0, the condition is fulfilled (and truly 200 is „NOT BELOW OR EQUAL” ;to 100) so the jump to label et2 will be performed
mov dx,1
et2:
mov cx,1
The TEST Instruction
TEST <opd>, <ops>
- The TEST instruction performs the AND logical operation between the two operands (fictive execution of ops AND opd) without saving the result of the operation in any of the operands.
- Both operands of the TEST instructions must be of the same size.
- The only effect of a TEST statement is to modify the flag content specified in the above table, corresponding to the result of the AND operation performed.
TEST d,s | fictive execution d AND s | OF = 0, CF = 0 SF,ZF,PF - modified AF - undefined |
Example 1:
bothOpZero: ;instruction set for this label....
test ECX, ECX ; set ZF to 1 if ECX == 0
je bothOpZero ; according to the jump instruction utilised
; (here JE) we establish the comparison criteria.
;In our case: jump to label if ZF = 1
; Alternatively, the jz bothOpZero option could be used here, these two conditional jump instructions (JE and JZ) being similar in terms of the tested condition (true if ZF = 1)
Example 2:
mov AH,[v]
test AH,0F2h
js et2 ; according to the jump instruction utilised (here JS) we establish the comparison criteria.
; In our case: if the result of the operation AH AND 0F2h is a negative number in signed interpretation (the signed bit of the result is 1) then Jump ;to label et2 (the flag that is set is SF).
et2: ;instructions set for this label....
- Similar to the situation from the CMP instruction, we have to make the following observation:
- Although its name is TEST, it is important to underline that in reality this instruction DOES NOT TEST anything, not establishing any test criteria and not actually taking any decision, but it just PREPARES the appropriate decision with the set flags, for the test criterion, the actual testing and the decision corresponding to the concrete conditional jump instruction that will be used after the TEST statement! If we do not use any decision-making instructions later, TEST has no specific role in doing any test, it is just a simple AND bit-by-bit operation with affecting flags and will not be representive of its name TEST).