Assembly — Conditions
Conditional execution in assembly language is accomplished by several looping and branching instructions. These instructions can change the flow of control in a program. Conditional execution is observed in two scenarios −
Unconditional jump
This is performed by the JMP instruction. Conditional execution often involves a transfer of control to the address of an instruction that does not follow the currently executing instruction. Transfer of control may be forward, to execute a new set of instructions or backward, to re-execute the same steps.
Conditional jump
This is performed by a set of jump instructions j<condition> depending upon the condition. The conditional instructions transfer the control by breaking the sequential flow and they do it by changing the offset value in IP.
Let us discuss the CMP instruction before discussing the conditional instructions.
CMP Instruction
The CMP instruction compares two operands. It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands. It is used along with the conditional jump instruction for decision making.
Syntax
CMP compares two numeric data fields. The destination operand could be either in register or in memory. The source operand could be a constant (immediate) data, register or memory.
Example
CMP is often used for comparing whether a counter value has reached the number of times a loop needs to be run. Consider the following typical condition −
Unconditional Jump
As mentioned earlier, this is performed by the JMP instruction. Conditional execution often involves a transfer of control to the address of an instruction that does not follow the currently executing instruction. Transfer of control may be forward, to execute a new set of instructions or backward, to re-execute the same steps.
Syntax
The JMP instruction provides a label name where the flow of control is transferred immediately. The syntax of the JMP instruction is −
Example
The following code snippet illustrates the JMP instruction −
Conditional Jump
If some specified condition is satisfied in conditional jump, the control flow is transferred to a target instruction. There are numerous conditional jump instructions depending upon the condition and data.
Following are the conditional jump instructions used on signed data used for arithmetic operations −
| Instruction | Description | Flags tested |
|---|---|---|
| JE/JZ | Jump Equal or Jump Zero | ZF |
| JNE/JNZ | Jump not Equal or Jump Not Zero | ZF |
| JG/JNLE | Jump Greater or Jump Not Less/Equal | OF, SF, ZF |
| JGE/JNL | Jump Greater/Equal or Jump Not Less | OF, SF |
| JL/JNGE | Jump Less or Jump Not Greater/Equal | OF, SF |
| JLE/JNG | Jump Less/Equal or Jump Not Greater | OF, SF, ZF |
Following are the conditional jump instructions used on unsigned data used for logical operations −
| Instruction | Description | Flags tested |
|---|---|---|
| JE/JZ | Jump Equal or Jump Zero | ZF |
| JNE/JNZ | Jump not Equal or Jump Not Zero | ZF |
| JA/JNBE | Jump Above or Jump Not Below/Equal | CF, ZF |
| JAE/JNB | Jump Above/Equal or Jump Not Below | CF |
| JB/JNAE | Jump Below or Jump Not Above/Equal | CF |
| JBE/JNA | Jump Below/Equal or Jump Not Above | AF, CF |
The following conditional jump instructions have special uses and check the value of flags −
| Instruction | Description | Flags tested |
|---|---|---|
| JXCZ | Jump if CX is Zero | none |
| JC | Jump If Carry | CF |
| JNC | Jump If No Carry | CF |
| JO | Jump If Overflow | OF |
| JNO | Jump If No Overflow | OF |
| JP/JPE | Jump Parity or Jump Parity Even | PF |
| JNP/JPO | Jump No Parity or Jump Parity Odd | PF |
| JS | Jump Sign (negative value) | SF |
| JNS | Jump No Sign (positive value) | SF |
The syntax for the J<condition> set of instructions −
Example
The following program displays the largest of three variables. The variables are double-digit variables. The three variables num1, num2 and num3 have values 47, 22 and 31, respectively −
When the above code is compiled and executed, it produces the following result −
CMP—Compare Two Operands
In 64-bit mode, r/m8 can not be encoded to access the following byte registers if a REX prefix is used: AH, BH, CH, DH.
Instruction Operand Encoding
| Op/En | Operand 1 | Operand 2 | Operand 3 | Operand 4 |
| RM | ModRM:reg (r) | ModRM:r/m (r) | NA | NA |
| MR | ModRM:r/m (r) | ModRM:reg (r) | NA | NA |
| MI | ModRM:r/m (r) | imm8 | NA | NA |
| I | AL/AX/EAX/RAX (r) | imm8 | NA | NA |
Description
Compares the first source operand with the second source operand and sets the status flags in the EFLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction. When an immediate value is used as an operand, it is sign-extended to the length of the first operand.
The condition codes used by the Jcc, CMOVcc, and SETcc instructions are based on the results of a CMP instruction. Appendix B, “EFLAGS Condition Codes,” in the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1, shows the relationship of the status flags and the condition codes.
In 64-bit mode, the instruction’s default operation size is 32 bits. Use of the REX.R prefix permits access to addi-tional registers (R8-R15). Use of the REX.W prefix promotes operation to 64 bits. See the summary chart at the beginning of this section for encoding data and limits.
Operation
Flags Affected
The CF, OF, SF, ZF, AF, and PF flags are set according to the result.
Protected Mode Exceptions
If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.
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:
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?
4 Answers 4
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.
![]()
the results of CMP is changing the values of ZF and CF, this is some examples to understand very much CMP instruction.
Example 1: if AX < BX
Result : ZF and CF set to ==> "ZF = 0" and "CF = 1"
Example 2 : if AX > BX
Result : ZF and CF set to ==> "ZF = 0" and "CF = 0"
Example 3 : if AX = BX
Result : ZF and CF set to ==> "ZF = 1" and "CF = 0"
i hope you understand the results of CMP is changing the value of ZF and CF
ZF = Zero Flag
CF = Carry Flag
![]()
![]()
We use cmp arg2, arg1 when we care about whether arg1 and arg 2 are equal. The processor determines this by subtracting arg2 from arg1, then looking at the result. If the result is zero (that is, arg1 = arg2), then the processor sets the zero flag (by «sets the flag», we mean it sets it to 1). Conversely, if the result isn’t zero (that is, arg1 != arg2), then the processor clears the zero flag (i.e, sets it to 0). The result itself is discarded, because we don’t care what it is, only whether it’s zero or not, which we now know based on whether the zero flag is set. We can then use instructions like JE , JNE , JZ and JNZ that examine the zero flag and jump (or not) based on its value. In the case of JE (jump if equal), the jump will happen if the zero flag is set, which (as we learned above) it will be if the arguments in the cmp were equal.
I think it’s very late to post an answer on this question. But I can give you a better illustration on how this CMP instruction works.
When you Compare two arguments using CMP arg1, arg2
CMP instruction sets status flags according to the comparisons between the arguments. See : wikipedia’s FLAGS page
The importance of CMP applies mostly in conditional code execution (Jump — See : assembly_conditions). When the processor executes a conditional-jump jcc instruction, it checks the status flags register and jumps to the target label if it meets the conditions, otherwise falls through to the next instruction.
Cmp ассемблер как работает
I’m very new to assembly and now I’m trying to understand how cmp works. Here is what’s written in wiki:
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?
![]()
4 Answers 4
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.
![]()
the results of CMP is changing the values of ZF and CF, this is some examples to understand very much CMP instruction.
Example 1: if AX < BX
Result : ZF and CF set to ==> «ZF = 0» and «CF = 1»
Example 2 : if AX > BX
Result : ZF and CF set to ==> «ZF = 0» and «CF = 0»
Example 3 : if AX = BX
Result : ZF and CF set to ==> «ZF = 1» and «CF = 0»
i hope you understand the results of CMP is changing the value of ZF and CF
ZF = Zero Flag
CF = Carry Flag
![]()
![]()
We use cmp arg2, arg1 when we care about whether arg1 and arg 2 are equal. The processor determines this by subtracting arg2 from arg1, then looking at the result. If the result is zero (that is, arg1 = arg2), then the processor sets the zero flag (by «sets the flag», we mean it sets it to 1). Conversely, if the result isn’t zero (that is, arg1 != arg2), then the processor clears the zero flag (i.e, sets it to 0). The result itself is discarded, because we don’t care what it is, only whether it’s zero or not, which we now know based on whether the zero flag is set. We can then use instructions like JE , JNE , JZ and JNZ that examine the zero flag and jump (or not) based on its value. In the case of JE (jump if equal), the jump will happen if the zero flag is set, which (as we learned above) it will be if the arguments in the cmp were equal.
I think it’s very late to post an answer on this question. But I can give you a better illustration on how this CMP instruction works.
When you Compare two arguments using CMP arg1, arg2
CMP instruction sets status flags according to the comparisons between the arguments. See : wikipedia’s FLAGS page
The importance of CMP applies mostly in conditional code execution (Jump — See : assembly_conditions). When the processor executes a conditional-jump jcc instruction, it checks the status flags register and jumps to the target label if it meets the conditions, otherwise falls through to the next instruction.