SoFunction
Updated on 2025-03-04

What is the difference between test and cmp in assembly language

Assembly language is a low-level language used in electronic computers, microprocessors, microcontrollers, or other programmable devices, also known as symbolic language. In assembly language, mnemonics are used instead of the operation code of machine instructions, and address symbols or labels are used instead of the address of instructions or operands. In different devices, assembly language corresponds to different machine language instruction sets and is converted into machine instructions through assembly process. Specific assembly language and specific machine language instruction sets are one-to-one, and cannot be directly ported between different platforms. Let’s see what is the difference between test and cmp in assembly language.

The difference between assembly test and cmp

After reading the cracking tutorial, everyone knows that test and cmp are more critical, but I have never known how they are compared. Finally, I decided to find a lot of information and figure them out with everyone.

First look at: Status Register (i.e. Flag Register)

The PSW (Program Flag) program status word (i.e. flag) register is a 16-bit register consisting of a condition code flag (flag) and a control flag.
As shown below:

Condition code:

①OF(Overflow Flag) overflow flag, which is 1 when overflowing, otherwise set 0. Indicate an overflow calculation, such as: the structure and the target do not match.
②SF(Sign Flag) symbol flag, set 1 when the result is negative, otherwise set 0.
③ZF (Zero Flag) zero flag, set 1 when the calculation result is 0, otherwise set 0.
④CF (Carry Flag) carry flag, set 1 when carrying, otherwise set 0. Note: The rightmost bit after calculation is stored in the Carry flag.
⑤AF (Auxiliary carry Flag) auxiliary carry flag, record the entry position generated by the third bit (half byte) during operation.
1 when there is a carry, otherwise set to 0.
⑥PF(Parity Flag) Parity flag. Set 1 when the number of 1 in the result operand is even, otherwise set 0.

Control flag bits:

⑦DF (Direction Flag) direction flag, controls the direction of information in the string processing instruction.
⑧IF(Interrupt Flag) interrupt flag.
⑨TF (Trap Flag) trap sign.

For example, let me explain jnz and jz

Test conditions
JZ ZF=1
JNZ ZF=0
That is, Jz=jump if zero (if the result is 0, set the ZF zero flag to 1, jump)
Jnz=jump if not zero

OK, let's look at test and cmp

test belongs to logical operation instructions

Function: Perform logical operations between BIT and BIT
Test (two operands are used to perform operations, only the flag bit is modified, and the result is not returned).
Test performs AND logic operations on two parameters (target, source) and sets flag registers according to the result, and the result itself will not be saved. EST AX,BX has the same effect as AND AX,BX commands

Syntax: TEST r/m,r/m/data
Influence flags: C, O, P, Z, S (where C and O flags will be set to 0)

Examples of use:

Used to test a bit, such as a register:

test eax, 100b; b suffix means binary
jnz **; If the third digit of the right number of eax is 1, jnz will jump

I think so, the condition for jnz jump is ZF=0, ZF=0 means that ZF (zero flag) is not set, that is, the logic and result are 1.

A very common usage is to test whether a party register is empty:

test ecx, ecx
jz somewhere

If ecx is zero, set the ZF zero flag to 1, and Jz jump

CMP belongs to arithmetic operation instructions

Function: Comparison of two values ​​(register, memory, direct value)
Syntax: CMP r/m,r/m/data
Flag bits: C,P,A,Z,O

CMP comparison. (Two operands are subtracted, only the flag bit is modified, and the result is not returned).
cmp is actually a subtraction that only sets flags and does not save structures, and sets Z-flag (zero flag).
The zero flag is very similar to carry, and is also a bit of the internal flag register.

For example:

Cmp eax, 2; If eax-2=0, that is, eax=2, set the zero flag to 1
Jz **; if zero flag is set, jump

Conclusion I've come to
If the test logic and operation result are zero, set ZF (zero flag) to 1;
cmp Arithmetic subtraction operation result is zero, so set ZF (zero flag) to 1.

The conclusion is very simple, why can’t I tell the difference before? How stupid is it!

ps: What is the role of CMP in assembly language?

The mp(compare) instruction compares the sizes of two operands.

1. Assembly language is a machine-oriented programming language. In assembly combinations, use mnemonics instead of opcodes, and use address symbols or labels instead of address codes. In this way, using symbols instead of binary code in machine language turns machine language into assembly language. Therefore, assembly language is also called symbolic language. Programs written in assembly language cannot be directly recognized by the machine. A program needs to translate the assembly language into machine language. This kind of program that plays a translation role is called assembler. Assembler is the language processing system software in the system software. The process of the assembly program translates assembly language into machine language is called assembly.

2. Example: cmmp oprd1, oprd2, subtracts the second operand for the first operation, but does not affect the values ​​of the two operands. It affects the CF, ZF, OF, AF, and PF of flag. If ZF=1 is executed, it means that the two numbers are equal, because zero is 1 means that the result is 0. When unsigned, CF=1 means there is a carry or borrowed bit, and cmp is a subtraction operation, so it can be seen that it is a borrowed bit, so oprd1<oprd2.

=0 means there is no borrowing bit, but at this time, you should pay attention to whether ZF is 0. If it is 0, it means that the result is not 0. Therefore, at this time, oprd1>oprd2. When there is a sign, if SF=0 and OF=0 means that the value at this time is a positive number and has no overflow. It can be seen intuitively that oprd1>oprd2. If SF=1 and OF=0 means that the value at this time is a negative number and has no overflow, it means that oprd1<oprd2. If SF=0 and OF=1 means that the value at this time is a positive number and has overflow. You can see that oprd1<oprd2. If SF=1 and OF=1 means that the value at this time is a negative number and has overflow. You can see that oprd1<oprd2. If SF=1 and OF=1 means that the value at this time is a negative number and has overflow. You can see that oprd1>oprd2.

Summarize

The above is what is the difference between test and cmp in the assembly language introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!