Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding cmp instruction

I'm very new to assembly and now I'm trying to understand how cmp works. Here is what's written in wiki:

cmp arg2, arg1

Performs a comparison operation between arg1 and arg2. The comparison is performed by a (signed) subtraction of arg2 from arg1, the results of which can be called Temp. Temp is then discarded.

What does it mean "Temp is then discarded"? Where is it stored? How can I access this result of the comparison? Can someone explain it?

like image 358
St.Antario Avatar asked Aug 26 '17 18:08

St.Antario


People also ask

How does the CMP instruction work?

The CMP instruction subtracts the value of Operand2 from the value in Rn . This is the same as a SUBS instruction, except that the result is discarded. The CMN instruction adds the value of Operand2 to the value in Rn . This is the same as an ADDS instruction, except that the result is discarded.

What does CMP mean in assembly?

Compare Two Operands (cmp) (IA-32 Assembly Language Reference Manual)

How does CMP work in assembly x86?

A CMP instruction performs a subtract operation on both operands, and the status flags ZF and CF will be set according to the result. It should be noted that the CMP instruction also does not affect the operands. When the destination operand and source operand are equal, ZF will be set to 1.


1 Answers

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.

like image 64
Bo Persson Avatar answered Sep 21 '22 17:09

Bo Persson