Assembly Programming Assembly Conditions - Assembly Programming

What is 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 −

SN Conditional Instructions
1

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.

2

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 coaching 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, that is performed by using the JMP instruction. Conditional execution often involves a transfer of control to the address of an instruction that does not follow the presently executing instruction. transfer of manage 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 in which 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,

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 –

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Assembly Programming Topics