Assembly basics-CMP assembly instructions

Source: Internet
Author: User

CMP is a comparison command. The CMP function is equivalent to a subtraction command. It does not save the result, but only affects the corresponding flag. Other commands identify these affected tags to learn the comparison results.

CMP Command Format: CMP operation object 1, operation object 2

Calculation operation object 1-operation object 2, but the result is not saved, the corresponding flag is modified according to the result.

For example, if eax = 0 h
Then CMP eax, eax (eax-eax = 0)

After our command is executed, the ZF flag is 1, pF = 0, Sf = 0, cf = 0, of = 0.

(Because our result is 0, the ZF flag is 1. The premise is that when the PF operand is 0
PS: Actually, the PF flag is irrelevant)

Continue with the example

MoV EBX, 05 h
MoV eax, 08 h
CMP eax, EBX (eax-EBX = 03 h)
After executing the command (ZF = 0, pF = 1, Sf = 0, cf = 0, of = 0)

In this case, we can use the flag to determine whether eax is greater than EBX. We can think about the problem in reverse order sometimes.
You can think about what will happen if it is smaller .. For example, if Al = 05 h, BL = 08 h (for more intuitive purposes, I use low 8-bit registers)

MoV Al, 05 h
MoV BL, 08 h
CMP Al, BL (Al-BL =-3 H)
(Note: Al is a low 8-bit register. To make it more intuitive, I use a low 8-bit register. The negative number of the computer is stored in the memory in the form of a complement. Please pick up your hand, calculate the value of-3 .)
After execution (ZF = 0, pF = 0, cf = 1, Sf = 1, of = 0)

(Then you will certainly ask: cf = 1, why is there a carry operation. When two pieces of data in our computer are used for subtraction, the bitwise may be higher.
MoV Al, 56 h
MoV BL, 57 H
Sub Al, BL
After the bid is used, 156-57 H is generated)
I will not talk about SF. the symbolic flag is negative, so Sf = 1


Can we analyze the above two positions. If the value is smaller than 1, Sf = 1, cf = 1 .. Haha

So we can solve it easily just now .. If our value is greater than, then SF, CF, and ZF must be 0. So

MoV EBX, 05 h
MoV eax, 08 h
CMP eax, EBX (eax-EBX = 03 h)
After executing the command (ZF = 0, pF = 1, SP = 0, cf = 0, of = 0)

Then we have analyzed the = (equal to), <(less than), and> (greater than) situations. Next we will analyze the situations where not equal to, greater than or equal to, and less than or equal.

First, the analysis is not equal
(We can analyze the question through the reverse thinking) Suppose that the two values we compare are equal at this time, ZF = 1, so if they are not equal, ZF is definitely not equal to 1, that is, ZF = 0

Then the analysis is less than or equal
In fact, this is the + equal condition we just analyzed above. We can see that Sf = 1, cf = 1 if it is smaller than above. Then, the value is less than or equal to CF = 1 or ZF = 1.

Last greater than or equal
I won't talk about it anymore. Let everyone think about it ..

There are so many courses today. Last job column:

1. If there are two values for CMP comparison, ZF = 0, cf = 0, Sf = 0 after execution. Then, can we tell you whether our values are greater than, less than, or equal.
ZF = 0 is not equal to! = B
Cf = 0 No Borrow A> B
Sf = 0 positive number

Greater

2. If two values are used for CMP comparison, ZF = 1, cf = 1, Sf = 1 may be executed. So can we tell you whether our values are greater than or equal to, greater than or equal to, or less than or equal.
ZF = 1 equal a = B
Cf = 1 borrow a <B
Sf = 1 negative a <B

Less than or equal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.