简单的通讯录

无二不说,先上代码:

DATAS SEGMENT
;此处输入数据段代码
namepar label byte
maxnlen db 21
actnlen db ?
_name db 21 dup(?) ;在MASM5.0下不能用name哦
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
DATAS ENDS

STACKS SEGMENT
;此处输入堆栈段代码
STACKS ENDS

CODES SEGMENT
main proc far
ASSUME CS:CODES,DS:DATAS,SS:STACKS

push ds
sub ax,ax
push ax
MOV AX,DATAS
MOV DS,AX
mov es,ax
cld
lea di,tel_tab ;di中存放表首地址
;此处输入代码段代码
inputloop:
;提示'Input name'
mov ah,09h
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 ;排序
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
mov cx,21
sub cx,bx
i20:
mov _name[bx],20h
inc bx
loop i20
ret
input_name endp
;*********************************************************
stor_name proc near
inc count
cld
lea si,_name
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,_name ;存放待查找的人名地址
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
MOV AH,4CH
INT 21H
CODES ENDS
END main


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值