cmp arg2, arg1
performs the same operation as sub arg2, arg1
except that none of the operands are modified. The difference is not stored anywhere.
However, the flags register is updated and can be used in a conditional jump, like jump-if-equal (JE
), most often as the next instruction after the cmp
.
The advantage over other instructions is that you can compare two values without destroying any of them. If you did sub arg2, arg1
and they happen to be equal, one of them would be zero afterwards. With cmp
they are both still there.