Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Flow Control Instructions

The JUMP Instruction:


Unconditional Jump The JMP instruction Conditional Jump The transfer of control

causes an unconditional transfer of control.

depends on the previous instruction.

Flow Control Instructions


Example of Conditional Jump:
CMP AX, BX JG END

Example of Unconditional Jump:


MOV AX, BX JMP END

What is the difference????

Flow Control Instructions


The CMP Instruction: The JUMP condition is often provided by the CMP (Compare) instruction.

Example:
CMP JL AX, BX BELOW
What is difference between CMP and TEST instruction?

Flow Control Instructions


Some Example of Conditional Jumps: JG/JNLE : jump if greater than jump if not less than or equal to JGE/JNL : jump if greater than jump if not less than to JL/JNGE : jump if less than jump if not greater than or equal

Flow Control Instructions


Some Example of Conditional Jumps: JLE/JNG : jump if less than jump if not greater than JE/JZ : JC/JNC : jump if equal jump if equal to zero jump if carry jump if not carry

Flow Control Instructions

High Level Language Structure

Flow Control Instructions


Branching Structure: IF-THEN
Pseudocode in HLL:
False True Condition

IF Condition is TRUE THEN Execute true-branch statements END_IF

True-branch statement

Flow Control Instructions


Branching Structure: IF-THEN
Example: Replace the value in AX by its absolute value if AX<0

CMP AX, 0 JGE END_IF NEG AX END_IF :

Flow Control Instructions


Branching Structure: IF-THEN-ELSE
Pseudocode in HLL: IF Condition is TRUE THEN Execute true-branch statements ELSE Execute false-branch statements END_IF
False Condition True

False-branch statements

True-branch statement

Flow Control Instructions


Branching Structure: IF-THEN-ELSE
Example: CMP IF AL < 0 THEN put FFh in AH ELSE put 0 in AH END_IF JL MOV JMP AL, 0 ELSE_ AH, EXIT_ MOV AH, 0FFH 0

ELSE_ : EXIT_ :

Modify it using greater than or equal to ..

Flow Control Instructions


Loop .. A loop is a sequence of instructions that is repeated until the condition becomes false. Example: -> for -> while -> repeat..until

Flow Control Instructions


FOR loop .
Example: Write the code that prints 80 stars in a row
Initialize count

Code in C for ( int i = 0; i < 80; i++ )

Statements

printf(*);

Code in Assembly MOV CX, 80 MOV AH, 2 MOV DL, *

Dec. count

False count = 0

TOP: INT 21H

True

LOOP TOP

Flow Control Instructions


WHILE loop .
Example: Write some code to count
the number of characters in a line. False Condition True
Code in Assembly
MOV CX, 0 MOV AH, 1 INT 21H

Code in C
int i = 0; while ( gectchar(ch[i]) !=\n) i++;

Statements

WHILE_: CMP AL, 0DH JE END_WHILE INC CX INT 21H JMP WHILE_ END_WHILE :

Flow Control Instructions


REPEAT LOOP Syntax:
REPEAT Statement UNTIL condition

Flow Control Instructions


REPEAT LOOP
Example: Write some code to read characters until a blank space is read.

Pseudo code :
REPEAT read a character UNTIL character is blank

MOV AH, 1 REPEAT : INT 21H CMP AL, JNE REPEAT

Flow Control Instructions


CASE
Syntax:
In C:

CASE expression values_1: statements_1 values_2: statements_2 : : : : : : Values_n: statements_n END_CASE

switch (ch) { case 1 : printf(1); break; case 2 : printf(2); break; default : printf(Quit); }

Flow Control Instructions


CASE
WRITE A PROGRAM THAT SHOWS CORRESPONDING HEXADECIMAL DIGIT FOR DECIMAL VALUE (10-15) Sample Input: Enter the Hex Digit [A-F]:: D Sample Output: The Decimal value is :: 13

Flow Control Instructions


.MODEL SMALL .STACK 100H .DATA MSG1 DB Enter the Hex value :: $ MSG2 0DH, 0AH, The Decimal value is :: $ .Code B_: CMP AL, B MAIN PROC JNE C_ MOV AX, @DATA MOV BX, 11 MOV DS, AX JMP DISPLAY MOV AH, 9 LEA DX, MSG1 INT 21H MOV AH, 1 INT 21H CMP AL, A JNE B_ MOV BX, 10 JMP DISPLAY C_: CMP AL, C JNE D_ MOV BX, 12 JMP DISPLAY D_: CMP AL, D JNE E_ MOV BX, 13 JMP DISPLAY E_: CMP AL, E JNE F_ MOV BX, 14 JMP DISPLAY

F_: CMP AL, F JNE EXIT MOV BX, 15 DISPLAY :

MOV AH, 9 LEA DX, MSG2 INT 21H MOV AX, BX CALL OUTDEC

EXIT:

MOV AH, 4CH INT 21H

MAIN ENDP INCLUDE OUTDEC.ASM END MAIN

Flow Control Instructions


EXERCISE Do IT in REVERSE FORM . . . . . i.e. the input will be decimal number [10 15] and the output will be [A - F]. NOTE: Please complete the exercises on page 113 & 114 of your text book.

We can do these now. RIGHT???


* Calculate the sum OF THE SERIES. i) 1 + 4 + 7 + . + 148 ii) 100 + 95 + 90 + .. + 5

You might also like