Chapter 5
Branching Program Design
∣X-Y ∣=?
Branching MOV AX,X
CMP AX,Y
In many practical problems processing, the
JAE LLL
program is not always executed
XCHG AX,Yaccording to the
instructions written order,and
LLL: SUB AX,Yit is often needed to
change the executing orderMOVof program
[3000],AX
instructions accordingX toDB
the43F5H
different conditions .
Y DB 37E6H
The system provides the Jump instructions.
Jump instructions are used to choose the next
instruction to be executed according to certain
conditions (F register flage bits).
This chapter covers Branching Program Structure,
Jump instructions used to complete Branching and
some examples.
5.1 Branching program structure
IF – THEN - ELSE
∣X-Y ∣=?
MOV AX,X
T CMP AX,Y
Condition
JAE LLL
F
XCHG AX,Y
Program LLL: SUB AX,Y
segment MOV [3000],AX
X DB 43F5H
Y DB 37E6H
Flow chart of branching program structure
5.2 Unconditional Jump
Instruction format
JMP Statement Label
MAIN: MOV AX,[2000]
MOV BX,3456H
ADD AX,BX
:
:
:
JMP MAIN
5.3 Conditional Jumps
Conditional jump instructions determine the location
of the statement to be executed next according to
the conditions formed in front .
∣X-Y ∣=?
Instruction format MOV AX,X
Jxx Statement Label CMP AX,Y
JAE LLL
XCHG AX,Y
Conditions LLL: SUB AX,Y
MOV [3000],AX
X DB 43F5H
Y DB 37E6H
Conditional Jump Instructions
Instructions Functions Conditions
JC When Carry(Borrow),jump CF=1
JNC When no Carry(Borrow),jump CF=0
JZ/JE When the result is 0/equal,jump ZF=1
JNZ/JNE When the result is not 0/equal,jump ZF=0
JA/JNBE When above/below and not equal(A﹥B) CF=0 AND ZF=0
JAE/JNB When above or equal/not below(A≥B) CF=0 OR ZF=1
JB/JNAE When below/not above and not equal (A < B) CF=1 and ZF=0
JBE/JNA When below or equal /not above (A≤B) CF=1 OR ZF=1
5.4 Branching Program Design
Example 1
Write a program to convert the uppercase letters entered
on the keybord to the corresponding lowercase letters
and display them.
ASCII Codes for Letters Begin
A 41 a 61
B 42 b 62
C 43 c 63
Input letter
D 44 d 64 →AL
E 45 e 65
F 46 f 66 T
G 47 g 67
AL=‘#’
H 48 h 68
F
I 49 i 69
F
J 4A j 6A AL
K 4B k 6B
≥’A’
L 4C l 6C T
M 4D m 6D
F
N 4E n 6E
AL
≤’Z’
O 4F o 6F
T
P 50 p 70
Q 51 q 71
AL+20H Output
R 52 r 72
→AL ERROR!
S 53 s 73
T 54 t 74 AL→DL
U 55 u 75
Output Letter
V 56 v 76
W 57 w 77
X 58 x 78
End
Y 59 y 79
Z 5A z 7A The program flow chart
DATA SEGMENT
ERRMESS DB ‘ERROR!’,’$’
DATA ENDS
STACK SEGMENT STACK
DB 100 DUP (?)
STACK ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,SS:STACK
START: MOV AX,DATA
MOV DS,AX
LLL: MOV AH,1 ;Input a letter and send it to AL
INT 21H
CMP AL,’#’ ;End when ‘#’ is inputted
JZ DONE
CMP AL,’A’ ;If what is inputted is not a uppercase
JB ERR ; letter,output the error message
CMP AL,’Z’
JA ERR
ADD AL,20H; Convert to the corresponding lowercase
MOV DL,AL ; letter
MOV AH,2 ;Display the corresponding lowercase letter
INT 21H
JMP LLL ;Input the next letter
ERR: MOV DX,OFFSET ERRMESS ;Output the error message
MOV AH,9
INT 21H
DONE: MOV AH,4CH ;Go back to DOS
INT 21H
CODE ENDS
END START
Example 2
Write a program to pick out the maximum number of
3 positive numbers,and send it to MAX word memory
unit.
DATA SEGMENT
X DW 1234H Begin
Y DW 4321H
Z DW 3421H
MAX DW 1234H X →AX
DATA ENDS
STACK SEGMENT STACK
DB 100 DUP (?) T F
STACK ENDS X >
CODE SEGMENT Y
F
ASSUME CS:CODE,DS:DATA,SS:STACK X >Z Y →AX
BEGIN: MOV AX,DATA
MOV DS,AX T
MOV AX,X ; Suppose X is the maximum F
CMP AX,Y ; X > Y? Y >Z
JA XGZ
MOV AX,Y ;Now , Suppose Y is the maximum
Z →AX T
CMP AX,Z ; Y > Z?
JA NEXT
ZTOAX: MOV AX,Z
JMP NEXT
XGZ: CMP AX,Z ; X > Z?
JNA ZTOAX AX →MAX
NEXT: MOV MAX,AX
MOV AH,4CH ;Go back to DOS
INT 21H End
CODE ENDS
END BEGIN The program flow chart