L5 - Branching Structures
L5 - Branching Structures
L5 - Branching Structures
1
Branching Structure: IF‐THEN
• Pseudocode in HLL:
IF condition is true
THEN
execute true‐branch statements false true
condition
End_if
True branch
statements
2
Example
Replace the number in AX by its absolute value.
value
Pseudocode Code
IF AX <0 CMP AX,0
AX 0
Then JNL END_IF
replace AX by –AX NEG AX
END_IF END_IF:
20/01/2015 3
Branching Structure: IF‐THEN‐ELSE
• Pseudocode in HLL:
IF condition is true
THEN
execute true‐branch statements
FALSE TRUE
ELSE CONDITION
execute false‐branch statements
End_IF FALSE BRANCH TRUE BRANCH
STATEMENTS STATEMENTS
4
EXAMPLE
Suppose AL and BL contains extended ASCII characters. Display the
one that
h comes fi first iin the
h character
h sequence.
Pseudocode Code:
IF AL < BL MOV AH,2 ; prepare to display
THEN CMP AL,BL ; AL<=BL?
display the char in AL JNBE ELSE_ ;no, display char in BL
ELSE MOV DL,AL
, ; move char to be displayed
p y
display the char in BL JMP DISPLAY ; go to display
END_IF ELSE_: ; BL<AL
MOV DL
DL,BL
BL
DISPLAY:
INT 21H ; display it
END IF:
END_IF:
20/01/2015
5
Branching Structure: CASE
A case is a multiway branch structure that tests a
register, variable, or expression for particular values
or range of values. The general form is as
follows:
Case Expression
values_1: statements_1
values_2: statements_2
…………..
values_n: statements_n
END_CASE
20/01/2015
6
Case Expression:
values_1:
values 1: statements_1
statements 1
values_2: statements_2
…………..
values_n: statements_n
END_CASE EXPRESSION
7
EXAMPLE
IF AX contains a negative number
number, put ‐11 in BX;
if AX contains 0, put 0 in BX; if AX contains a
positive number
number, put 1 in BX.
BX
20/01/2015
8
Pseudocode Code:
CMP AX,0 ; test AX
Case AX
JL NEG ; AX<0
AX 0
< 0:put ‐1 in BX JE ZERO ;AX=0
p 0 in BX
= 0:put JG POS ;AX>0
NEG: MOV BX,‐1 ; put ‐1 in BX
> 0:put 1 in BX
JMP END_CASE ; and exit
END CASE
END_CASE
ZERO: MOV BX,0 ; put 0 in BX
JMP END_CASE ; and exit
END CASE:
END_CASE:
•NOTE : only one CMP is needed, because jump instructions do not affect
th flags.
the fl
9
Branches with compound conditions
• Sometimes the branching condition in an IF or
CASE takes the forms
Condition_1
Condition 1 and condition_2
condition 2
Condition_1 or condition_2
20/01/2015
10
AND conditions
• An AND condition is true if and only if condition_1 and
condition_2 are both true.
• Iff any condition
d is false,
f l then
h the
h whole
h l thing
h is ffalse.
l
false t
true
Con1 & Con2
DISPLAY
20/01/2015 EXIT
11
EXAMPLE
Read a character and if itit’ss an uppercase letter,
letter display it.
it
Pseudocode
If ((‘A’
A >= character) and (character <=‘Z’)
<= Z )
Then
Code:
Display
p y character
End_if MOV AH,1 MOV DL,AL
INT 21H MOV AH,2
CMP AL,’A’ INT 21H
JL END_IF END_IF:
CMP AL,’Z’
AL ’Z’
JG END_IF
20/01/2015
12
OR conditions
• If condition_1 OR condition_2 is true then an OR conditions
is true.
• if both
b th conditions
diti are false,
f l th then th
the whole
h l thing
thi is
i false.
f l
false t
true
Con1 OR Con2
DISPLAY
20/01/2015 EXIT
13
Example
Read a character
character. If it’s
it s “y”
y or “Y”
Y , display it; otherwise,
otherwise
terminate the program.
Pseudocode
if (character = “y” or character
character= “Y”)
Then Code:
Display character
MOV AH,1
AH 1 THEN:
Else
INT 21H MOV AH,2
Terminate the program
End if
End_if CMP AL,, ’y’
y MOV DL,AL
,
JE THEN INT 21H
CMP AL,’Y’ JMP END_IF
JE THEN ELSE_:
JMP ELSE_ END_IF:
20/01/2015
14
THANK YOU