📄 sample4.asm
字号:
dataseg segment
namepar label byte ;人名表;存放人名信息
maxnlen db 21
actnlen db ?
names db 21 dup(?)
phonepar label byte ;电话号码表;存放号码信息
maxplen db 9
actplen db ?
phone db 9 dup(?)
crlf db 13,10,'$' ;回车换行
endaddr dw ?
mess1 db 'Input name:','$' ;提示输入信息
mess2 db 'Input a telephone number:','$'
mess3 db 'Do you want a telephone number?(Y/N)','$'
mess4 db 'name?','$'
mess5 db 'name',16 dup(' '),'tel',0dh,0ah,'$'
mess6 db 'Not in the table.',0dh,0ah,'$'
mess7 db 'Invalid input!',0dh,0ah,'$'
count db 0 ;输入计数
tel_tab db 50 dup(20 dup(' '),8 dup(' ')) ;总表,存放人名和号码信息
temp db 20 dup(' '),8 dup(' '),0dh,0ah,'$' ;暂存单元
swapped db 0 ;排序结束标志
dataseg ends
codeseg segment
assume cs:codeseg,ds:dataseg,es:dataseg
main proc far
push ds ;保存断点信息
sub ax,ax
push ax
mov ax,dataseg
mov ds,ax
mov es,ax
cld ;设置循环方向
lea di,tel_tab ;di中存放表首地址
inputloop:
mov ah,09h ;提示'Input name'
lea dx,mess1
int 21h
call input_name
cmp actnlen,0 ;没有输入人名时
jz a10 ;直接跳到提示是否查找的地方
cmp count,50 ;输入上限;跳转到查找模块
je a10
call stor_name ;保存人名到tel_tab
;提示'Input a telephone number'
mov ah,09h
lea dx,mess2
int 21h
call input_stor_phone ;输入并保存电话号码
jmp inputloop
a10:
cmp count,1
jbe searchloop ;如果没有输入或者输入一个
call name_sort ;排序
call disp_all ;显示所有
searchloop:
lea dx,mess3
mov ah,09h
int 21h
mov ah,01h
int 21h
cmp al,'N' ;区分大小写字母
je exit
cmp al,'n'
je exit
cmp al,'Y'
je showname
cmp al,'y'
je showname
mov ah,09
lea dx,crlf
int 21h
lea dx,mess7 ;非法输入
mov ah,09h
int 21h
jmp searchloop
showname:
mov ah,09
lea dx,crlf
int 21h
lea dx,mess4 ;当输入Y时,显示'name?'
mov ah,09
int 21h
call input_name ;输入要查找的人名
call name_search ;查找
call printline
jmp searchloop
exit:
ret
main endp
input_name proc near
mov ah,0ah ;输入人名
lea dx,namepar
int 21h
mov ah,02h
mov dl,0ah ;换行
int 21h
mov bh,0
mov bl,actnlen ;置人名长度到bx
mov cx,21
sub cx,bx ;cx循环次数
i20:
mov names[bx],20h ;未写入的字符置为空格
inc bx
loop i20
ret
input_name endp
stor_name proc near
inc count
cld
lea si,names
mov cx,10 ;把name中的前20个字符放入tel_tab中每一个人信息的前20个byte中
rep movsw
ret
stor_name endp
input_stor_phone proc near
mov ah,0ah
lea dx,phonepar
int 21h
mov ah,02h
mov dl,0ah ;换行
int 21h
mov bh,0
mov bl,actplen
mov cx,9
sub cx,bx ;计算剩下的长度
is20:
mov phone[bx],20h ;剩下的地方填充空格
inc bx
loop is20
cld
lea si,phone
mov cx,4 ;把phone中的前8个字符放入tel_tab中每一个人信息的后8个byte中
rep movsw
ret
input_stor_phone endp
name_sort proc near ;排序模块
sub di,56
mov endaddr,di;设置结束地址
ns10:
mov swapped,0
lea si,tel_tab
ns20:
mov cx,20
mov di,si
add di,28 ;下一个被比较的名字
mov ax,di
mov bx,si
repe cmpsb
jbe ns30 ;小于或等于不用交换
call npxchg
ns30:
mov si,ax
cmp si,endaddr
jbe ns20
cmp swapped,0
jnz ns10
ret
name_sort endp
npxchg proc near
mov cx,14
lea di,temp
mov si,bx
rep movsw ;move lower item to save
mov cx,14
mov di,bx
rep movsw
mov cx,14
lea si,temp
rep movsw
mov swapped,1
ret
npxchg endp
disp_all proc near ;显示所有信息模块
mov ah,09h
lea dx,mess5
int 21h
lea si,tel_tab
da10:
lea di,temp
mov cx,14
rep movsw
mov ah,09h
lea dx,temp
int 21h
dec count
jnz da10
ret
disp_all endp
name_search proc near
lea si,tel_tab
mov bx,si
add endaddr,28
nase10:
lea di,names ;存放待查找的人名地址
mov cx,10
repe cmpsw
jcxz nase_exit ;相等
add bx,28 ;表中下一个比较的人名
mov si,bx
cmp si,endaddr
jbe nase10
notintab:
mov cx,-1
ret
nase_exit:
mov si,bx
lea di,temp
mov cx,14
rep movsw
ret
name_search endp
printline proc near ;打印结果
cmp cx,-1
je norecord
mov ah,09h
lea dx,mess5
int 21h
mov ah,09h
lea dx,temp
int 21h
ret
norecord:
mov ah,09h
lea dx,mess6
int 21h
ret
printline endp
codeseg ends
end main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -