Assembly cmp/test/jxx etc.

Source: Internet
Author: User
Tags processing instruction

0x01 Jump Instruction

In general, there are two kinds of JUMP commands in x86:

1. Arithmetic jumps, such as JZ (jump if zero), JC (Carry), JNC (jumping if not carry), etc.

2. Compare Jumps: Like JE (jump if equal), JB (jumping if below), JAE (if above or equal), etc.

The first type of jump applies after an arithmetic or logical instruction, for example:

Sub eax, Ebxjnz Result_is_not_zero and ECX, EDXJZ. The_bit_is_not_set

The second type of jump is applied after the CMP directive, for example:

cmp eax, Ebxjne eax_is_not_equal_to_ebxcmp ecx, Edxja. Ecx_is_above_than_edx

It is important to note that these directives are sometimes the same, such as JZ = = JE; JC = = JB; JNC = = Jae et cetera. Here is the detailed table, there are only 16 types of conditional jump instructions, but there are 30 instructions. This allows for the readability of the assembly program to be enhanced by mutual substitution.

Mnemonic Condition tested Description

Jo of = 1 overflow
Jno of = 0 Not overflow
JC, JB, Jnae CF = 1 carry/below/not above nor equal
Jnc, jae, jnb CF = 0 Not carry/above or Equal/not below
JE, jz ZF = 1 Equal/zero
Jne, jnz ZF = 0 Not equal/not zero
Jbe, JNA CF or ZF = 1 below or equal/not above
JA, jnbe CF or ZF = 0 Above/not below or equal
JS SF = 1 Sign
Jns SF = 0 Not sign
JP, JPE PF = 1 parity/parity even
JNP, Jpo PF = 0 Not parity/parity odd
JL, Jnge SF xor of = 1 less/not greater nor equal
Jge, Jnl SF xor of = 0 greater or equal/not less
Jle, Jng (SF xor of) or ZF = 1 less or equal/not greater

JG, Jnle (SF xor of) or ZF = 0 greater/not less nor equal

0x02 cmp/test etc.

Jump instruction, according to the relevant flag bit to jump. The cmp/test and other directives affect the PSW register-related flags, and the PSW register is the status register.

PSW Register Graph:

15   14    13    12   11   10    9    8    7    6    5    4    3    2   1   0 
           of    DF   IF   TF    SF   ZF   AF   PF   ; CF

    • of (Overflow flag) overflow flag, 1 overflow, or 0.

    • SF (sign flag) symbol, the result is negative 1, otherwise set 0. 

    • zf (Zero flag) 0 flag, operation result is 0 o'clock 1, otherwise set 0. 

    • CF (Carry flag) Carry flag, carry 1, or 0. Note: The CARRY flag holds the most right bit in the calculation.  

    • AF (auxiliary carry flag) auxiliary carry flag that records the incoming position of the 3rd bit (half byte) of the operation. With rounding 1, otherwise 0. 

    • PF (Parity Flag) Parity flag. The number of 1 in the result operand is an even time 1, otherwise 0.

    • DF (Direction flag) direction flag, which controls the direction of information in a string processing instruction.  

    • if (Interrupt flag) interrupt flag.  

    • TF (Trap flag) trap flags.

++++++++++++++++ test is a logical operation instruction ++++++++++++++++

Function: Performs a logical operation between bit and bit

Syntax: TEST r/m,r/m/data

Impact flag bit: C,o,p,z,s (where C and O two flags are set to 0)

Example:

1. test is used for testing a bit, such as a register:

Test eax, 100bjnz short loc_401168; If the third bit of EAX right is 1,JNZ will jump

2.test is used for testing whether the register is empty :

Test ecx, ecx JZ short loc_401334

If ECX is zero, set the ZF 0 flag to 1,JZ jump

++++++++++++++++  CMP belongs to arithmetic operation instruction ++++++++++++++++

Function: Compare two values (register, memory, direct value)

Syntax: CMP r/m,r/m/data

Impact Flag: C,p,a,z,o

CMP is actually a subtraction that only sets the flag to not save the result

Example:

cmp eax, 2; If Eax-2=0 is eax=2, set 0 flag to 1 jz short loc_401334; jump if 0 flag is set
the b/w/d in 0x03 Assembly

CMPS is used for comparison of two strings, with CMPS/CMPSB/CMPSW/CMPSD series directives. In the compilation, B/W/D has the following meanings:

B:byte/w:word (2bytes)/d:double Word (4bytes)


Assembly cmp/test/jxx etc.

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.