ALT by Bilal Ahmed Shaik - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

img120.png

 

  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:

 

img121.png 

  The 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

 

img122.png 

  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:

 

img123.png

img124.png CMP is often used for comparing whether a counter value has reached the number of time a loop needs to be run. Consider the following typical condition:

 

img125.png 

  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 e xecuting 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 synta x of the JMP instruction is:

 

img126.png 

  EXAMPLE:

 

  The following code snippet illustrates the JMP instruction:

 

img127.png

img128.png

 

  The following conditional jump instructions have special uses and check the value of flags:

 

img129.png

  The syntax for the J<condition> set of instructions:

  Example,

 

img130.png 

  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, 72 and 31 respectively:

 

img131.png

img132.png

 

  When the above code is compiled and executed, it produces fol owing result:

 

img133.png