COA Lecture-6 (B) Flow Control Instructions (Theory)
COA Lecture-6 (B) Flow Control Instructions (Theory)
or
condition_1 OR condition_2
Where condition 1 and condition:2 are either true or false. We will refer to
the
THEN
display character
END IF
Converting to Assembly
;read a character CMP AL, 'Z'
Read a character. If it's "y" or "Y", display it; otherwise, terminate the program.
THEN
display it
ELSE
terminate the program
END IF
Assembly Conversion
MOV AH,1 THEN:
END_IF:
Looping Structure
A loop Is a sequence of instructions that is repeated.
1. FOR LOOP
2. WHILE LOOP
3. 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,
• LOOP destination_label
• The counter for the loop is the register CX which is initialized to loop_count.
• TOP:
• ;initialize CX to loop_count
• ;body of the
loop
LOOP TOP
Example:
MOV CX,80
LOOP TOP
JCXZ and The LOOP
FOR LOOP executes at least once.
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:
LOOP TOP
SKIP:
WHILE LOOP
• WHILE condition DO
statements
END_WHILE
WHILE LOOP
The condition is checked 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.
UNTIL condition
In a REPEAT…UNTIL loop, the statements are executed, and then the condition is checked.
REPEAT REPEAT:
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.
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.
References
• https://www.slideshare.net/prodipghoshjoy/flow-control-instructions-6060
2372
Books