单分支程序设计
对2个8位非符号数,按递减次序重新排序
data segment
num db 12h,65h
data ends
code segment
assume cs:code,ds:data
main:
mov ax,data
mov ds,ax
mov al,num
cmp al,num[1]
jae Done;因为使比较非符号数,所以使用jae(大于等于)
mov dl,num[1];如果num<num[1],那么需要交换次序
mov num[1],al
mov num,dl
Done:
mov ah,4ch
int 21h
code ends
end main
从键盘读入一个小写字母,显示对应的大写字母,若读入的字母不是小写字母,则不显示
data segment
Msg1 db 'Input a letter:$'
Msg2 db 0Dh,0Ah,'Uppercase letter is :' ;这里0Dh,0Ah用来换行
result db ?,'$'
data ends
code segment
assume cs:code,ds:data
main:
mov ax,data
mov ds,ax
;调用int21h的9h功能,显示一个字符串
;ah=功能号,ds:dx=字符串首地址
mov dx,offset Msg1
mov ah,9h
int 21h
;;调用1h功能,读入一个字符,al读入的字符ASCii码
mov ah,1h
int 21h
cmp al,'a' ;因为大写字母的asiic小于小写字母
jb Exit
cmp al,'z'
ja Exit
sub al,20h ;转化为大写字母
mov result,al
mov dx,offset Msg2
mov ah,9h
int 21h
Exit:
mov ah,4ch
int 21h
code ends
end main
双分支
定义两个16位符号数,以递增次序重新排列,排序完毕后,显示swapped,如果本来就是,则显示need no swap
data segment
Msg1 db 'Swapped!',0Dh,0Ah,'$'
Msg2 db 'Need no Swap!',0Dh,0Ah,'$'
num dw 9a10h,7932h
data ends
code segment
assume cs:code,ds:data
main:
mov ax,data
mov dx,ax
mov ax,num
cmp ax,num[2]
jle Skip;因为使两个符号数比较大小,所以使用jle
xchg ax,num[2];ax保存着num,这下num[2]变成了num的值,现在ax保存着原来num[2]
mov num,ax
mov dx,offset Msg1
mov ah,09h
int 21h
jmp Done
Skip:
mov dx,offset Msg2
mov ah,09h
int 21h
Done:
mov ah,4ch
int 21h
code ends
end main
多分支
在键盘上输入1-7对应星期1-7,其他值显示invali function number
逻辑分解法
将多分支结果以逻辑等效的方法,分解为一串双分支结构
data segment
Msg db 'Input a number(1-7):$'
Msg1 db 0Dh,0Ah,'Monday','$'
Msg2 db 0Dh,0Ah,'Tuesday','$'
Msg3 db 0Dh,0Ah,'Wednesday','$'
Msg4 db 0Dh,0Ah,'Thursday','$'
Msg5 db 0Dh,0Ah,'Friday','$'
Msg6 db 0Dh,0Ah,'Saturday','$'
Msg7 db 0Dh,0Ah,'Sunday','$'
ErrMsg db 0Dh,0Ah,'Invalid function number','$'
data ends
code segment
assume cs:code,ds:data
main:
mov ax,data
mov ds,ax
mov dx,offset Msg
mov ah,9h
int 21h
mov ah,1h
int 21h
cmp al,'1'
je Func1
cmp al,'2'
je Func2
cmp al,'3'
je Func3
cmp al,'4'
je Func4
cmp al,'5'
je Func5
cmp al,'6'
je Func6
cmp al,'7'
je Func7
mov dx,offset ErrMsg
jmp Output
Func1:
mov dx,offset Msg1
jmp Output
Func2:
mov dx,offset Msg2
jmp Output
Func3:
mov dx,offset Msg3
jmp Output
Func4:
mov dx,offset Msg4
jmp Output
Func5:
mov dx,offset Msg5
jmp Output
Func6:
mov dx,offset Msg6
jmp Output
Func7:
mov dx,offset Msg7
Output:
mov ah,9h
int 21h
mov ah,4ch
int 21h
code ends
end main
地址表法
把多个分支的每个程序段的首地址顺序存放在一个存储区中,这个存储区称为地址表存储区
data segment
Msg db 'Input a number(1-7):$'
Msg1 db 0Dh,0Ah,'Monday','$'
Msg2 db 0Dh,0Ah,'Tuesday','$'
Msg3 db 0Dh,0Ah,'Wednesday','$'
Msg4 db 0Dh,0Ah,'Thursday','$'
Msg5 db 0Dh,0Ah,'Friday','$'
Msg6 db 0Dh,0Ah,'Saturday','$'
Msg7 db 0Dh,0Ah,'Sunday','$'
ErrMsg db 0Dh,0Ah,'Invalid function number','$'
AddrTab dw Func1,Func2,Func3,Func4,Func5,Func6,Func7
data ends
code segment
assume cs:code,ds:data
main:
mov ax,data
mov ds,ax
mov dx,offset Msg
mov ah,9h
int 21h
mov ah,1h
int 21h
cmp al,'1'
jb Error
cmp al,'7'
ja Error
;1-31h,7-37h
and ax,000fh
dec ax
shl ax,1;因为对应着的使dw,所以要乘以2
mov bx,ax
jmp AddrTab[bx]
Error:
mov dx,offset ErrMsg
jmp Output
Func1:
mov dx,offset Msg1
jmp Output
Func2:
mov dx,offset Msg2
jmp Output
Func3:
mov dx,offset Msg3
jmp Output
Func4:
mov dx,offset Msg4
jmp Output
Func5:
mov dx,offset Msg5
jmp Output
Func6:
mov dx,offset Msg6
jmp Output
Func7:
mov dx,offset Msg7
Output:
mov ah,9h
int 21h
mov ah,4ch
int 21h
code ends
end main
如果主程序与各功能程序不在同一个代码段内,需要采用远地址指令,此时,就应该是AddrTab dd FUnc1
地址表也可以用来存储变量的地址
data segment
Msg db 'Input a number(1-7):$'
Msg1 db 0Dh,0Ah,'Monday','$'
Msg2 db 0Dh,0Ah,'Tuesday','$'
Msg3 db 0Dh,0Ah,'Wednesday','$'
Msg4 db 0Dh,0Ah,'Thursday','$'
Msg5 db 0Dh,0Ah,'Friday','$'
Msg6 db 0Dh,0Ah,'Saturday','$'
Msg7 db 0Dh,0Ah,'Sunday','$'
ErrMsg db 0Dh,0Ah,'Invalid function number','$'
MsgTab dw Msg1,Msg2,Msg3,Msg4,Msg5,Msg6,Msg7
data ends
code segment
assume cs:code,ds:data
main:
mov ax,data
mov ds,ax
mov dx,offset Msg
mov ah,9h
int 21h
mov ah,1h
int 21h
cmp al,'1'
jb Error
cmp al,'7'
ja Error
;1-31h,7-37h
and ax,000fh
dec ax
shl ax,1;因为对应着的使dw,所以要乘以2
mov bx,ax
mov dx,MsgTab[bx]
jmp Output
Error:
mov dx,offset ErrMsg
Output:
mov ah,9h
int 21h
mov ah,4ch
int 21h
code ends
end main