0% found this document useful (0 votes)
427 views51 pages

Flow Control Instructions: Chapter-6

The document discusses various flow control instructions in assembly language like jump and loop instructions to transfer program control conditionally or unconditionally. It provides examples of using conditional jump instructions like JNZ to implement loops and explains how high-level programming structures like IF-THEN, IF-THEN-ELSE, and CASE can be simulated using jump and conditional jump instructions in assembly language. The document also discusses different types of conditional jumps based on status flag settings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
427 views51 pages

Flow Control Instructions: Chapter-6

The document discusses various flow control instructions in assembly language like jump and loop instructions to transfer program control conditionally or unconditionally. It provides examples of using conditional jump instructions like JNZ to implement loops and explains how high-level programming structures like IF-THEN, IF-THEN-ELSE, and CASE can be simulated using jump and conditional jump instructions in assembly language. The document also discusses different types of conditional jumps based on status flag settings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Flow Control

Instructions

Chapter-6

Prepared by~
Mohammad Samawat Ullah
Assistant Professor,
Department of CS
AIUB
Email: [email protected]
Overview
At the end of this chapter, we will know
about:

• Decision making and repeating statement

• Jump and loop instructions

• Algorithm conversion to assembly language

• High-Level Language Structures


Jump and Loop
 Jump and Loop instructions transfers control to
another program

 The transfers can be unconditional or

 Depends on a particular combination of status


flags settings

 Conversion of algorithm is easier in assembly


Example of Jump
.MODEL SMALL PRINT_LOOP:

INT 21H
.STACK 100H
INC DL

.CODE DEC CX

JNZ PRINT_LOOP ;
MAIN PROC
; return to DOS
MOV AH,2 MOV AH,4Ch ;DOS exit

INT 21H
MOV CX,256
MAIN ENDP
MOV DL,0 ; ASCII of null
END MAIN
JNZ (Jump if Not
Zero)
• JNZ is the instruction that controls loop.

• If result of preceding instruction is not Zero Then the JNZ


transfers the control to the instruction at label PRINT_LOOP.

• If the preceding instruction contains zero (i.e. CX=0) then the


program goes to execute DOS return instructions.

• PRINT_LOOP is the first statement label

• Labels are needed to refer another instruction

• Labels end with colon (:)

• Labels are placed on a line by themselves to make it stand


out.
Conditional Jumps
 JNZ is an example of Conditional Jump Instruction:

JXXX destination_label

 if the condition for the jump is true, the next


instruction to be executed is at destination_label

 If condition is false, the instruction following the


jump is done next.

 i.e. for JNZ if the preceding instruction is non-zero


Implementation of
Conditional JUMP by CPU

 The CPU looks at the FLAGS register (it reflects the


result of last thing that processor did)

 If the conditions for the jump (combination of status


flag settings) are true, the CPU adjusts the IP to point
to the destination label.

 The instruction at this label will be done next.

 If the jump condition is false, then IP is not altered


and naturally the next instruction is performed.
Example(cont’d…)
In the previous example:

 The JNZ PRINT_LOOP is executed by


inspecting ZF

 If ZF=0 then control is transferred to


PRINT_LOOP and continues

 If the ZF=1 then the program goes on to


execute next (i.e. MOV AH,4CH)
CMP Instruction
 CMP destination , source

 Compares the destination with source by computing


contents

 It computes by…

destination contents - source contents

 The result is not stored but the FLAGS are affected

 The OPERANDS of CMP may not both be Memory


Locations

 Destination may not be CONSTANT

 CMP is just like SUB. However result is not stored in


destination.
Signed Conditional
Jumps
• Jump if Greater than
JG or ZF = 0 and
• Jump if Not Less than or
JNLE SF = OF
Equal to
• Jump if Greater than or
JGE or Equal to
SF = OF
JNL • Jump if Not less than or
Equal to
• Jump if less than
JL or • Jump if not greater than or SF<>OF
JNGE
equal

JLE or • Jump if less than or Equal


ZF = 1 or SF<> OF
JNG • Jump if not greater than
Unsigned Conditional
Jumps
• Jump if Above
JA or ZF = 0 and
• Jump if Not Below or Equal
JNBE CF = 0
to

JAE or • Jump if Above or Equal to


CF = 0
JNB • Jump if Not Below

JB or • Jump if Below
CF = 1
JNAE • Jump if not Above or Equal

JBE or • Jump if Below or Equal


CF=1 or ZF = 1
JNA • Jump if Not Above
Single-Flag Jumps
JE or • Jump if Equal
ZF = 1
JZ • Jump if equal to Zero
JNE or • Jump if Not Equal
ZF = 0
JNZ • Jump if Not Zero
CF = 1
JC • Jump if Carry
CF = 0
JNC • Jump if no Carry CF=0

JO • Jump if Overflow CF=1 or ZF = 1

JNO • Jump if No Overflow OF=1


JS Jump if Sign Negative SF = 1
JNS Jump if Non-Negative Sign SF =0
JP/JPE Jump if Parity Even PF=1
JNP/JPO Jump if parity Odd PF=1
Conditional Jumps
 Interpretation
Signed JUMPs correspond to an analogous unsigned JUMPs (i.e.
JG is equivalent to JA)

 Signed jump’s operates on ZF, SF and OF

 Unsigned JUMP’s operates on ZF and CF

 Using wrong kind of JUMP can lead to wrong results. For


example: for AX= 7FFFh and BX= 8000h

CMP AX,BX

JA BELOW

 Even though 7FFFh>8000h in a signed sense, the program does


not jump to label BELOW. Because

 In unsigned sense (JA) 7FFFh < 8000h


Working with
Characters
 To deal with ASCII character set, either Signed
or Unsigned jumps may be used.

i.e. sign bit of a byte in a character is


always zero.

 However, while comparing extended ASCII


characters (80h to FFh ), UNSIGNED jumps
should be used.
JMP Instruction
 JMP instruction causes an unconditional
transfer of control.

JMP destination

 Destination is usually a label in the same


segment as the JMP itself. [ref: appendix - F]

 To get around the range of restriction of a


conditional jump, JMP can be used.
JMP Vs JNZ
TOP:

DEC CX
TOP:
JNZ BOTTOM ; keep looping till
CX>0
DEC CX
JMP EXIT
JNZ TOP BOTTOM:

JMP TOP
MOV AX,BX
EXIT:

MOV AX,BX
High-Level Language
Structures
 Jump can be used to implement branches and
loops

 As the Jump is so primitive, it is difficult to


code an algorithm with jumps without some
guidelines.

 The High-level languages (conditional IF-ELSE


and While loops) can be simulated in
assembly.
Branching Structures
 Branching structures enable a program to
take different paths, depending on
conditions.

 Here, We will look at three structures.

1. IF-THEN

2. IF-THEN-ELSE

3. CASE
IF-THEN

IF condition is true.

THEN

execute true-
branch statements

END_IF
A Pseudo Code , Algorithm and
Code for IF-THEN
• The co11dition is an expression that is either true
or false.

• lf It is true, the true-branch statements are


executed.

• lf It is false, nothing is done, and the program goes


on to whatever follows.

• Example: to Replace a number in AX by its absolute


value…
CMP AX, 0
IF AX < 0
JNL END_IF
THEN
NEG AX
replace AX by -AX
END_IF:
END_IF
IF-THEN-ELSE
IF condition is true

THEN

execute true-branch
statements

ELSE

execute false-branch
statements

END_IF
A Pseudo Code and Algorithm and
Code for
IF-THEN-ELSE
• The condition is an expression that is either true or false.

• If It is true, the true-branch statements are executed.

• If It is false then False-branch statements are executed.

• Example: Suppose AL and BL contain extended ASCII


characters. Display the one that comes first in the
character sequence…

IF AL <= BL MOV AH,2


THEN CMP AL,BL ;AL<=BL ?
Display the JNBE ELSE_
MOV DL,AL
character in AL
JMP DISPLAY
ELSE ELSE_:
Display the MOV DL,BL
character in BL DISPLAY:
END IF INT 2lh
CASE
A CASE is a multi-way branch structure that tests a
register, variable, or expression for particular values or a
range of values.

CASE Expression

Values_1: Statement_1

Values_2: Statement_2

Values_n: Statement_n

END_CASE
CASE
• Example: If AX contains a negative number, put
-1 in BX; if AX contains 0, put 0 in BX; and if AX
contains a positive number, put 1 in BX.

CMP AX,O
JL NEGATIVE
JE ZERO
JG POSITIVE
CASE AX NEGATIVE:
<0 : put -1 in BX MOV BX,-1
=0 : put 0 in BX JMP END_CASE
ZERO:
>0 : put +l in BX
MOV BX,0
END_CASE JMP END_CASE
POSITIVE:
MOV BX, l
END_CASE:
Solve the Following
• If AL contains 1 or 3, display "o"; if AL
contains 2 or 4, display "e".

 CASE AL

1,3: display ‘o’

2,4: display ‘e’

END_CASE
Branches with Compound
Conditions
 Sometimes the branching condition in an IF or CASE takes the
form

condition_1' AND condition_2’

or

condition_1 OR condition_2

 Where condition 1 and condition:2 are either true or false. We


will refer to the

 First of these as an AND condition and to the second as an OR


condition.
Example : AND
 An AND condition is true if and only if Condition_1 and
Condition_2 are both true. Likewise, if either condition is false,
then the whole thing is false.

 Read a character, and if it's an uppercase letter, display


it.

Read a character (into AL)

IF ('A'<= character) and (character <= 'Z')

THEN

display character

END IF
Converting to Assembly
;read a character CMP AL, 'Z'

MOV AH,1 JNGE END_lF ;no


exit
INT 21H
MOV DL, AL.
if ('A' <= char> and
(chai: <= 'Z') MOV AH, 2

CMP AL, ‘A ;char >’A’ INT 21H

JNGE END_lF ;no exit END_IF:


OR Conditions
 Condition_1 OR condition_2 is true if at least one of the conditions is true;
it is only false when both conditions are false.

 Read a character. If it's "y" or "Y", display it; otherwise,


terminate the program.

Read a character (into AL)

IF (character = ‘y' OR (character = 'Y'J

THEN

display it

ELSE

terminate the program

END IF
ASSEMBLY CONVERSION
MOV AH,1 THEN:

INT 21H MOV AH,2 ;prepare


to display
CMP AL,’y’
MOV CL,AL ;get char
;AL==‘y’
INT 21H ;display
JE THEN it
CMP AL, 'Y' ;char JMP END IF ;and exit
~ 'Y'? -

JE THEN ;yes, go ELSE_:


to display it MOV AH, 4CH
JMP ELSE_ ;no - INT 21H ;DOS
Terminate exit
END_IF:
Looping Structure
 A loop Is a sequence of instructions that is
repeated.

 The number of times to repeat may be known in


advance, or

 It may depend on conditions

FOR LOOP

WHILE LOOP

REPEAT LOOP
FOR LOOP
• FOR LOOP is a loop structure in which the loop statements are repeated
a known number of times (a count-controlled loop). In pseudo
code,

FOR loop_count times DO

Statements

END_FOR

• The LOOP instruction can be used to implement a FOR loop. i.e.

LOOP destination_label

• The counter for the loop is the register CX which is initialized to


loop_count.

• Execution of the LOOP Instruction causes CX to be decremented


automatically,
FOR LOOP
• The control is transferred to
destination_label until CX
becomes 0.

• A FOR LOOP can be


implemented using the LOOP
instruction:

TOP:

;initialize CX to loop_count

;body of the loop

LOOP TOP
Example:
• Write a count- MOV CX,0
controlled loop to
display a row of 80 MOV AH,2
stars: MOV DL, '*'

FOR 80 times DO TOP:

display ‘*’ INT 21H

END_FOR LOOP TOP


JCXZ and The LOOP
 FOR LOOP executes at least once.

 if CX contains 0 when the loop is entered, the


LOOP instruction causes CX to be decremented to
FFFFh

 The loop is then executed FFFFh=65535 times


more!

 To Prevent this, the instruction JCXZ (jump if CX is


zero) may be used before the loop. Its syntax


JCXZ destination_label
Use of JCXZ
• If CX contains 0, control transferred to the
destination label. So a loop implemented as follows
is bypassed if CX is 0:

JCXZ SKIP

TOP:

;body of the loop

LOOP TOP

SKIP:
WHILE LOOP

• This WHILE LOOP


depends on a
condition.

• WHILE condition DO

statements

END_WHILE
WHILE LOOP
 The condition is checked at the top of the loop.

 If true, the statements are executed;

 If false, the program goes on to whatever


follows.

 It is possible the condition will be false initially, in


which case the loop body ls not executed at all.

 The loop executes as long as the condition is true


Example: WHILE LOOP
• Write some code to count the number of characters in an
input line.

MOV DX,0 ; char count


• Initialize count to 0 MOV AH,1

• Read a character INT 21H

WHILE_:
• WHILE character <>
carriage_return DO CMP AL,0DH ; CR ?

JE END_WHILE ;yes, exit


• count =count + 1
INC DX ; not CR so inc
• read a character INT 21H; read next char

• END_WHILE JMP WHILE_ ; loop again

END_WHILE:
WHILE LOOP Insights
 A WHILE loop checks the terminating condition
at the top of the loop,

 So, you must make sure that any variables


involved in the condition are initialized
before the loop is entered.

 So you read a character before entering the


loop, and read another one at the bottom.

 The label WHILE_: .is used because WHILE is a


reserved word
REPEAT LOOP

REPEAT

statements

UNTIL condition

In a REPEAT…UNTIL loop, the statements are


executed, and then the condition is checked.

• If true, the loop terminates;

• If false, control branches to the top of the loop.


 Write code to read characters until a
blank is read.

MOV AH,1
REPEAT
REPEAT:
read a character
INT 21H

UNTIL character is a CMP AL,' '


BLANK
JNE REPEAT
Difference between WHILE
and REPEAT
 Use of a WHILE loop or a REPEAT loop Is a matter of personal
preference.

 The advantage of a WHILE is that the loop can be bypassed


if the terminating, condition is initially false.

 Whereas the statements in a REPEAT must be done at least


once.

 However, the code for a REPEAT loop Is likely to be a little


shorter because there is only a conditional jump at the end,

 But a WHILE loop has two jumps: a conditional jump at the


top and a JMP at the bottom.
Programming with High-Level
Structures
• Problem:

• Prompt the user to enter a line of text.

• On the next line, display the capital Letter entered that


comes first alphabetically and the one that comes last.

• If no capital letters are entered, display "No capital letters".

• The execution should look like this:


Type a line of text:
THE QUICK BROWN FOX
JUMPED.
First capital= B Last
capital= X
Solving Method:
• We use the top-down program design like the high-
level language programming.

• In this method, the original problem is solved by


solving a series of sub problems, each of which Is
easier to solve than the original problem.

• Each sub problem is in turn broken down further until


we reach a level of sub problems that can be coded
directly.

• The use of procedures (Chapter 8) will explain these in


detail.
Solution: First
refinement

1.Display-the opening message.

2.Read and process a line of text.

3.Display the results. - ·


Step 1. Display the opening
message.
PROMPT DB 'Type a line of text:', OOH, OAH,
'S'

MOV AH,9

LEA DX,PROMPT

INT 21H
Step 2. Read and process a line of
text.
Read a character

WHILE character ls not a carriage return DO

IF character is a capital letter (*)

THEN

IF character precedes first capital

THEN

first capital = character

END_IF

IF character follows last capital

THEN

last capital = character

END_IF

END IF
 Variables FIRST and LAST must have values before the
WHILE loop is executed the first time. They can be
initialized in the data segment

MOV AH,1

INT 21H
MOV FIRST,AL ;FIRST - char
CHECK_LAST:
WHILE_:
CMP AL,LAST ;char > LAST?
CMP AL,0DH ;CR?
JNG END_IF ;no, <=
JE END_WHILE ;yes, exit
MOV LAST I AL ; LAST - char
CMP AL, 'A' ;char >= 'A'?

JNGE END_IF ; not a capital END IF:

CMP AL, 'Z' ;char <- "Z'? INT 21H ; char in AL

JNLE END -IF ;not a capital..then JMP WHILE ;repeat loop


CMP AL, FIRST ;char < FIRST? END WHILE:
JNL CHECK_LAST
Step 3. Display the results.
NOCAP_MSG DB 'No capitals
$' MOV AH,9

CAP_MSG DB 'First ;if no capitals were typed then


capital =‘

FIRST DB ‘]’ CMP FIRST, ‘]’ ; FIRST = ] ?

DB ‘Last JNE CAPS ; NO, display


capital=‘
result
LAST DB '@ $'
IF no capitals were typed,
LEA DX, NOCAP_MSG

THEN JMP DISPLAY

display "No capitals" CAPS:

ELSE LEA DX, CAP_MSG

display First and Last Capital DISPLAY:

END IF INT 21H


***The Complete program may be found in page 111(6.2)
Assignment-2

• Solve Exercise 1 to 7 and (hand Written


Hard Copy) [page-113-115]

• Programming Exercise: 8 to 12 (lab Test)


[page-115-116]

You might also like